how to plot this data as data-points?

1 view (last 30 days)
Hi All,
I have written a code to estimate a root of x^10=1 using modified regula falsi. My code is working pretty well now but I want to plot ea versus iter variable and xr versus iter variable as data-points but there is a problem about this. In my workspace, iter,ea and xr appear only as their last values. I want to see their previous values too. Because of this I can't plot a data-point figure point by point.
Here is my code;
f=@(x)x^10-1;
xl=0;
xu=1.3;
xr = xu;
es=0.01;
fl=f(xl);
fu=f(xu);
iter=0;
iu = 0;
il = 0;
fprintf('\n\n%10s\t%10s\t%10s\t%10s\t%10s\t%10s\t\t%10s\t%10s\n',...
'iter','xl','xu','f(xl)','f(xu)','xr','f(xr)','ea')
fprintf([' ',repmat('-',1,92),'\n'])
while 1
xrold = xr;
xr = xu - fu*(xl-xu)/(fl-fu);
fr = f(xr);
iter=iter+1;
if xr<0
elseif xr>0
ea=abs((xr-xrold)/xr)*100;
end
test = fl*fr;
if test<0
xu = xr;
fu = f(xu);
iu = 0;
il = il+1;
if il>=2
fl=fl/2;
end
elseif test>0
xl=xr;
fl=f(xl);
il=0;
iu=iu+1;
if iu>=2
fu=fu/2;
end
else
ea=0;
end
if ea<es
break
end
fprintf('%10.2i\t%10.7f\t%10.7f\t%10.7f\t%10.7f\t%10.10f\t%10.7f\t%10.4f\t\n',...
iter,xl,xu,f(xl),f(xu),xr,f(xr),ea)
end
fprintf('\n\n')
ModFalsePos=xr;
What would you recommend for this situation?
I'll appreciate for any help.
Thanks Already!

Accepted Answer

Walter Roberson
Walter Roberson on 6 Nov 2012
In the loop, before the "end",
saved_iter(iter) = iter;
saved_ea(iter) = ea;
saved_xr(iter) = xr;
Then after the loop, you can plot saved_iter vs saved_ea and so on.
  1 Comment
Otto
Otto on 6 Nov 2012
Thanks for your help, I have a last question. With this code, I can see the iterations between number 01 and number 11 but I want to see the initial values,i.e 00'th iteration, and the final iteration, ie 12'th iteration, how can I do this? what would you recommend for that? I have only 1 hour to complete this project, so time is running out, I will appreciate for any help. Thank you again!

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!