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

조회 수: 2 (최근 30일)
tyler hollings
tyler hollings 2020년 10월 2일
답변: Priysha LNU 2020년 10월 5일
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
Walter Roberson
Walter Roberson 2020년 10월 2일
subplot() or figure() or use tiledlayout()
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에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by