trying to make a graph of each iteration in my while loop

3 次查看(过去 30 天)
i use a pause(0.5) to get a graph out which works however once i pause it pulls me out of the while loop
function [c]= bisection(a,A,B,varargin)
nvarargs =length(varargin);
if nvarargs>3
error('requires at most 3 optional inputs');
end
%set defaults for optional inputs
optargs = {0.000001 50 0};
optargs(1:nvarargs) = varargin;
[tol,maxiter,plt] = optargs{:};
if a(A)*a(B)>0
error('must choose new bracket')
end
i=0;
while i~=maxiter
i=i+1
c=(A+B)/2
if i>1
ea=abs(c-X);
if abs(a(c))<=tol | ea<= tol
i=maxiter;
elseif i==maxiter & (abs(a(c))>tol | ea >tol)
error('root within given tolerance not found within maxiterations')
end
end
% if i>1 & plt==1
% pause(0.5)
% x_ax=[A,B,c]
% Y_ax=[a(A),a(B),a(c)]
% plot(x_ax,Y_ax)
%
% end
if a(A)*a(c)<0
B=c;
else
A=c;
end
X=c;
a(c)
end
end
i commented the relevant bits of the function
  6 个评论
per isakson
per isakson 2020-10-2
... and replacing
plot(x_ax,Y_ax) % plots to the current axes, i.e. overwrites
by
figure, plot(axes,x_ax,Y_ax) % creates a new figure for every plot
will give you a bunch of figures, each with one diagram

请先登录,再进行评论。

回答(1 个)

Priysha LNU
Priysha LNU 2020-10-5
You may use saveas function in MATLAB to save plots in each iteration of the loop.
Example :
for k=1:5
h=figure
plot(...)
saveas(h,sprintf('FIG%d.png',k)); % will create FIG1, FIG2,...
end
DISCLAIMER: These are my own views and in no way depict those of MathWorks.

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by