필터 지우기
필터 지우기

Presenting this results in a tabulated form?

조회 수: 1 (최근 30일)
Otto
Otto 2012년 11월 4일
답변: Dianne Dampil 2014년 2월 25일
Hi All,
I've written a code. Now, I want to tabulate this results using fprintf in a format that;
1'st column corresponds i(i.e. iteration number 1,2,3...), 2'st column corresponds xl, 3 column xu, 4'th f(xl), 5'th f(xu), 6'th xr, 7'th f(xr) and 8'th column corresponds ea. And also xl,xu,f(xl),f(xu) and f(xr) should have 7 digits after decimal point, xr must have 10 digits after decimal point. and finally ea must include 4 digits after decimal point.
Here is the 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;
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
end
ModFalsePos=xr
I've tried something but find out that some of the variables include only the last value of calculations. for example xl,xr, iter, but i want to present them in a listed form.
I'll appreciate for any help.
Thanks already for your interest!

채택된 답변

Matt Fig
Matt Fig 2012년 11월 5일
편집: Matt Fig 2012년 11월 5일
Holy lack of semi-colons! Try this:
home
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%10s%10s%10s%10s%10s%10s%10s\n',...
'iter','xl','xu','f(xl)','f(xu)','xr','f(xr)','ea')
fprintf([' ',repmat('-',1,74),'\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%10.2f%10.2f%10.2f%10.2f%10.2f%10.2f%10.2f\n',...
iter,xl,xu,f(xl),f(xu),xr,f(xr),ea)
end
fprintf('\n\n')
ModFalsePos=xr;
  댓글 수: 1
Otto
Otto 2012년 11월 5일
Hi Matt,
I've worked on the code which you sent a bit and I also have a final question. I want to display also the very first, the initial values (00'th values) and the last values (12'th iteration) too. how can I do this? Here is the new code;
home
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;
Thank you so much for your help!

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

추가 답변 (1개)

Dianne Dampil
Dianne Dampil 2014년 2월 25일
can someone help me tabulate this?

카테고리

Help CenterFile Exchange에서 Signal Generation and Preprocessing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by