How to graph an ODE45
Show older comments
I have two scripts one with a split second order differential equation into two one order differential equation and the other is meant to call it and solve a ode45 with that and show a graph but it doesnt seem to work im getting an error with the first order differential equation. Please tell me where i have gone wrong and how to fix it.
function dX=MSD_numerical_equation(t,X,m,b,k)
%
% We need to split the 2nd order differential equations for the
% mass-spring-damper in to 2 1st order differential equations so they are
% of a suitable form for Matlab to solve (e.g. with ODE45)
%
% we obtain the derivative for each of the variables
% (position and velocity of the particle)
%
t=0;
X=0;
% The first column : the particle position
% The second column : the particle velocity
% m,b,k: parameters for the system
m=10;
b=5;
k=160;
% Apply two 1st order differential equations
%dx(n+1)/dt<-v(n);
dX(1,1)=X(2,1);
%dv(n+1)/dt<-1/m*(F0sin(wt)-bv(n)-kx(n));
dX(2,1)=(1/m)*(-b*X(2,1)-k*X(1,1));
end
%
%Second script
%
%
function O=MSD_ode45(m,b,k,x0,v0,dt)
%
% Solver for Mass-Spring-Damper System with high order Runge-Kutta Method
%
% NO FORCING TERM INCLUDED
%
% ----- Input argument -----
m=10;
b=5;
k=160;
x0=1;
v0=0;
dt=0.02;
%
options=odeset('InitialStep',dt,'MaxStep',dt);
% set time span to be generated from Runge-Kutta solver
% from 0 sec to 20 sec with 0.02 sec time step
td=[0:dt:20];
% Solve differential equation with Runge-Kutta solver
[t,x]=ode45(@(t,X)MSD_numerical_equation(t,X,m,b,k),td,[x0;v0],options);
% Extract only particle position trajectory
O=[t x(:,1)];
plot(t,x(:,1))
hold on
xlabel('time (s)')
ylabel ('height (m)')
end
6 Comments
Star Strider
on 4 Dec 2016
What error is it throwing?
Please copy the entire error message — all the red text in your Command Window — and post it here.
omar El Olimi
on 4 Dec 2016
omar El Olimi
on 4 Dec 2016
Tamir Suliman
on 4 Dec 2016
I think this is all confusing what you trying to do you will have to format the question in a way that explains it well and explains what u trying to
please post the equation you modelling I realized tha you passing values through the function input variables like t X while you already have their values assisnged on the script any idea why u doing this ? your first function :
function dX=MSD_numerical_equation(t,X,m,b,k)
%
% We need to split the 2nd order differential equations for the
% mass-spring-damper in to 2 1st order differential equations so they are
% of a suitable form for Matlab to solve (e.g. with ODE45)
%
% we obtain the derivative for each of the variables
% (position and velocity of the particle)
%
t=0;
X=0;
% The first column : the particle position
% The second column : the particle velocity
% m,b,k: parameters for the system
m=10;
b=5;
k=160;
% Apply two 1st order differential equations
%dx(n+1)/dt<-v(n);
dX(1,1)=X(2,1);
%dv(n+1)/dt<-1/m*(F0sin(wt)-bv(n)-kx(n));
dX(2,1)=(1/m)*(-b*X(2,1)-k*X(1,1));
end
%
Your second Function
function O=MSD_ode45(m,b,k,x0,v0,dt)
%
% Solver for Mass-Spring-Damper System with high order Runge-Kutta Method
%
% NO FORCING TERM INCLUDED
%
% ----- Input argument -----
m=10;
b=5;
k=160;
x0=1;
v0=0;
dt=0.02;
%
options=odeset('InitialStep',dt,'MaxStep',dt);
% set time span to be generated from Runge-Kutta solver
% from 0 sec to 20 sec with 0.02 sec time step
td=[0:dt:20];
% Solve differential equation with Runge-Kutta solver
[t,x]=ode45(@(t,X)MSD_numerical_equation(t,X,m,b,k),td,[x0;v0],options);
% Extract only particle position trajectory
O=[t x(:,1)];
plot(t,x(:,1))
hold on
xlabel('time (s)')
ylabel ('height (m)')
end
What I m trying to under stand why do you have to go through multiple functions which adds more complexity instead of using one functions then pass those variables to
omar El Olimi
on 4 Dec 2016
Tamir Suliman
on 5 Dec 2016
Edited: Tamir Suliman
on 5 Dec 2016
you have X=0
then you have X(2,1) do you think this a right ? what / where is x(2,1)
you will need to fix the matrix if you have other values
Answers (0)
Categories
Find more on Graphics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!