Newton-Raphson Method for Solving non-linear equations in MATLAB(mfile)



MATLAB Program:

% Newton-Raphson Algorithm            
 % Find the root of y=cos(x) from o to pi.

 f = @(x) (cos(x));
 fd = @(x) (-sin(x));
 p0 = input('Enter initial approximaation: ');
 n = input('Enter no. of iterations, n: ');
 tol = input('Enter tolerance, tol:  ');

 i = 1;
 while i <= n
    d=f(p0)/fd(p0);
    p0 = p0 - d; 
    if abs(d) < tol
       fprintf('\nApproximate solution xn= %11.8f \n\n',p0);
       break;
    else
       i = i+1;
    end
 end




OUTPUT:

>> newton_raphson_m
Enter initial approximaation: 1
Enter no. of iterations, n: 20
Enter tolerance, tol:  0.0001

Approximate solution xn=  1.57079633

>> 


No comments