How to pass values from one ode to another ode?

6 views (last 30 days)
I av to use the results of one ode(ode45) inside another ode... here is my code:
1st ode function
function dw=sd(t,w)
va=10;
vb=0;vc=0;
%MOMENT OF INERTIA IN PRINCIPAL AXIS
I1=27;
I2=17;
I3=25;
%DIFFERENTIAL EQUATIONS
dw(1,1)= ((I2-I3)/I1)*w(2)*w(3)+(va/I1);
dw(2,1)=((I3-I1)/I2)*w(1)*w(3)+(vb/I2);
dw(3,1)=((I1-I2)/I3)*w(1)*w(2)+(vc/I3);
function cal
[t,W]=ode45('sd',[tinit tfinal],[0;0;0]);
i av use the results of these in another ode function below
function dq=sk(t,q,w1,w2,w3)
dq(1,1)=0.5*(w3*q(2)-w2*q(3)+w1*q(4));
dq(2,1)=0.5*(-w3*q(1)+w1*q(3)+w2*q(4));
dq(3,1)=0.5*(w2*q(1)-w1*q(2)+w3*q(4));
dq(4,1)=0.5*(-w1*q(1)-w2*q(2)+w3*q(3));
end
function cal
[t,Q]=ode45('sk',[tinit tfinal],[0;1;0;0],W(:,1),W(:,2),W(:,3))
there s some error in passing as shown aabove
any suggestion pl?

Answers (2)

ChristianW
ChristianW on 10 Feb 2013
Write all equations in 1 function and make your state vector like this: 'x = [w;q]'. Tell me, if you need more details.

the cyclist
the cyclist on 10 Feb 2013
When you call ode45() the second time, with the function 'sk', you are calling it with 4 input arguments, but I don't think you've got the input structure correct.
If you run your code after first having done
>> dbstop if error
you will see that your W(:,1) vector is being interpreted by ode45() as the options input.

Tags

Community Treasure Hunt

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

Start Hunting!