plotting matlab state space
Show older comments
plotting matlab state space
I am trying to simulate a very simple dynamical system.
mx''+bx'+kx=u(t)
(mx''+cx'+kx=m*9.81)
I want to draw the displacement (position x) and velocity (v) of the system over a time from 0 to 10s with 3 different time stepsizes. i am supposed to work with an A-matrix approach;
ydot = [(-c/m)(-k/m);1 0] [x xdot] +[9.81;0]
i have no idea where to start due to the fact im new to matlab. Here is what i have
clc;
close all;
clear all;
m=7;
c=10;
k=100;
dt=5
t=0:dt:10
A=[(-c/m) (-k/m);1 0 ];
B=[9.81 ; 0];
Any suggestion for completing the code?
Best regards,
Marco
4 Comments
darova
on 31 Mar 2020
I know how to solve this using ode45 (you can get x and 
mx''+bx'+kx=u(t)
But don't know what is this for
ydot = [(-c/m)(-k/m);1 0] [x xdot] +[9.81;0]
Marco Boutrus
on 31 Mar 2020
darova
on 31 Mar 2020
as i understood you correctly
Marco Boutrus
on 31 Mar 2020
Edited: Marco Boutrus
on 31 Mar 2020
Answers (1)
Ameer Hamza
on 31 Mar 2020
Edited: Ameer Hamza
on 31 Mar 2020
Following use ode45 to solve your ODE
dt=5;
t= [0 10];
ic = [0;0]; % initial condition
[t,y] = ode45(@myOdeFcn, t, ic);
plot(t,y);
legend({'Velocity', 'Position'});
function dydt = myOdeFcn(t, y)
m=7;
c=10;
k=100;
A=[(-c/m) (-k/m);1 0 ];
B=[9.81; 0];
dydt = A*y(:)+B;
end

22 Comments
Marco Boutrus
on 31 Mar 2020
Edited: Marco Boutrus
on 31 Mar 2020
Ameer Hamza
on 31 Mar 2020
Marco, myOdeFcn is just the implementation of the ODE you wrote in your question. You can see it has the same equation as yours.
About step-size, I didn't understand your point. You you just what to make graph at 3 points t=[0 5 10]. That will appear a bit strange but if that is what you want then try
dt=5;
t= 0:dt:10; % <---- this line is changed
ic = [0;0]; % initial condition
[t,y] = ode45(@myOdeFcn, t, ic);
plot(t,y);
legend({'Velocity', 'Position'});
function dydt = myOdeFcn(t, y)
m=7;
c=10;
k=100;
A=[(-c/m) (-k/m);1 0 ];
B=[9.81; 0];
dydt = A*y(:)+B;
end
Marco Boutrus
on 31 Mar 2020
Ameer Hamza
on 31 Mar 2020
I saw that comment. I think that your professor what to write your own ode solver instead of using ode45. Probably he has taught a solver in his lectures, which he wants you to use. Without the formula of the solver given by the professor, it is difficult to suggest a solution.
Marco Boutrus
on 31 Mar 2020
Edited: Marco Boutrus
on 31 Mar 2020
Marco Boutrus
on 31 Mar 2020
Edited: Marco Boutrus
on 31 Mar 2020
Ameer Hamza
on 31 Mar 2020
Yes, you are correct. You will need to write the code for something like that. Since this is a homework question, you first need to write the code for the algorithm yourself, and we can help you if you face some issues, specifically related to MATLAB.
Marco Boutrus
on 31 Mar 2020
Ameer Hamza
on 31 Mar 2020
Few of the MATLAB's old ode solvers were based on Euler and Runge-Kutta. MATLAB have still made their code available. Download and study them. They will give you the general structure of the numerical solver. Adapt it according to your function.
Marco Boutrus
on 31 Mar 2020
Ameer Hamza
on 31 Mar 2020
This happens because you are using too large value of dt=0.5. The solver diverges. Use a small value say dt=0.1 and see the result.
Ameer Hamza
on 31 Mar 2020
Also, if you want to show both displacement and velocity then change the first plot line like this
plot(t',y','b--')
Marco Boutrus
on 31 Mar 2020
Edited: Marco Boutrus
on 31 Mar 2020
Ameer Hamza
on 1 Apr 2020
I guess the 0.5 second interval was required to demonstrate that the solver can diverge if dt is large. Also, when I plot with
plot(t',y','b--')
both x and v follow the initial conditions.
I am not sure about the B matrix, but I guess you can replace A*y with A*y+B to solve the omplete equation.
Since you are just using A*y so this is the natural response of the system. Therefore, If the initial position of the system is x=0, then you don't see any oscillations.
Marco Boutrus
on 1 Apr 2020
Ameer Hamza
on 1 Apr 2020
The eigenvalue part is not useful for the numerical solution. The first part should be enough for your assignment. Matrix B can be included like this
for i=1:length(t)-1
y_star = y(:,i) + (A*y(:,i)+B)*dt; % Predictor
y(:,i+1) = y(:,i) + 0.5*(A*y(:,i) + B + A*y_star + B)*dt; % Corrector
end
Also note that due to the way matrix A is created, the first variable is velocity, and the second variable is the position. Take care of this when interpreting the variables
x0 = 0; % Initial displacement [m]
v0 = 1; % Initial velocity x' [m/s]
y(:,1) = [v0; x0]; % Initial conditions, first column of solution vector
Marco Boutrus
on 1 Apr 2020
Edited: Marco Boutrus
on 1 Apr 2020
Ameer Hamza
on 1 Apr 2020
Marco, when dt=0.5, the graph does not start from the right. The graph is actually divergeing. It means that it will continue to increase to infinity. You can increase the end time t=[0 100] and see the y-axis. It will show that the solution is diverging.
Marco Boutrus
on 1 Apr 2020
Ameer Hamza
on 1 Apr 2020
I am not sure what you meant by the graph not starting from zero.
The graph is not exactly same for 0.05 and 0.1. But the difference is too minor to be observed with eyes. You can check the number of elements of t and y to see the difference for both cases.
Marco Boutrus
on 1 Apr 2020
Ameer Hamza
on 1 Apr 2020
This is nothing strange, and it is a well-known fact. All numerical method suffers from this to some extent. Note that Euler is one of the basic numerical methods, so it will be affected the most. See the numerical stability section here: https://en.wikipedia.org/wiki/Euler_method#Numerical_stability
Categories
Find more on Mathematics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!