Numerical Analysis PRACTICAL
Numerical Analysis PRACTICAL
To be filled by Student
Registration No Name
Q#1. Write the Bisection programme for solving f ( x )=2 xsin ( 4 x )−( x +1 )3 , with given
interval [ −1 ,1 ]and ϵ =10−8 . (05)
ANSWER:
f=inline(a);
if f(xu)*f(xl)<0
else
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), '']
(05)
ANSWER:
f=inline(a);
if f(xu)*f(xl)<0
else
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), '']
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
f=inline(a);
if f(xu)*f(xl)<0
else
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), '']
Q#3. Write the MATLAB code of newton’ method for solving f ( x )=x 3 +e x , ϵ=10−7 ,with
initial guass p0=2 . (05)
ANSWER:
f = @(x) exp(x)-3*x;
fp = @(x) exp(x)-3;
x0 = 1;
N = 10;
tol = 1E-10;
n = 2;
nfinal = N + 1;
while (n <= N + 1)
fe = f(x(n - 1));
nfinal = n;
break;
end
n = n + 1;
end
plot(0:nfinal - 1,x(1:nfinal),'o-')
title('Solution:')
xlabel('Iterations')
ylabel('X')
table([1:length(x)]',x']
Q#4. Construct the programme for backward difference table for y=[12, 19 ,−1 , 2].
(05)
ANSWER:
clc;
clear all;
close all;
fprintf('\n**********************');
fprintf('\n**********************');
for i=2:n
x(i)=x(i-1)+h;
fprintf('\nX%d = %f',i,x(i));
fprintf('\t\tY%d: ',i);
y(i) = input('');
end
x_reqd = input('\nEnter X for which value of Y is sought: ');
s=(x_reqd-x(n))/h;
for i=1:n
diff(i,1)=y(i);
end
for j=2:n
for i=n:-1:j
diff(i,j)=diff(i,j-1)-diff(i-1,j-1);
end
end
for i=1:n
fprintf('\n %.3f',x(i));
for j=1:i
fprintf('\t%.3f',diff(i,j));
end
end
ans=y(n);
for i=1:n-1
term=1;
for j=1:i
term=term*(s+j-1)/j;
end
ans=ans+term*diff(n,i+1);
end
fprintf('\nValue of Y at(X=%f) = %f',x_reqd,ans);