MATLAB: Find Root of Equation by Newton Raphson Method


Problem 1: Write programs to find the real root of the following equations by using Newton Raphson Method.
a)       f(x) = x3 – 3x – 1 = 0; correct to 5 decimal point near x = 0, 2, -2
b)       x sin x + cos x = 0; correct to 5 decimal point near x = 3
c)       x = e-x; correct to 5 decimal point, near x = 2
(a) Solve:

function y = f1(x);
y = x^3-3*x-1;

function y = f2(x);
y = 3*x^2-3;

When x = 0,

clc
clear all
close all
e = 10^(-5);

x0=0;
x1=0;

for i=1:100
    x1=x0-(f1(x0)/f2(x0));
    y=f1(x1);
    if abs(y)<=e
        break;
    else x0=x1;
    end
end
fprintf('The value of root is %.5f \n',x1);
fprintf('Number of iteration required %.f \n',i);

When x = 2,

clc
clear all
close all
e = 10^(-5);

x0=2;
x1=0;

for i=1:100
    x1=x0-(f1(x0)/f2(x0));
    y=f1(x1);
    if abs(y)<=e
        break;
    else x0=x1;
    end
end
fprintf('The value of root is %.5f \n',x1);
fprintf('Number of iteration required %.f \n',i);
When x = - 2,

clc
clear all
close all
e = 10^(-5);

x0=3;
x1=0;

for i=1:100
    x1=x0-(f1(x0)/f2(x0));
    y=f1(x1);
    if abs(y)<=e
        break;
    else x0=x1;
    end
end
fprintf('The value of root is %.5f \n',x1);
fprintf('Number of iteration required %.f \n',i);

(b) Solve:

function y=g1(x);
y=x*sin(x)+cos(x);

function y=g2(x);
y=x*cos(x);

clc
clear all
close all
e = 10^(-5);

x0=3;
x1=0;

for i=1:100
    x1=x0-(g1(x0)/g2(x0));
    y=g1(x1);
    if abs(y)<=e
        break;
    else x0=x1;
    end
end
fprintf('The value of root is %.5f \n',x1);
fprintf('Number of iteration required %.f \n',i);

(c) Solve:

function y = h1(x);
y = x-exp(-x);

function y = h2(x);
y = 1 + exp(-x);

clc
clear all
close all
e = 10^(-5);

x0=2;
x1=0;
for i=1:100
    x1=x0-(h1(x0)/h2(x0));
    y=h1(x1);
    if abs(y)<=e
        break;
    else x0=x1;
    end
end
fprintf('The value of root is %.5f \n',x1);
fprintf('Number of iteration required %.f \n',i);

Problem 2: Solve 1 (a) using roots, fzero, fsolve Matlab function
Solve:
ROOTS:
clc
clear all
close all
A = [1 0 -3 -1]
X = roots(A)

FZERO:
clc
clear all
close all
a = input('Enter the nearest guess value:');
x =fzero(@(x) x^3-3*x-1,a)

FSOLVE:
clc
clear all
close all

g = input('Enter the nearest guess value:');
X = fsolve(@(x) x^3-3*x-1, g)

Problem 3: Solve 1(b) and 1(c) using fzero, fsolve Matlab function.
Solve: 1 (b)
FZERO:
clc
clear all
close all

x = fzero(@(x) x*sin(x)+cos(x),3)

FSOLVE:
clc
clear all
close all

root=fsolve(@(x) x*sin(x)+cos(x),3)

Solve: 1 (c)
FZERO:
clc
clear all
close all

root = fzero(@(x) x - exp(-x), 2)



FSOLVE:
clc
clear all
close all

root = fsolve(@(x) x - exp(-x), 2)

Problem 4: What is the number of iterations required to find the root of problem 1(a), 1 (b), 1(c).
Solve:
Using Newton Raphson Method,
No of iteration required for 1(a) = 3
No of iteration required for 1(b) = 3
No of iteration required for 1(c) = 4

Problem 5: Write the advantage and disadvantage of Newton Raphson Method
Solve:
  • The method is very expensive - It needs the function evaluation and then the derivative evaluation. 
  • If the tangent is parallel or nearly parallel to the x-axis, then the method does not converge. 
  • Usually Newton method is expected to converge only near the solution.
  • The advantage of the method is its order of convergence is quadratic.
  • Convergence rate is one of the fastest when it does converge
  • If the solution met convergence, the required number of iteration decreases.
Discussion:  
In this experiment, we have used MATLAB Functions to find the value of root.
¢  The first method we used is Newton Raphson Method. Which is an open iterative method. The required equations and functionalities were studied at first. Then the procedure has been coded and implemented on MATLAB. The root was obtained by providing an initial guess value to the software.
¢  During the experiment a minimum amount of error level was maintained. All the values of roots are 5 decimal place accurate.
¢  During conducting the experiment none of the equations met divergence. So, the output was very fast and found after a few numbers of iteration.
¢  We have compared the no of iterations required in N/R Method with False Position Method. It seems N/R is a very fast method and required no of iteration is less than False Position Method.
¢  We have also learned alternative ways to find out the root. The alternative functions include roots, fzero, fsolve method.
¢  On roots no initialization is required. By inputting the value of co-efficient the roots are obtained. This function is only applicable for polynomial equations.
¢  fzero and fsolve function can solve any type of equation. But a initial guess value is required to find the nearby value of root.

أحدث أقدم