How to stop the axes from changing within loop without defining them every iteration
Show older comments
I would like the decrease the running time of my code, setting the axes takes up a lot of time. How do a fix the axes, having then within the given limits and being equal without having to define them in every iteration of the for loop.
clc,clear variables, close all
h = 0.01;
t = 0:h:2; % Time
% Initial conditons
y0 = 2;
x0 = 5;
dydt0 = 5;
dxdt0 = 7;
Y0 = [y0 x0 dydt0 dxdt0];
% Solve system
Y = RK4_ODEs(@dYdt_ball,Y0,t);
Ysol = Y(:,1);
Xsol = Y(:,2);
% Motion plot
hf = figure(1);
for i = 1:length(Y)
plot(Xsol(i),Ysol(i),'r.',MarkerSize=30)
hold on
rectangle('Position',[-0.1 -0.1 10.2 4.9])
ht = text(4.4,5,'',Interpreter='latex');
ht.String = ['t = ' num2str(t(i),5) '\ s'];
hold off
axis equal % Slow
axis([-1 11 -1 6]) % Slow
title('Motion of Ball in Box')
pause(0.001)
end
Thank you
Accepted Answer
More Answers (1)
David Hill
on 17 Mar 2022
Why not just move it outside the loop?
hf = figure(1);
hold on
rectangle('Position',[-0.1 -0.1 10.2 4.9])
for i = 1:length(Y)
plot(Xsol(i),Ysol(i),'r.',MarkerSize=30)
ht = text(4.4,5,'',Interpreter='latex');
ht.String = ['t = ' num2str(t(i),5) '\ s'];
end
axis equal
axis([-1 11 -1 6])
title('Motion of Ball in Box')
1 Comment
Magnus Sejer Lind
on 17 Mar 2022
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!