필터 지우기
필터 지우기

how to see previous results while tabulating?

조회 수: 1 (최근 30일)
Otto
Otto 2012년 11월 8일
Hi All,
I've written a code using Newton-Raphson algorythm but i have problems with tabulating. I want to tabulate my results in a form which first column corresponds "it_no"(iteration number),second column corresponds "x", third column corresponds "f(x)". But I'm only getting the last results of iteration, i.e it_no 9;
Here is the code
f=@(x)x^5-x^4+(2*x^3)-(3*x^2)+x-4
dfdx=@(x)(5*x^4)-(4*x^3)+(6*x^2)-(6*x)+1
x0=5
x=x0
it_no=0
while 1
f(x)
it_no=it_no+1
xprev=x
x=xprev-(f(x)./dfdx(x))
if f(x)<10^-4
break
end
end
fprintf('\n\n%18s%18s%18s\n','it_no','x','f(x)')
fprintf(' %17.0f\t %17.7f\t %17.7f\t\n',[it_no,x,f(x)])
I want to see all results from first iteration to last iteration. Anyway to see previous results in my table?
I'll appreciate for any help
Thanks Already!

채택된 답변

John Petersen
John Petersen 2012년 11월 8일
편집: John Petersen 2012년 11월 8일
Change where you put your print statements to see running total.
fprintf('\n\n%18s%18s%18s\n','it_no','x','f(x)');
while 1
it_no=it_no+1;
xprev=x;
x=xprev-(f(x)./dfdx(x));
if f(x)<10^-4;
break
end
fprintf(' %17.0f\t %17.7f\t %17.7f\t\n',[it_no,x,f(x)]);
end
For faster code, you could vectorize it, but it would print after the loop not while running in the loop.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Biotech and Pharmaceutical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by