0% found this document useful (0 votes)
112 views10 pages

Numerical Analysis PRACTICAL

The document is a final exam paper for Numerical Analysis consisting of 4 questions worth 5 marks each. Question 1 involves writing a bisection program in MATLAB to solve an equation. Question 2 involves using the trapezoidal rule in MATLAB to evaluate an integral. Question 3 requires writing a MATLAB code for Newton's method to solve an equation. Question 4 asks to construct a backward difference table program in MATLAB for a given set of y-values.

Uploaded by

Fahad Arshad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views10 pages

Numerical Analysis PRACTICAL

The document is a final exam paper for Numerical Analysis consisting of 4 questions worth 5 marks each. Question 1 involves writing a bisection program in MATLAB to solve an equation. Question 2 involves using the trapezoidal rule in MATLAB to evaluate an integral. Question 3 requires writing a MATLAB code for Newton's method to solve an equation. Question 4 asks to construct a backward difference table program in MATLAB for a given set of y-values.

Uploaded by

Fahad Arshad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Final Exam / Fall 2021 (Paper Duration 24 hours)

Course No.: CS-572 Course Title: Numerical Analysis


Total Marks: 20 Date of Exam: 09/02/2021
Degree: BSCS Semester: 5th Section: A+B
Marks
Q. No. 1 2 3 4 5 6 7 8 9 10 Obtained/
Total Marks
Marks
Obtaine
d
Total Marks in Words:
Name of the Teacher:
Who taught the course: Signature of Teacher / Examiner:

To be filled by Student

Registration No Name

Practical Examination FALL 2021

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:

% 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), '']

Q#2. Evaluate ∫ cosx dx ;n=8, by using MATLAB code of Trapezoidal rule.


0

(05)

ANSWER:

AB Program for Bisection MethodMATLAB

% 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), '']

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

% 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), '']

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;

x(1) = x0; % Set initial guess

n = 2;

nfinal = N + 1;

while (n <= N + 1)

fe = f(x(n - 1));

fpe = fp(x(n - 1));

x(n) = x(n - 1) - fe/fpe;

if (abs(fe) <= tol)

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']

x(n) = x(n - 1) - fe/fpe;

fprintf('%3d: %20g %20g\n', n, x(n), abs(fe));

if (abs(fe) <= tol)

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******* NIRALI PUBLICATIONS ********');

fprintf('\n**** CONM by M. T. Puranik & V. N. Chougule ****');

fprintf('\n** Interpolation by Newtons Backward Difference Formula **');

fprintf('\n**********************');

n = input('\nEnter number of data points = ');

h = input('\nEnter step size (h) = ')

x(1) = input('\nx0 = ');

y(1) = input('y0 = ');

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

%% Calculate Backward Differance Table

for j=2:n

for i=n:-1:j

diff(i,j)=diff(i,j-1)-diff(i-1,j-1);

end

end

%% Print Backward Differance Table

fprintf('\n\tX\t Backward Differance Table');

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);

You might also like