second order differencial equations system
3 views (last 30 days)
Show older comments
Hello,
I am trying to solve a system of 2 second order differencial equations below :
J1*x'' + k(x-y)=-Cp
J2*y''+k(y-x)=Cm
where J1, J2, k, cp, cm constants.
For the moment, I have this on my torsion.m :
function [x,y]=torsion(t,x,y)
global Jf Jt K Cm Cp
J1=1;
J2=1;
K=1;
Cm=1;
Cp=1;
dx(1)=x(2);
dx(2)=-Cp/J1-(K/J1)*(x(1)-y(1));
dy(1)=y(2);
dy(2)=Cm/J2-(K/J2)*(y(1)-x(1));
end
then, what can I do ? I have tried to do this :
>> [t,x,y]=ode45(@torsion,[0 10],[0;1000],[0;1000]);
considering :
- tspan : 0 to 10s
- x(0) = 0
- y(0) = 0
- x'(0) = 1000
- y'(0) = 1000
I think there is someting wrong with my vector architecture. thanks for your future recommendations!!
Kevin
2 Comments
Harsh Karve
on 2 Feb 2015
Can you give more detail on why you think it is wrong? Does MATLAB throw an error? Does the output not look like what you expected?
Harsh
Answers (3)
John D'Errico
on 2 Feb 2015
Edited: John D'Errico
on 2 Feb 2015
Um, first of all, you compute vectors called dx and dy, but then never do anything with them, including return them as outputs from the function. Should ode45 know what you intend?
Next, you have two second order ODEs, each of which is converted to a pair of first order ODEs. So you have a system of 4 ODEs for ODE45 to solve. You must return a vector of length 4 for that to happen. I don't know why you have your function returning x and y instead. Ok, I think perhaps YOU don't know why either.
I might also point out that your use of global variables there is a crutch, and not at all necessary. They can easily be passed in via an anonymous function, so just make them additional arguments to the function torsion.
function [x,y]=torsion(t,x,y,Jf,Jt,K,Cm,Cp)
...
No need for globals there. Now, when you call ode45, do it as:
[t,x,y]=ode45(@(t,x,y) torsion(t,x,y,Jf,Jt,K,Cm,Cp),[0 10],[0;1000],[0;1000]);
The current values for those constants are taken from your workspace.
2 Comments
K L
on 3 Feb 2015
2 Comments
Torsten
on 4 Feb 2015
Why making life so difficult ?
Define
z(1)=x, z(2)=x', z(3)=y, z(4)=y',
call ODE45 as
[T,Z]=ode45(@(t,z)torsion(t,z,Jf,Jt,K,Cm,Cp),[0 10],[0 1000 0 1000]);
and define
function dzdt=torsion(t,z)
global Jf Jt K Cm Cp
J1=1;
J2=1;
K=1;
Cm=1;
Cp=1;
dzdt=zeros(4,1);
dzdt(1)=z(2);
dzdt(2)=-Cp/J1-(K/J1)*(z(1)-z(3));
dzdt(3)=z(4);
dzdt(4)=Cm/J2-(K/J2)*(z(3)-z(1));
end
Best wishes
Torsten.
Michael Haderlein
on 4 Feb 2015
Edited: Michael Haderlein
on 4 Feb 2015
I agree with your definition of z, but the function header must contain (at least) as many input arguments as you use. In your example, you only have two input arguments (t,z) but you use 7 (t,z,Jf,Jt,K,Cm,Cp). Thus, the header must be
function dzdt=torsion(t,z,Jf,Jt,K,Cm,Cp)
Please note that Jf and J1 and Jt and J2 are a bit mixed up, this needs to be cleared that in the final program.
Still, I see no need for the globals here. All the constants (J*, K, C*) will be known without the global attribute as John as mentioned.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!