我有两个二阶非线性微分方程,我需要解决这个问题,以便模拟和绘制悬挂在弹簧上的质量块的运动,该弹簧像钟摆一样摆动。他们在这里:
我正在使用odeToVectorField将它们重写为一阶线性ODE,然后我调用ode45来求解最终的方程组。但是当我绘制结果时,它给出了奇怪的答案,如负半径等。
任何人都可以帮我找到我弄乱代码的地方吗?非常感谢!
我的代码如下:
global m R g B k %declare global variables to use them everywhere
m=1.0; %mass of ball [kg]
k=200; % stiffness of the spring [N/m]
R=0.5; % unstretched length of the spring [m]
g=9.81; % acceleration due to the gravity [m/s^2]
B=0; % coefficient of air drag [kg/m]
syms r(t) f(t)
[V] = odeToVectorField(diff(r,2)== r*((diff(f,1))^2) + (g*cos(f))-(k*(r-R))-B*diff(r,1)*sqrt(diff(r,1)^2 +r^2*diff(f,1)^2),...
diff(f,2)== ((g*sin(f)+2*diff(r,1)*diff(f,1))/-r)-B*diff(f,1)*sqrt(diff(r,1)^2 +r^2*diff(f,1)^2));
F = matlabFunction(V,'vars',{'t','Y'});
%define initial conditions:
theta_0=(70*pi/180);
theta_dot_0=0;
r_0= R + (m*g*cos(theta_0))/k ;
r_dot_0=0;
t_start=0; %start time
t_step=.01; % time step
t_final=5; % final time
%Solve that system of ODEs from [V]
[t , X]=ode45(F, t_start:t_step:t_final , [r_0;r_dot_0;theta_0;theta_dot_0]);
figure(1)
subplot(2,1,1)
plot(t,X(:,1),'LineWidth',2)
xlabel('t');ylabel('r','fontsize',12);
hold on
subplot(2,1,2)
plot(t,X(:,2),'LineWidth',2)
xlabel('t');ylabel('r dot','fontsize',12);
hold on
figure(2)
subplot(2,1,1)
plot(t,X(:,3),'LineWidth',2)
xlabel('t');ylabel('theta [rad]','fontsize',12);
hold on
subplot(2,1,2)
plot(t,X(:,4),'LineWidth',2)
xlabel('t');ylabel('theta dot [rad/s]','fontsize',12);
hold on