second order differencial equations system

2 views (last 30 days)
K L
K L on 2 Feb 2015
Answered: K L on 16 Feb 2015
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
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
K L
K L on 3 Feb 2015
Hello, you can read the last post, I have given more information about it !

Sign in to comment.

Answers (3)

John D'Errico
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
Greg Heath
Greg Heath on 3 Feb 2015
Is this any help?
(x-y)'' + k3*(x-y) = C3
(J1*x + J2*y)'' = C4
K L
K L on 3 Feb 2015
Hello,
I can't see how it could be easier like this... can you ?
Thanks!

Sign in to comment.


K L
K L on 3 Feb 2015
Hello again,
John D'Errico : I have tried what you told me to do. Here is the new updated torsion.m. You can see below the error message generated when I'm calling it as you said :
AS you see, all my constant inputs are red underlined, the same for dx(2) and dy(2)
For more transparency, I repeat the equations I want to solve :
J1*x'' + k(x-y)=-Cp
J2*y''+k(y-x)=Cm
where J1, J2, k, cp, cm constants.
with the initial conditions :
tspan : 0 to 10s
x(0) = 0
y(0) = 0
x'(0) = 1000
y'(0) = 1000
  • Can I really introduce a vector [x,y] for function [x,y] ?
  • why dx(2) and dy(2) are red underlined ? (looked like unused by matlab)
Hope you will see what's wrong ! :)
  2 Comments
Torsten
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
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.

Sign in to comment.


K L
K L on 16 Feb 2015
Hello,
Thanks to Torsten and Michael Haderlein for your answers that solved my issue !
Kevin

Categories

Find more on Programming 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!