I'm supposed to plot (x1 vs x2) for a system. (Dynamics and Controls)

I have a system in state space model x'=Ax where A = [0 1; -14 -4]. The system quation is x(t) = e^At*x(0) where given x(0) is [-0.2; 0.2] Now I'm supposed to solve for x(t) which gives a matrix [x1(t);x2(t)] and plot the graph of the elements x1(t) vs x2(t) which is also known as Phase portrait. I believe its a simple code but I'm going wrong somewhere. Can you help me where and what's wrong? Any help is appreciated. Thank you!
A=[0 1;-14 -4];
t=linspace(0,10,300);
x=zeros(2,300);
matA = zeros(2,2,300);
x0 = [-0.2;0.2];
for k=1:300, matA(:,k) = expm(A*t(k)); x(:,k) = matA(:,k)*x0;end;
plot(x(:,1),x(:,2))

 Accepted Answer

You’re almost there. You did everything correctly, but you need a ‘C’ output matrix (part of the ‘ABCD’ matrices in a state space system). I added one I considered appropriate to your problem, and since this creates ‘x’ as a (2x300) array, I changed the plot references to reflect that:
A=[0 1;-14 -4];
t=linspace(0,10,300);
x=zeros(2,300);
matA = zeros(2,2,300);
x0 = [-0.2;0.2];
C = [1 0; 0 1];
for k=1:300,
matA(:,:,k) = expm(A*t(k));
x(:,k) = C*matA(:,:,k)*x0;
end
figure(1)
plot(x(1,:),x(2,:))
grid
The plot looks as a phase space plot should!

4 Comments

Hey, thanks for the reply. Its a homogenous state equation in the form x'=Ax instead of x'=Ax+Bu. And since the plot is a phase diagram we will be dealing with x(t) only and therefore we don't use any C matrix. [y=Cx+Du is the output equation and C belongs there. We don't use C for plotting but only A and B to solve for x(t). Here we don't have any B and so we only have A]
My pleasure.
In that case eliminate ‘C’ and your loop becomes:
A=[0 1;-14 -4];
t=linspace(0,10,300);
x=zeros(2,300);
matA = zeros(2,2,300);
x0 = [-0.2;0.2];
for k=1:300,
matA(:,:,k) = expm(A*t(k));
x(:,k) = matA(:,:,k)*x0;
end;
plot(x(1,:),x(2,:))
grid
with the same plot. Your original problem was that you preallocated ‘matA’ correctly, but you didn’t specify the correct number of dimensions for it in your loop. I added the extra dimension and it works. I also corrected your ‘x’ array references in your plot call so it now plots correctly.
Yeah, no wonder the command window showed errors related to dimensions. It's all good now. I got exactly what I want. Thanks!
My pleasure!
Have fun in your controls course, and take as many others as you can!

Sign in to comment.

More Answers (0)

Asked:

PVR
on 12 Nov 2014

Commented:

on 13 Nov 2014

Community Treasure Hunt

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

Start Hunting!