ode45 2 degree of freedom

4 次查看(过去 30 天)
David Botha
David Botha 2019-9-12
回答: darova 2019-9-12
Hi all.
trying to solve a 2 degree of freedom system but not getting the expected output.
Am I making a fundemental error?
Thanks in advance!
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

回答(1 个)

darova
darova 2019-9-12
Take a closer look on example from MATLAB help
211Untitled.png
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])

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by