Jesus
Jesus

Reputation: 179

How to use a sinewave function into a state space model as an input (for the B matrix) and get only ONE plot?

I would like to have a sinewave input (as the input force) into my state space model. The main issue is that when I do the step response of the system, I get 10 subplots in ONE plot and I only want to see one plot. I am not sure if I am setting my sine input correctly. I think I might need to do a loop so I don't get multiple plots? Not sure.

Below is my code for a simple Spring-Damping Cart System:

k1=150;
k2=700;
b1=15;
b2=30;
M1=5;
M2=20;
w=1;
t=1e-10:.1:1;
F=sin(w*t); %Sinewave input

A=[0 0 1 0; 0 0 0 1; -k1/M1 k1/M1 -b1/M1 b1/M1; k1/M2 -(k1+k2)/M2 b1/M2 -(b1+b2)/M2];
B=[0 0 1/M1 0]'*F;
C=[1 0 0 0];
D=0;

sys=ss(A,B,C,D); %State Space Model

step(sys)
margin(sys)

Want this to be only ONE plot

Thank you.

Upvotes: 0

Views: 1648

Answers (1)

CroCo
CroCo

Reputation: 5741

The step function will apply the unit step input, so this line B=[0 0 1/M1 0]'*F; is incorrect. You are modifying the input matrix. In order to see the response of the system due to the step input, you do

k1=150;
k2=700;
b1=15;
b2=30;
M1=5;
M2=20;
w=1;
t=1e-10:.1:1;
%F=sin(w*t); %Sinewave input

A=[0 0 1 0; 0 0 0 1; -k1/M1 k1/M1 -b1/M1 b1/M1; k1/M2 -(k1+k2)/M2 b1/M2 -(b1+b2)/M2];
B=[0 0 1/M1 0]';
C=[1 0 0 0];
D=0;

sys=ss(A,B,C,D); %State Space Model

step(sys)
figure % create a new figure for Bode plot
margin(sys)

The results are

enter image description here

enter image description here

In order to define an arbitrary input, you may use lsim. In your case, you can do the following

k1=150;
k2=700;
b1=15;
b2=30;
M1=5;
M2=20;
w=1;
%t=1e-10:.1:1;
t = linspace(0, 100, 1000);
F=sin(w*t); %Sinewave input

A=[0 0 1 0; 0 0 0 1; -k1/M1 k1/M1 -b1/M1 b1/M1; k1/M2 -(k1+k2)/M2 b1/M2 -(b1+b2)/M2];
B=[0 0 1/M1 0]';
C=[1 0 0 0];
D=0;

sys=ss(A,B,C,D); %State Space Model

% step(sys)
% figure
% margin(sys)

output = lsim(sys, F, t);                                
plot(t, output)

enter image description here

Upvotes: 1

Related Questions