Below is a basic code of finding a root of a function using the Newton Root method. While i set the imax, which is the maximum number of iteration, the final number of iteration running this code has exactly one more than my maximum no. of iterations set. And the no. of iteration is different with the one of the model answer from my teacher. Anyone has an idea why is this? I have exam next week please help.
clear; clc; Fun=@(x)(8-4.5*(x-sin(x))); FunDer=@(x)(-4.5*(1-cos(x))); root=newtonroothaha(Fun,FunDer,2,0.0001,10)
function[Xs]=newtonroothaha(Fun,FunDer,Xest,Err,imax) oerr=100; iteration=0; while (iteration<=imax && oerr>Err) iteration=iteration+1; Xnew=Xest-(Fun(Xest)/FunDer(Xest)); oerr=abs((Xnew-Xest)/Xest); end Xs=Xnew; iact=iteration end

 채택된 답변

Roger Stafford
Roger Stafford 2015년 3월 20일

0 개 추천

Your 'newtonroothaha' function has the fatal error that Xest is never replaced by the newly calculated Xnew inside the while loop, so the loop will continue with the same values until the imax limit. Hence the Xs which is returned will be an erroneous root.
The iteration count problem, caused by allowing iteration=imax in the while condition followed by iteration=iteration+1, is a very minor difficulty by comparison.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2015년 3월 20일

댓글:

2015년 3월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by