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

Star Strider
Star Strider 2016년 12월 4일
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
omar El Olimi 2016년 12월 4일
First script: >> MSD_numerical_equation Index exceeds matrix dimensions.
Error in MSD_numerical_equation (line 21) dX(1,1)=X(2,1);
Second script: >> MSD_ode45 Index exceeds matrix dimensions.
Error in MSD_numerical_equation (line 21) dX(1,1)=X(2,1);
Error in MSD_ode45>@(t,X)MSD_numerical_equation(t,X,m,b,k) (line 20) [t,x]=ode45(@(t,X)MSD_numerical_equation(t,X,m,b,k),td,[x0;v0],options);
Error in odearguments (line 87) f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in MSD_ode45 (line 20) [t,x]=ode45(@(t,X)MSD_numerical_equation(t,X,m,b,k),td,[x0;v0],options);
Thank you for helping:)
omar El Olimi
omar El Olimi 2016년 12월 4일
It may also be because of the t and X because i didnt understand what they were for so i just made them as 0. It was stated before that they were:
  1. t: current time.
  2. X: matrix for state variables.
  3. The first column : the particle position.
  4. The second column : the particle velocity.
  5. m,b,k: parameters for the system.
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
omar El Olimi 2016년 12월 4일
Thank you Tamir for replying, I think this is used because the function in the symbolic script is used in other scripts as well later on, which will make it easier later on. I am having errors on both functions though, do you know why this is coming up?
Tamir Suliman
Tamir Suliman 2016년 12월 5일
편집: Tamir Suliman 2016년 12월 5일
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

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Graphics에 대해 자세히 알아보기

태그

질문:

2016년 12월 4일

편집:

2016년 12월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by