How to plot x(i+1,j+1) with respect to i,j from the given data ,code shown in the body?

clc;
close all;
clear all;
A1=[0 -.75;-1 0];
A2=[.27 0;0 0];
A3=[0 -.001;0 0];
x(1,1)=1
x(1,2)=2
x(2,1)=1
for i=1:2
for j=1
z=x(i+1,j+1)==A1.*x(i,j+1)+A2.*x(i+1,j)+A3.*x(i,j)
end
end
[i,j]=meshgrid(2:3,2)
surf(i,j,z)
[EDITED, Jan, please format your code]

4 Comments

Please format your code, using the {} code button.
Also, please explain what you're trying to do with that line:
z=x(i+1,j+1)==A1.*x(i,j+1)+A2.*x(i+1,j)+A3.*x(i,j)
At the moment you're checking if x(i+1,j+1) is equal to the expression on the right, and putting the result (0 or 1) in z. It's not going to work since x(i+1,j+1) is not defined, but I'm sure that's not what you intended anyway.
Click on his name to see 4 near duplicates of the same problem. No one could figure it out in those either. So I suggest this link and this link and this link.
The problem is as follows: x(i+1,j+1)=A1*x(i,j+1)+A2*x(i+1,j)+A3*x(i,j) where x(i+1,j+1),x(i,j+1) and x(i+1,j) and x(i,j) , is a state vector of 2x1 dimension and A1,A2 and A3 are constant system matrices of 2x2 dimension, initial conditions is x(1,1)=1;x(1,2)=2;x(2,1)=1 Problem is to plot x(i+1,j+1) with respect to i and j. Values of A1=[0 -.75;-1 0] , A2=[.27 0;0 0], A3=[0 -.001;0 0]
Sir the code which I had tried is not working.
"Is not working" is not useful. Please explain any details.

Sign in to comment.

 Accepted Answer

Hi Abhay,
The problem as you as you have stated it is under-determined. What you additionally need are boundary conditions. For example if you knew the boundary values:
x(1, j) for all j
and
x(i, 1) for all i
Then you could easily generate all the values. Maybe you need to assume that the boundaries are zero?
Also since this is a 2-D to 2-D function, how do you plan to visualize it in a plot?
By the way, to type code, just start the code line with two spaces.

10 Comments

Dear SK Thanks for the answer you are absolutely correct,the state response plot requires boundary conditions.Suppose I take boundary conditions x(1,j)=1 for all j and x(i,1)=1 for all i then can you please show me the code because while do looping I am doing some basic mistake.Moreover I am trying to generate 3-d plot for this 2-D function. With thanks and regards, Abhay
I just posted code but deleted it subsequently because I just read you edited first post.
It seems there is some confusion about the actual problem at hand. You may need to give a clearer statement of the problem. Your code has some puzzling errors. For example:
== checks for equality and returns boolean values. z will therefore only contain true/false values and will be overwritten on each loop. If in case you meant:
x(i+1,j+1) = A1.*x(i,j+1)+A2.*x(i+1,j)+A3.*x(i,j)
then that is not valid code since you are trying to assign a matrix to a scalar.
In Matlab, x(i,j) cannot be vector or array. x(i,j) has to be a scalar (either a number or a cell or a user defined object). Do you need x(i,j) to be a vector? In that case you need to reformulate the problem.
Also note that if A is a (k x k) matrix, and y is a scalar, then:
A.*y
and
A*y
both do the same thing, ie: multiply every element of A with y. On the other hand if y is a (k x 1) vector and A is a (k x k) matrix, then:
A*y
does matrix multiplication of A with y.
Dear sir, Thanks for your kind suggestion. My problem is that I need x(i+1,j+1),x(i+1,j),x(i,j+1)and x(i,j) to be a vector of 2x1 dimension ,even after the for loop operation . The error you pointed out is correct ,I should use single'=' sign in the equation, i.e. x(i+1,j+1) = A1.*x(i,j+1)+A2.*x(i+1,j)+A3.*x(i,j) is ok. Moreover I have sent you one paper attached with this comment for your kind reference,in this paper at the last the author has generated a 3-D state response plot from one of such 2-D discrete system equation. Please help me in providing a right code to solve my problem. With thanks and regards, Abhay
OK, so try this:
A1 = [0, -.75; -1, 0];
A2 = [.27, 0; 0, 0];
A3 = [0, -.001; 0, 0];
N = 100; % 100 iterations.
% Initialize (2 x N x N) array
% First dim is of size 2 so so that you have space to store 2 components
X(2, N, N) = 0;
% Set both components to 1 at both boundaries.
X(:, 1, :) = 1;
X(:, :, 1) = 1;
% Generate the values.
for i = 1 : N-1
for j = 1 : N-1
X(:, i+1, j+1) = A1*X(:, i, j+1) + A2*X(:, i+1, j) + A3*X(:, i, j);
end
end
Now
X1 = reshape(X(1, :, :), [N, N])
is the matrix of the first component and
X2 = reshape(X(2, :, :), [N, N])
is the matrix of the second component.
Dear Sir, Thank you very much for this kind help from you.The plot is also generating in the same way as being proved mathematically in my work.
With sincere thanks and regards, Abhay
Dear sir, There is one more query, that suppose I going to generate the plot, in which the variables X1 and X2 are considered simultaneously, then which one command is OK,and what exactly the third command is doing, because all the three ways are generating a different pattern.The commands are as follows: 1. surf([X1;X2]) 2. surf([X1,X2]) 3. surf(X1,X2). further there is one more doubt and that is to incorporate X1 and X2 simultaneously in the surf command I define another variable W and define W =reshape(X(:,:,:),[N,N]),the symbols and meanings of the other variables are the same as we use in our program, and then use surf(W), then the error occurred at the line where W is defined i.e. the number of elements should be the same in reshape command, but already in our program we define the size of X as X(:,N,N)=0, then why this command is creating problem.
Dear Sir, Please see if you can suggest me anything on my query.
With Regards, Abhay
If you look at the documentation for the surf() funstion in Matlab, you will see that you are using 2 different functions:
surf([X1; X2])
Here you have written [X1; X2] - which is a single matrix formed by concatenating X1 and X2 vertically. Similarly in
surf([X1, X2])
[X1, X2] is also a single matrix formed by concatenating X1 and X2 horizontally.
So in both these you are using the 1-argument form of surf where the (x y) grid is taken to be (1:n) x (1:n). But because [X1; X2] is a different matrix from [X1, X2], the result is different
On the other hand in:
surf(X1, X2)
the first argument is X1 and the second X2. You are using the two argument form of surf(). The second argument specifies only the color shading. So you are only plotting X1.
X is a 2 x N x N matrix, so when you reshape, the result should also have 2*N*N elements. But in your reshape() line it has only N*N elements. For example you could reshape as:
reshape(X, [2*N, N]).
But be very careful when you reshape. In fact the above command is probably not what you want because of the order in which the elements are placed. When Matlab reshapes, it takes the elements, counting along each dimension starting from the first in turn. You have to reshape in such a way that your elements don't get mixed up. In your case you may need to use the permute() function before you reshape.
_________
In addition, let me offer you some unsolicited advice. If you are planning to use Matlab for at least some period of time for your work, it would be worth it to invest some of your time learning the basic syntax and getting familiar with it. It is not exactly my favorite language, but it is very widely used and is relatively easy to learn.
You could start by going to:
The suggested exercises there can help you get comfortable with with Matlab syntax. Then you would not be so dependent on others for these basic language issues. There are also a lot of online tutorials and short documents that give you information about Matlab. Of course you need to be willing to invest some time experimenting with it.
Dear sir, Thanks for the clarification and your kind advice. Now if will try to tune myself more towards, in learning the cody which in my opinion is the base of this matlab programme, thanks for providing a link for cody,as a beginner it helps me a lot.
With Thanks and Regards, Abhay

Sign in to comment.

More Answers (2)

Dear all; Regarding my earlier question of 3D plot of 2-D discrete system which was wonderfully solved by SK, I had posted one query,Kindly put your expert comment over that.Thanks

1 Comment

Dear all; Regarding my earlier question of 3D plot of 2-D discrete system which was wonderfully solved by SK, I had posted one query,Kindly put your expert comment over that.Thanks

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Asked:

on 12 Oct 2014

Commented:

on 16 Oct 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!