【问题标题】:How to plot pendulum motion for Elastic Pendulum with Spring in Matlab如何在 Matlab 中绘制带弹簧的弹性摆的摆运动
【发布时间】:2017-05-15 14:22:21
【问题描述】:

我有一个代码可以为带弹簧的弹性摆创建正确的 xy 图。我想在 xy 图上显示弹性弹簧摆的动画,因为系统会及时前进。如何做到这一点?

这是我的模拟代码:

clc
clear all;

%Define parameters

global M K L g;
M = 1;
K = 25.6;
L = 1;
g = 9.8;


% define initial values for theta, thetad, del, deld
theta_0 = 0;
thetad_0 = .5;
del_0 = 1;
deld_0 = 0;
initialValues = [theta_0, thetad_0, del_0, deld_0];

% Set a timespan
t_initial = 0;
t_final = 36;
dt = .1;
N = (t_final - t_initial)/dt;
timeSpan = linspace(t_final, t_initial, N);

% Run ode45 to get z (theta, thetad, del, deld)
[t, z] = ode45(@OdeFunHndlSpngPdlmSym, timeSpan, initialValues);

% initialize empty column vectors for theta, thetad, del, deld
M_loop = zeros(N, 1);
L_loop = zeros(N, 1);
theta = zeros(N, 1);
thetad = zeros(N, 1);
del = zeros(N, 1);
deld = zeros(N, 1);
T = zeros(N, 1);
x = zeros(N, 1);
y = zeros(N, 1);

% Assign values for variables (theta, thetad, del, deld)
for i = 1:N
    M_loop(i) = M;
    L_loop(i) = L;
    theta(i) = z(i, 1);
    thetad(i) = z(i, 2);
    del(i) = z(i, 3);
    deld(i) = z(i, 4);
    T(i) = (M*(thetad(i)^2*(L + del(i))^2 + deld(i)^2))/2;
    V(i) = (K*del(i)^2)/2 + M*g*(L - cos(theta(i))*(L + del(i)));
    E(i) = T(i) + V(i);
    x(i) = (L + del(i))*sin(theta(i));
    y(i) = -(L + del(i))*cos(theta(i));
end

figure(1)
plot(x, y,'r');
title('XY Plot');
xlabel('x position');
ylabel('y position');

这是我的功能代码:

function dz = OdeFunHndlSpngPdlmSym(~, z)
% Define Global Parameters
global M K L g


% Take output from SymDevFElSpringPdlm.m file for fy1 and fy2 and
% substitute into z2 and z4 respectively
%fy1=thetadd=z(2)= -(M*g*sin(z1)*(L + z3) + M*z2*z4*(2*L + 2*z3))/(M*(L + z3)^2)
%fy2=deldd=z(4)=((M*(2*L + 2*z3)*z2^2)/2 - K*z3 + M*g*cos(z1))/M

% return column vector [thetad; thetadd; deld; deldd]
dz = [z(2);
    -(M*g*sin(z(1))*(L + z(3)) + M*z(2)*z(4)*(2*L + 2*z(3)))/(M*(L + z(3))^2);
    z(4);
    ((M*(2*L + 2*z(3))*z(2)^2)/2 - K*z(3) + M*g*cos(z(1)))/M];

【问题讨论】:

  • 谢谢我运行了添加的代码,但图表不断放大到矢量末尾的点。我想保持 xy 图形的范围不变,但有钟摆以便可视化钟摆运动。

标签: matlab animation plot


【解决方案1】:

您可以通过不断更新的 FOR 循环和将图形对象分配给变量来“模拟”绘图中的动画。

类似的东西(我假设只使用 x,y 作为时间数组 t 的数组函数)

%In order to block the axis and preventing continuous zoom, choose proper axes limit
x_lim = 100; %these values depends on you, these are examples 
y_lim = 100;
axis equal
axis([-x_lim x_lim -y_lim y_lim]); %now x and y axes are fixed from -100 to 100
ax = gca;


for i=1:length(t)
 if i > 1
  delete(P);
  delete(L);
 end

 P = plot(x(i),y(i)); %draw the point
 hold on
 L = quiver(0,0,x(i),y(i)); %draw a vector from 0,0 to the point
 hold on
 %other drawings
 drawnow
 pause(0.1) %pause in seconds needed to simulate animaton. 
end

在每条绘图指令之后都会发出“等待”指令。 当然,这只是一个基本的动画。

【讨论】:

  • 谢谢我运行了添加的代码,但图表不断放大到矢量末尾的点。我想保持 xy 图形的范围不变,但有钟摆以便可视化钟摆运动。
  • @PatStarks 我更新了答案。如果它再次不起作用(图表仍然不存在),请尽量不要使用“drawnow”,而在与绘图指令相关的每一行代码之后使用“hold on”(摆锤的每个组件至少有 1 条指令)。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多