You are on page 1of 8

Assignment no: ______

NAME: uzair aslam


REG No: FA19-CVE-033
Subject: NC-Lab
Submitted to: Sir Asad
Date: 19-3-2021
Q.1:- Implement Newton Raphson method as function. Take function,
initial guess and tolerance as input from user. Calculate its derivative
and find its roots.
Answer:-
% Program Code of Newton-Raphson Method in MATLAB
a=input('Enter the function in the form of variable x:','s');
x(1)=input('Enter Initial Guess:');
error=input('Enter allowed Error:');
f=inline(a)
dif=diff(sym(a));
d=inline(dif);
for i=1:100
x(i+1)=x(i)-((f(x(i))/d(x(i))));
err(i)=abs((x(i+1)-x(i))/x(i));
if err(i)<error
break
end
end
root=x(i)

Newton-Raphson Method Example:


Now, let’s analyze the above program of Newton-Raphson method in Mat lab,
taking the same function used in the above program and solving it numerically.
The function is to be corrected to 9 decimal places.

Solution:
Given function: x3−x−1 = 0, is differentiable.
The first derivative of f(x) is f’(x) = 3x2 – 1
Let’s determine the guess value.
f (1) = 1 -1 -1 = -1 and f (2) = 8 – 2 -1 = 5
Therefore, the root lies in the interval [1, 2]. So, assume x1= 1.5 as the initial
guess root of the function f(x) = x3−x−1.
Now,
f (1.5) = 1.53 – 1.5 – 1 = 0.875
f’ (1.5) = 3 * 1.52– 1 = 5.750
Using Newton’s iteration formula:
x2 = x1 – f(x1)/f’(x1) = 1.5 – 0.875/5.750 = 1.34782600
The iteration for x3, x4, is done similarly.
The table below shows the whole iteration procedure for the given function in the
program code for Newton Raphson in MATLAB and this numerical example.

Therefore, x = 1.324717957 is the desired root of the given function, corrected to


9 decimal places. The MATLAB program gives the result x = 1.3252 only, but this
value can be improved by improving the value of allowable error entered.
Q.2:- Solve any five exercise questions from book using both methods
and compare them.

Bisection Method:-
% Bisection Method in MATLAB
a=input('Enter function with right hand side zero:','s');
f=inline(a);
xl=input('Enter the first value of guess interval:') ;
xu=input('Enter the end value of guess interval:');
tol=input('Enter the allowed error:');
if f(xu)*f(xl)<0
else
    fprintf('The guess is incorrect! Enter new guesses\n');
    xl=input('Enter the first value of guess interval:\n') ;
    xu=input('Enter the end value of guess interval:\n');
end
for i=2:1000
xr=(xu+xl)/2;
if f(xu)*f(xr)<0
    xl=xr;
else
    xu=xr;
end
if f(xl)*f(xr)<0
    xu=xr;
else
    xl=xr;
end
 
xnew(1)=0;
xnew(i)=xr;
if abs((xnew(i)-xnew(i-1))/xnew(i))<tol,break,end
end
str = ['The required root of the equation is: ', num2str(xr), '']

Bisection Method Example:


Now, let’s analyze the above program of bisection method in Mat
lab mathematically. We have to find the root of x2 -3 = 0, starting with
the interval [1, 2] and tolerable error 0.01.
Given that, f(x) = x2 -3 and a =1 & b =2
Mid-value of the interval, c = (a+b)/2 = (a+2)/2 = 1.5
f(c) = 1.52 -3 = -0.75
Check for the following cases:
f(c) ≠ 0 : c is not the root of given equation.
f(c ) * f(a) = -0.75 * -2 = 1.5 > 0 : root doesn’t lie in [1, 1.5]
f(c ) * f( b) = -0.75 * 1= -0.75 < 0 : root lies in [1.5, 2]
The process is then repeated for the new interval [1.5, 2]. The table shows the
entire iteration procedure of bisection method and its MATLAB program:
Thus, the root of x2 -3 = 0 is 1.7321. It is slightly different from the one obtained
using MATLAB program. But, this root can be further refined by changing the
tolerable error and hence the number of iteration.
If you have any questions regarding bisection method or its MATLAB code, bring
them up from the comments. You can find more Numerical methods tutorial
using MATLAB

Newton Method:-
% Program Code of Newton-Raphson Method in MATLAB
a=input('Enter the function in the form of variable x:','s');
x(1)=input('Enter Initial Guess:');
error=input('Enter allowed Error:');
f=inline(a)
dif=diff(sym(a));
d=inline(dif);
for i=1:100
x(i+1)=x(i)-((f(x(i))/d(x(i))));
err(i)=abs((x(i+1)-x(i))/x(i));
if err(i)<error
break
end
end
root=x(i)

Newton-Raphson Method Example:


Now, let’s analyze the above program of Newton-Raphson method in Mat lab,
taking the same function used in the above program and solving it numerically.
The function is to be corrected to 9 decimal places.

Solution:
Given function: x3−x−1 = 0, is differentiable.
The first derivative of f(x) is f’(x) = 3x2 – 1
Let’s determine the guess value.
f (1) = 1 -1 -1 = -1 and f (2) = 8 – 2 -1 = 5
Therefore, the root lies in the interval [1, 2]. So, assume x1= 1.5 as the initial
guess root of the function f(x) = x3−x−1.
Now,
f (1.5) = 1.53 – 1.5 – 1 = 0.875
f’ (1.5) = 3 * 1.52– 1 = 5.750
Using Newton’s iteration formula:
x2 = x1 – f(x1)/f’(x1) = 1.5 – 0.875/5.750 = 1.34782600
The iteration for x3, x4, is done similarly.
The table below shows the whole iteration procedure for the given function in the
program code for Newton Raphson in MATLAB and this numerical example.
Therefore, x = 1.324717957 is the desired root of the given function, corrected to
9 decimal places. The MATLAB program gives the result x = 1.3252 only, but this
value can be improved by improving the value of allowable error entered.

You might also like