Home > Back-end >  Matlab novice can help me take a look at why my Newton iterative method to find the roots of nonline
Matlab novice can help me take a look at why my Newton iterative method to find the roots of nonline

Time:11-04

The function/xnew, y=NM (fun, x0, tol, Max)
Syms x
Xnew=zeros (Max, 1);
Y=zeros (Max, 1);
Xold=zeros (Max, 1);
Xold (1)=x0;
Fp=the inline (diff (fun (x)));
For I=1: Max
Xnew (I)=xold (I) - (fun (xold (I))/fp (xold (I)));
Y (I)=feval (fun, xnew (I));
If (abs (xnew (I) - xold (I)) Fprintf (' NM method has converged \ n ');
break;
End
If y (I)==0
Fprintf (' Exact solution found \ n ");
break;

End
Iter=I + 1;
End

If (iter> Max)
Fprintf (' Zero not found to desired how within the maximum number of iterations \ n ');
End

% the Output results
K=1: iter.
Fprintf (' iter xold xnew y \ n ');
Disp ([k 'xold (1: iter) xnew (1: iter) y (1: iter)]);
  • Related