Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Help with ODE Problem

Subject: Help with ODE Problem

From: Lauara

Date: 12 Jul, 2013 16:02:07

Message: 1 of 8

Hi everyone,

I am trying to solve the Initial value problem ordinary differential equation,
Mx’’+Cx’+Kx=P(t)
Where M, C and K are nxn symmetric matrices. Differentiation is wrt time and P is an nx1 vector.
The initial conditions are x=zeros(n,1) and x’=zeros(n,1). The equations are coupled. I know that since I have higher order ODE I have to first convert it to first order differential equation but dealing with implicit, higher order, multiple equations (vectors in ODE solver) makes it hard to form the equations using some solvers like ODE15i. Could anyone help me with writing the function and calling the solver?

your help would be greatly appreciated

Subject: Help with ODE Problem

From: Jan Simon

Date: 15 Jul, 2013 12:31:11

Message: 2 of 8

Dear Lauara,

Follow the standard approach directly:

> Mx’’+Cx’+Kx=P(t)
> x'' = inv(M) * (P(t) - Cx' -Kx)

Lookup table: y1 = x, y2 = x', y3 = x''

  dydt(1) = y(2);
  dydt(2) = (P(t) - C * y(2) - K * y(1)) \ M;

Or you could expand M to work with the vector y.

Kind regards, Jan

Subject: Help with ODE Problem

From: Lauara

Date: 15 Jul, 2013 15:12:21

Message: 3 of 8

"Jan Simon" wrote in message <ks0q2f$rla$1@newscl01ah.mathworks.com>...
> Dear Lauara,
>
> Follow the standard approach directly:
>
> > Mx’’+Cx’+Kx=P(t)
> > x'' = inv(M) * (P(t) - Cx' -Kx)
>
> Lookup table: y1 = x, y2 = x', y3 = x''
>
> dydt(1) = y(2);
> dydt(2) = (P(t) - C * y(2) - K * y(1)) \ M;
>
> Or you could expand M to work with the vector y.
>
> Kind regards, Jan

Thank you so much Jan for your response. I have written the code as you said:

tspan=[0 0.25 0.5];
y0=zeros(76,1);
yp0=zeros(76,1);
[T,Y]=ode15i(@(t,y) Differential_Equation_Solver(t,y,K,M,C,P),tspan,y0,yp0);

%%%%%%%

function dydt=Differential_Equation_Solver(t,y,K,M,C,P)
dydt(1) = y(2);
dydt(2) = M\(P-C*y(2)-K*y(1));
end

But I am still getting an error for the number of the input arguments:

Too many input arguments.

Error in odearguments (line 88)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.

Error in ode15i (line 117)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, ~, odeFcn, ...

Error in Bridge_Displacement (line 49)
[T,Y]=ode15i(@(t,y) Differential_Equation_Solver(t,y,K,M,C,P),tspan,y0,yp0);

why do you think this format is wrong and how should I fix it?

Again, Thank you very much for your useful tip.

Subject: Help with ODE Problem

From: Marc

Date: 16 Jul, 2013 02:38:06

Message: 4 of 8

"Lauara " <osornol@fiu.edu> wrote in message <ks13gl$pmk$1@newscl01ah.mathworks.com>...
> "Jan Simon" wrote in message <ks0q2f$rla$1@newscl01ah.mathworks.com>...
> > Dear Lauara,
> >
> > Follow the standard approach directly:
> >
> > > Mx’’+Cx’+Kx=P(t)
> > > x'' = inv(M) * (P(t) - Cx' -Kx)
> >
> > Lookup table: y1 = x, y2 = x', y3 = x''
> >
> > dydt(1) = y(2);
> > dydt(2) = (P(t) - C * y(2) - K * y(1)) \ M;
> >
> > Or you could expand M to work with the vector y.
> >
> > Kind regards, Jan
>
> Thank you so much Jan for your response. I have written the code as you said:
>
> tspan=[0 0.25 0.5];
> y0=zeros(76,1);
> yp0=zeros(76,1);
> [T,Y]=ode15i(@(t,y) Differential_Equation_Solver(t,y,K,M,C,P),tspan,y0,yp0);
>
> %%%%%%%
>
> function dydt=Differential_Equation_Solver(t,y,K,M,C,P)
> dydt(1) = y(2);
> dydt(2) = M\(P-C*y(2)-K*y(1));
> end
>
> But I am still getting an error for the number of the input arguments:
>
> Too many input arguments.
>
> Error in odearguments (line 88)
> f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
>
> Error in ode15i (line 117)
> [neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, ~, odeFcn, ...
>
> Error in Bridge_Displacement (line 49)
> [T,Y]=ode15i(@(t,y) Differential_Equation_Solver(t,y,K,M,C,P),tspan,y0,yp0);
>
> why do you think this format is wrong and how should I fix it?
>
> Again, Thank you very much for your useful tip.

You can not pass the K,M,C,P variables into the solver. See "nested functions" or the documentation on "passing other variables into functions" with the Matlab ODE solvers.

Also, make sure your ode function is spitting on a column vector.

Using the call to dydt = zeros(2,1); before the equations will fix that.

So, your function needs to look like this.....

function dydt = yourODE(t,y)

K = ....
M = ....
etc. etc.

dydt = zeros(2,1);
etc.etc.

end

Try hard coding the variables first, then look into using a nested function.

Subject: Help with ODE Problem

From: Torsten

Date: 16 Jul, 2013 06:39:10

Message: 5 of 8

"Lauara " <osornol@fiu.edu> wrote in message <ks13gl$pmk$1@newscl01ah.mathworks.com>...
> "Jan Simon" wrote in message <ks0q2f$rla$1@newscl01ah.mathworks.com>...
> > Dear Lauara,
> >
> > Follow the standard approach directly:
> >
> > > Mx’’+Cx’+Kx=P(t)
> > > x'' = inv(M) * (P(t) - Cx' -Kx)
> >
> > Lookup table: y1 = x, y2 = x', y3 = x''
> >
> > dydt(1) = y(2);
> > dydt(2) = (P(t) - C * y(2) - K * y(1)) \ M;
> >
> > Or you could expand M to work with the vector y.
> >
> > Kind regards, Jan
>
> Thank you so much Jan for your response. I have written the code as you said:
>
> tspan=[0 0.25 0.5];
> y0=zeros(76,1);
> yp0=zeros(76,1);
> [T,Y]=ode15i(@(t,y) Differential_Equation_Solver(t,y,K,M,C,P),tspan,y0,yp0);
>
> %%%%%%%
>
> function dydt=Differential_Equation_Solver(t,y,K,M,C,P)
> dydt(1) = y(2);
> dydt(2) = M\(P-C*y(2)-K*y(1));
> end
>
> But I am still getting an error for the number of the input arguments:
>
> Too many input arguments.
>
> Error in odearguments (line 88)
> f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
>
> Error in ode15i (line 117)
> [neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, ~, odeFcn, ...
>
> Error in Bridge_Displacement (line 49)
> [T,Y]=ode15i(@(t,y) Differential_Equation_Solver(t,y,K,M,C,P),tspan,y0,yp0);
>
> why do you think this format is wrong and how should I fix it?
>
> Again, Thank you very much for your useful tip.

tspan=[0 0.25 0.5];
y0=zeros(152,1);
[T,Y]=ode15s(@(t,y) Differential_Equation_Solver(t,y,K,M,C,P),tspan,y0);
>
> %%%%%%%
>
function dydt=Differential_Equation_Solver(t,y,K,M,C,P)
dydt=zeros(152,1);
dydt(1:76,1) = y(77:152,1);
dydt(77:152,1) = M\(P-C*y(77:152,1)-K*y(1:76,1));
end

Best wishes
Torsten.

Subject: Help with ODE Problem

From: Jan Simon

Date: 21 Jul, 2013 21:29:16

Message: 6 of 8

Dear Marc,

> > [T,Y]=ode15i(@(t,y) Differential_Equation_Solver(t,y,K,M,C,P),tspan,y0,yp0);
> >
> > function dydt=Differential_Equation_Solver(t,y,K,M,C,P)
> > dydt(1) = y(2);
> > dydt(2) = M\(P-C*y(2)-K*y(1));
> > end
> >

> You can not pass the K,M,C,P variables into the solver. See "nested functions" or the documentation on "passing other variables into functions" with the Matlab ODE solvers.

It is valid and recommended to pass constants by using anonymous functions, see:
  http://www.mathworks.com/matlabcentral/answers/1971

The problem is the yp0. This is another method to pass constants to the function to be integrated. But the function Differential_Equation_Solver does not accept further parameters in the inputs. So omitting yp0 as input to ODE45 should solve the problem, as mentioned above.

Kind regards, Jan

Subject: Help with ODE Problem

From: Torsten

Date: 22 Jul, 2013 06:58:10

Message: 7 of 8

"Jan Simon" wrote in message <kshjrc$b7g$1@newscl01ah.mathworks.com>...
> Dear Marc,
>
> > > [T,Y]=ode15i(@(t,y) Differential_Equation_Solver(t,y,K,M,C,P),tspan,y0,yp0);
> > >
> > > function dydt=Differential_Equation_Solver(t,y,K,M,C,P)
> > > dydt(1) = y(2);
> > > dydt(2) = M\(P-C*y(2)-K*y(1));
> > > end
> > >
>
> > You can not pass the K,M,C,P variables into the solver. See "nested functions" or the documentation on "passing other variables into functions" with the Matlab ODE solvers.
>
> It is valid and recommended to pass constants by using anonymous functions, see:
> http://www.mathworks.com/matlabcentral/answers/1971
>
> The problem is the yp0. This is another method to pass constants to the function to be integrated. But the function Differential_Equation_Solver does not accept further parameters in the inputs. So omitting yp0 as input to ODE45 should solve the problem, as mentioned above.
>
> Kind regards, Jan

Two further errors in the code:
1. The OP tries to solve 152 ODEs, not only 2 as implied by the function Differential_Equation_Solver.
2. For ODE15i, you will have to supply residuals, not time derivatives.

Best wishes
Torsten.

Subject: Help with ODE Problem

From: Marc

Date: 23 Jul, 2013 04:35:10

Message: 8 of 8

"Torsten" wrote in message <ksil62$k9p$1@newscl01ah.mathworks.com>...
> "Jan Simon" wrote in message <kshjrc$b7g$1@newscl01ah.mathworks.com>...
> > Dear Marc,
> >
> > > > [T,Y]=ode15i(@(t,y) Differential_Equation_Solver(t,y,K,M,C,P),tspan,y0,yp0);
> > > >
> > > > function dydt=Differential_Equation_Solver(t,y,K,M,C,P)
> > > > dydt(1) = y(2);
> > > > dydt(2) = M\(P-C*y(2)-K*y(1));
> > > > end
> > > >
> >
> > > You can not pass the K,M,C,P variables into the solver. See "nested functions" or the documentation on "passing other variables into functions" with the Matlab ODE solvers.
> >
> > It is valid and recommended to pass constants by using anonymous functions, see:
> > http://www.mathworks.com/matlabcentral/answers/1971
> >
> > The problem is the yp0. This is another method to pass constants to the function to be integrated. But the function Differential_Equation_Solver does not accept further parameters in the inputs. So omitting yp0 as input to ODE45 should solve the problem, as mentioned above.
> >
> > Kind regards, Jan
>
> Two further errors in the code:
> 1. The OP tries to solve 152 ODEs, not only 2 as implied by the function Differential_Equation_Solver.
> 2. For ODE15i, you will have to supply residuals, not time derivatives.
>
> Best wishes
> Torsten.

Yeah, this threw me off. I missed the anonymous function call.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us