How to generate double pendulum using ode 45
12 次查看(过去 30 天)
显示 更早的评论
function Math462hw2Q3parta
m1=1;
m2=1;
l1=1;
l2=1;
g=9.8;
tspan=0:1:100;
%theta1initial=pi/8;
%theta2initial=pi/8;
theta1=pi/8;
theta2=pi/8;
theta1dot=0;
theta2dot=0;
thetainitial=[theta1 theta2 theta1dot theta2dot];
%Write out the ode45 function
[t,thetasoln]=ode45(@(t,thetainitial)derivativefuncs(t,thetainitial,m1,m2,l1,l2,g),tspan,thetainitial);
%Plot function
plot(t,thetasoln,'bl');
xlabel('time');
ylabel('value');
title('double pendulum');
function dxdt=derivativefuncs(~,thetavalues,m1,m2,l1,l2,g)
theta1=thetavalues(1);
theta2=thetavalues(2);
theta1dot=thetavalues(3);
theta2dot=thetavalues(4);
theta1doubledot=(m2*l2*theta2doubledot*cos(theta1-theta2)+m2*l2*theta2dot^2*sin(theta1-theta2)+(m1+m2)*g*sin(theta1))/((m1+m2)*l);
theta2doubledot=l1*theta1doubledot*cos(theta1-theta2)-l1*theta1dot^2*sin(theta1-theta2)+g*sin(theta2);
dxdt=[theta1doubledot;theta2doubledot];
end
end
Just got another question on how to use ode 45 to generate the double pendulum model. I think the problem is Matlab keeps saying the line with ode45 function is still wrong but there is no marks in the code. I think I did pass the parameters but it still does not work. Can some one maybe help?
0 个评论
回答(1 个)
James Tursa
2021-2-23
编辑:James Tursa
2021-2-23
You have a 4-element state vector, so your derivative needs to be a 4-element state vector. E.g.,
dxdt=[theta1dot;theta2dot;theta1doubledot;theta2doubledot];
Then you will need to pick off the appropriate columns of thetasoln to plot.
3 个评论
James Tursa
2021-2-23
"not right" doesn't tell us much. Can you copy & post the entire error message including the offending line?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!