简体   繁体   English

在Matlab上绘制后,updatedline不更新

[英]Updatedline not updating after drawnow on Matlab

I have a code for showing the animation of a pendulum that works fine. 我有一个代码来显示正常工作的摆的动画。

Now i want to also plot the pendulums position simultaneously. 现在我想同时绘制摆的位置。 But it isnt working. 但这是行不通的。

Here is my code: 这是我的代码:

% Program 6.3 Animation program for pendulum using IVP solver
% Inputs: int = [a b] time interval,
%  initial values ic = [y(1,1) y(1,2)], h = step size
% Calls a one-step method such as trapstep.m
% Example usage: pend([0 10],[pi/2 0],.05)
function pend2(int,ic,h,l)
n=round((int(2)-int(1))/h); % plot n points in total
y(1,:)=ic;                % enter initial conds in y
t(1)=int(1);
clf;                      % clear screen
subplot(2,1,1);
x(1)=1
axis([0 200 -2 2])
set(gca,'xlim',[-2.2 2.2],'ylim',[-2.2 2.2], ...
  'xtick',[-2 -1 0 1 2],'ytick',[-2 -1 0 1 2], ...
  'drawmode','fast','Visible','on','NextPlot','add');
plot(0,0,'ks');           % pivot where rod attached
axis square               % make aspect ratio 1 - 1
bob = line('color','r','Marker','.','markersize',40,...
    'erase','xor','xdata',[],'ydata',[]);
rod = line('color','b','LineStyle','-','LineWidth',3,...
    'erase','xor','xdata',[],'ydata',[]);
for k=1:n
  x(1,k+1)=x(1,k)+1;
  t(k+1)=t(k)+h;
  y(k+1,:)=trapstep(t(k),y(k,:),h,l);
  xbob = l*cos(y(k+1,1)-pi/2); ybob = l*sin(y(k+1,1)-pi/2);
  xrod = [0 xbob]; yrod = [0 ybob];
  set(rod,'xdata',xrod,'ydata',yrod)
  set(bob,'xdata',xbob,'ydata',ybob)
  subplot(2,1,2);
  addpoints(animatedline,x(1,k),y(k,1));
  drawnow;
  pause(h)
end

function y = trapstep(t,x,h,l)
%one step of the Trapezoid Method
z1=ydot(t,x,l);
g=x+h*z1;
z2=ydot(t+h,g,l);
y=x+h*(z1+z2)/2;

function z=ydot(t,y,l)
g=9.81;m=10;c=4.5;
z(1) = y(2);
z(2) = -(g/l)*sin(y(1)) -(c/m)*y(2);

Could anyone help me out? 有人可以帮我吗?

Thanks! 谢谢!

The problem is this line: 问题是这一行:

addpoints(animatedline,x(1,k),y(k,1));

It creates a new animatedline everytime it is called. 每次调用时都会创建一个新的动画行。

Enter the following before the for-loop 在for循环之前输入以下内容

ax = subplot(2,1,2);
hdl = animatedline;
ax.XLim = [0 200];
ax.YLim = [-2 2];

and change the line mentioned above to 并将上述行更改为

addpoints(hdl,x(1,k),y(k,1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM