how to plot this data as data-points?

조회 수: 1 (최근 30일)
Otto
Otto 2012년 11월 5일
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!
  댓글 수: 1
Otto
Otto 2012년 11월 6일
Anyone to solve this problem?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2012년 11월 6일
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
Otto
Otto 2012년 11월 6일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by