MATLAB Answers

how to use ss and lsim for 1 dof differential equation

26 views (last 30 days)
Hiroki
Hiroki on 1 Jan 2021
Commented: Hiroki on 2 Jan 2021
i want to solve 1 dof differential equation by using ss and lsim for just as an example.
probrem is the result calculated by ss&lsim and the result calcuated by matmatically analitically caliculation are different.
do you know why this things happen? if my code has some mistake, I want to know where is prob.
Best,
hiroki
---code---
M=4;
C=0;
K=2500;
A=[0 1; -K/M -C/M];
B=[0;1/M];
C=[1 0];
D=0;
sys=ss(A,B,C,D)
dt=0.001;
t=dt*[1:100];
w=100;
f=2*cos(w*t);
u=f;
x=2/(K-M*w^2)*cos(w*t);
y = lsim(sys,u,t);
plot(t,u)
hold on
yyaxis right
plot(t,y)
hold on
yyaxis right
plot(t,x);
legend('u:input','y:sys solution','x: analitical solution')

  0 Comments

Sign in to comment.

Accepted Answer

Paul
Paul on 1 Jan 2021
That lsim command calculates y using default initial conditions x(0) = xdot(0) = 0, but the analytical solution clearly doesn't satisfy those initial conditions. It looks like that solution satisfies the initial conditions x(0) = 2/(K-M*w^2), xdot(0) = 0. Try this:
y1 = lsim(sys,u,t,[2/(K-M*w^2) 0]);
and compare to x and y.
Alos, why not define t starting from 0?
t = dt*[0:100]

  1 Comment

Hiroki
Hiroki on 2 Jan 2021
thank you so much! it works! your answer is perfect. I made two mistakes. first is initial condition of x(0) and second is starting definition of t!! thank!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!