フィルターのクリア

I'm trying to animate both the plots in the 2 for loops simultaneously. But it's getting executed one after the other. Can anyone help me resolve this. I need to get this done. I'm attaching images of plots for your undertanding.

1 回表示 (過去 30 日間)
clear
close all
clc
%========================Given Inputs ==============================
b = 0.1;
g = 9.81;
l = 1;
m = 1;
%==================== Initial Condition =======================
theta_0 = [0,3];
%==================== Time Points =============================
t_span = linspace(0,20,500);
%==================== Solve Ode ===============================
[t,results] = ode45(@(t,theta) ode_func(theta,b,g,l,m),t_span,theta_0);
%============ plotting pendulum motion w.r.t time =============
figure(2)
a = results(:,1);
b = results(:,2);
for j = 1:length(t)
subplot(2,1,1)
plot(t(1:j),a(1:j),'b','linewidth',2);
hold on
plot(t(1:j),b(1:j),'g','linewidth',2);
axis([0 20 -4 4]);
xlabel('Time')
ylabel('Plot')
title('Plot of Angular Velocity & Displacement w.r.t Time')
legend({'Displacement','Angular Velocity'})
pause(0.1);
hold off
end
%=================== Using For loop ===========================
figure(2)
hold on
ct = 1;
for i = 1:length(results(:,1))
subplot(2,1,2)
x0 = 0; %initial 'x' co-ordinate of pendulum string
y0 = 0; %initial 'y' co-ordinate of pendulum string
x1 = 1*sin(results(i,1)); % 'x' co-ordinate where the bob is attached
y1 = -1*cos(results(i,1)); % 'y' co-ordinate where the bob is attached
plot([0 0],[-1.5 0.5]);
hold on
plot([-1,1],[0,0],'linewidth',3,'color','b');
line([x0 x1],[y0 y1],'linewidth',3,'color','g'); %pendulum string plot
hold on
plot(x1,y1,'o','markers',20,'markerfacecolor','r'); %plotting bob of the pendulum
axis([-1.5 1.5 -1.5 0.5]);
pause(0.9);
hold off
M(ct) = getframe(gcf);
ct = ct+1;
end
%=================== Syntax for Movie ==========================
movie(M)
videofile = VideoWriter('Pendulum_oscillation.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

回答 (1 件)

Cris LaPierre
Cris LaPierre 2021 年 3 月 15 日
編集済み: Cris LaPierre 2021 年 3 月 15 日
Code executes top down. If you want the plots to update together, you'll have to put the code inside the same for loop.
Don't put code that only needs to be run once inside the for loops. Better is to create the original plot, and then update the XData and YData with a for loop.
  7 件のコメント
Abhinandan Angadi
Abhinandan Angadi 2021 年 3 月 15 日
The trace of the pendulum bob is overlapping, which is inappropriate. I' m not able to figure out why.
Cris LaPierre
Cris LaPierre 2021 年 3 月 15 日
Look at what code you are using to animate the pendulum. Are you adding more points, or are you updating the existing X and Y values?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by