# ode45 2 degree of freedom

6 views (last 30 days)
David Botha on 12 Sep 2019
Answered: darova on 12 Sep 2019
Hi all.
trying to solve a 2 degree of freedom system but not getting the expected output.
Am I making a fundemental error?
syms k1 k2 m1 m2 x1(t) x2(t)
Eqs = [m1*diff(x1(t),2) == -x1(t)*(k1)-x1(t)*(k2)+k2*x2(t) , ...
m2*diff(x2(t),2) == k1*x1(t)-k2*(x2(t)) ]
SymSys = odeToVectorField(Eqs)
F = matlabFunction(SymSys)
F = @(t,Y,k1,k2,m1,m2) [Y(2);...
-(k2.*Y(1)-k1.*Y(3))./m2;...
Y(4);...
-(-k2.*Y(1)+k1.*Y(3)+k2.*Y(3))./m1];
k1=24;
k2=3;
m1=9;
m2=1;
tspan=[0 20];
Y0=[0 0 1 0];
[t,y] = ode45( @(t,Y) F(t,Y,k1,k2,m1,m2), tspan, Y0);
figure(1)
plot(t, y)
grid

darova on 12 Sep 2019
Take a closer look on example from MATLAB help
I suggest you to comment these lines (or remove) since you are using odeToVectorField
% F = @(t,Y,k1,k2,m1,m2) [Y(2);...
% -(k2.*Y(1)-k1.*Y(3))./m2;...
% Y(4);...
% -(-k2.*Y(1)+k1.*Y(3)+k2.*Y(3))./m1];
Also define your constants BEFORE equations
Use something like that to define or add variables from your ode function
syms t Y
F = matlabFunction(SymSys,'vars',[t Y])