Professional Documents
Culture Documents
Lab No.11
Lab No.11
Lab No.11
Submitted By:
Muhammad Hassan (2020-MC-274)
Submitted to:
Dr. Arshi Khalid
--------------------------------------------------------------------------------------------------------
-
Lab Objective:
Implement the following Root Finding Methods on the Matlab.
xn+1=xn − f′(xn)/f(xn)
1. Compute f′(xn).
2. Choose an appropriate initial guess x0.
3. Apply the next point formula and calculate the next points until the difference of x(n+1) – x(n) ≈ 0.
Matlab Code:
% Newton Raphson Method
clc;
clear;
syms x;
f(x) = input('\nPlease Enter the Function without function handler.\nEnter the
function: ');
% to differentiate the input function w.r.t to x
g(x) = diff(f(x),x);
% to take the initial guess
a = 0;
b = 1;
while ((f(a)*f(b))>0)
a = a+1;
b = b+1;
end
x(1) = (a+b)/2;
x(2) = x(1) - (f(x(1)))/(g(x(1)));
n = 3;
while (((x(n-1) - x(n-2)) > 0.00001) || ((x(n-1) - x(n-2)) < -0.00001))
x(n) = x(n-1) - (f(x(n-1)))/(g(x(n-1)));
2
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid
n = n+1;
end
fprintf('\nRoot from Newton-Raphson Method is: \t%f\n',x(n-1));
Output: