MATLAB: Simulate dynamic response to a state space differential equation

differential equationdynamic responsestate-space

Hi guys!
I am a beginner with MATLAB, forgive me for any stupid expression and this terrible formatting. I am trying to get a dynamic response for a state space differential equation in a matrix form: Xdot=A*x+B*u (A is 4by4, B is 4by2)
The input u is [1;0] between interval [0,1], and is 0 everywhere else. I have used a stupid way to construct my u, but I guess it should work. Here's the code I have:
A=[-2.6 0.25 -38 0; -0.075 -0.27 4.4 0; 0.078 -0.99 -0.23 0.052; 1.0 0.078 0 0];
B=[17 7; 0.82 -3.2; 0 0.046; 0 0];
w1=zeros(1,10000);
w3=zeros(1,10000);
w2=ones(1,10001);
t=-1:0.0001:2.000;
u1=[w1 w2 w3];
u2=zeros(1,30001);
u=[u1; u2];
lsim(sys1,u,t)
However, the plot seems to be u(1:) of t only, and I would like to get a response for x1, x2, x3, and x4.
I then tried the following code:
A=[-2.6 0.25 -38 0; -0.075 -0.27 4.4 0; 0.078 -0.99 -0.23 0.052; 1.0 0.078 0 0];
B=[17 7; 0.82 -3.2; 0 0.046; 0 0];
w1=zeros(1,10000);
w3=zeros(1,10000);
w2=ones(1,10001);
t=-1:0.0001:2.0001;
u1=[w1 w2 w3];
u2=zeros(1,30001);
u=[u1; u2];
x=zeros(4,30001);
for k=1:1:30001
x(:,k+1)=A*x(:,k)+B*u(:,k);
end
Then I have checked the values for x(:,10002) to x(:,11000) column by column, and found out that the values got too large that eventually MATLAB gave me NaN.
Is any of my approaches might work? Or either of them is correct…? Also, is there any more efficient way to solve this? Thanks!

Best Answer

How about defining your input like this.
u = [t <= 1 & t >= 0; zeros(size(t));];
I also have few comments.
  • I don't understand why you would start your system with a 'negative' time. Why not simply start with 0?
  • Your input, u, as you defined is u = [w1, w2, w3]. However, the size of your w2 is different than the other two, which doesn't make much sense to me.