Final File - 5th Semester

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

NUMERICAL METHODS

PRACTICAL
FILE

BY SHRIYA JAIN
JESUS AND MARY COLLEGE
B.SC(HONS) MATHEMATICS
ROLL NO.: 160546

INDEX
S.NO. TOPIC PAGE NO.

1 Sum of Series
2 To find the
absolute value of
an integer
3 Finding maximum of
two numbers
4 Swapping numbers
5 Bisection Method
6 To sort the list
of numbers in
ascending order
7 Secant method
8 Lagrange
Interpolation
9 Simpson’s Rule
10 Gauss Jacobi
11 Gauss Siedel

SUM OF SERIES
EDITOR WINDOW

function sum_of_series
n=input('enter the value of n');
sum=1;
for i=2:n
sum=sum+(1.0)./i;
end
str=['sum of the series is:' num2str(sum)];
disp(str);
end

COMMAND WINDOW

>>sum_of_series

enter the value of n : 100

sum of the series is:5.1874

TO FIND THE ABSOLUTE VALUE OF AN INTEGER


EDITOR WINDOW

%program to find the absolute value of an integer


function absolute
a=input('Enter a number:');
if a>=0
a
else
-a
end

COMMAND WINDOW

>>absolute

Enter a number:-4

ans =

>>absolute

Enter a number:23

a=

23
FINDING MAXIMUM OF TWO NUMBERS
EDITOR WINDOW

function max2nos
a=input('Enter the first number: ');
b=input('Enter the second number; ');
if a>b
str=['max is;' num2str(a)];
disp(str);
else
str=['max is:' num2str(b)];
disp(str);
end
end

COMMAND WINDOW

>> max2nos

Enter the first number: 5

Enter the second number; 7

max is:7

SWAPPING NUMBERS
EDITOR WINDOW

function swapnos
x=input('enter a number x: ');
y=input('enter a number y: ');
temp=y;
y=x;
x=temp;
x
y
end

COMMAND WINDOW
>> swap2nos

enter a number x: 7

enter a number y: 5

x=

y=

BISECTION METHOD
EDITOR WINDOW

function[r]=bisection(a,b,eps,N)
f=@(x) x.^3+2*x.^2-3*x-1;
if (f(a)==0)
r=a;
return;
elseif (f(b)==0)
r=b;
return;
elseif (f(a)*f(b)>0)
error('f(a) and f(b) are not the of opposite sign')
end
for K=1:N
c=(a+b)/2;
if ((b-a)/2<eps)
r=c;
return;
end
if (f(c)==0)
r=c;
return;
elseif (f(a)*f(c)<0)
b=c;
else
a=c;
end
end
error('The method does not converge')
end

COMMAND WINDOW

>>bisection(1,2,5*(10)^(-5),50)
ans =

1.1987

TO SORT THE LIST OF NUMBERS IN ASCENDING


ORDER
EDITOR WINDOW

functionsort_numbers
A=input('Enter the list of numbers to be sorted :');
[m n]=size(A);
fori=1:n
for j=1:n-i
if(A(j)>A(j+1))
temp=A(j);
A(j)=A(j+1);
A(j+1)=temp;
end
end
end
str=['The sorted list is :' num2str(A)];
disp(str);
end

COMMAND WINDOW

>>sort_numbers

Enter the list of numbers to be sorted :[ 52 67 8 56 21 ]

The sorted list is :8 21 52 56 67

SECANT METHOD
EDITOR WINDOW

function [r]=secant_method
p0=input('enter the first approximation:');
p1=input('enter the second approximation:');
eps=input('enter the error tolerance:');
N=input('enter the maximun no. of iterations:');
f=@(x)x.^3+2*x.^2-3*x-1;
if(f(p0)==0)
r=p0;
return;
end
if(f(p1)==0)
r=p1;
return;
end
fori=1:N
if(abs(p1-p0)<eps)
r=p1;
return;
else
p2=p1-((p1-p0)/(f(p1)-f(p0)))*f(p1);
p0=p1;
p1=p2;
end
end
error('the method does not converge');
end

COMMAND WINDOW

>> [r]=secant_method

enter the first approximation:1

enter the second approximation:2

enter the error tolerance:0.00005

enter the maximun no. of iterations:50

r =

1.1987

LAGRANGE INTERPOLATION
EDITOR WINDOW

functionlagrange_interpolation
n=input('Enter the number of data points:');
xi=input('Enter the data values:');
fi=input('Enter the function values:');
symsx;
poly=0;
fori=1:n
L=1;
for j=1:n
if(j~=i)
L=L*(x-xi(j))/(xi(i)-xi(j));
end
end
poly=poly+L*fi(i);
end
simplify(poly)
end

COMMAND WINDOW

>>lagrange_interpolation

Enter the number of data points:3

Enter the data values:[0 1 3]

Enter the function values:[1 3 55]

ans =

8*x^2-6*x+1

SIMPSON’S RULE
EDITOR WINDOW

functionsimpsons
a=input('Enterlower limit of integration :');
b=input('Enter upper limit of integration ;');
f=@(x) 1/x;
int=(b-a)/6*(f(a)+4*f((a+b)/2)+f(b));
str=['The value of the integral is ' num2str(int)];
disp(str);
end

COMMAND WINDOW
>>simpsons

Enterlower limit of integration :1

Enter upper limit of integration ;3

The value of the integral is 1.1111

SECOND EXAMPLE

EDITOR WINDOW

function simpsons1
a=input('Enterlower limit of integration :');
b=input('Enter upper limit of integration ;');
f=@(x) exp(-x);
int=(b-a)/6*(f(a)+4*f((a+b)/2)+f(b));
str=['The value of the integral is ' num2str(int)];
disp(str);
end

COMMAND WINDOW

>> simpsons1

Enterlower limit of integration :0

Enter upper limit of integration ;1

The value of the integral is 0.63233

GAUSS JACOBI
EDITOR WINDOW

functionxnew=gauss_jacobi
n=('enter the number of equations, n:');
A=input('enter the n*n matrix :');
b=input('enter the RHS vector b :');
xold=input('enter the initial approx:');
eps=input('enter the error tolerance:');
nmax=input('enter maximum number of iterations:');
for k=1:nmax
fori=1:n
sum=0;
for j=1:n
if j~=i
sum=sum+A(i,j)*xold(j);
end
end
xnew(i)=(1./A(i,j))*[b(i)-sum];
end
if max(abs(xnew-xold))<eps
return;
else
xold=xnew
end
end
error('Method does not converge');
end

COMMAND WINDOW

>>xnew=gauss_jacobi

enter the number of equations, n:3

enter the n*n matrix :[5 1 2; -3 9 4; 1 2 -7]

enter the RHS vector b :[10 -14 -33]

enter the initial approx:[0 0 0]

enter the error tolerance:0.00005

enter maximum number of iterations:40

??? Error using ==>gauss_jacobi at 24

Method does not converge

GAUSS SIEDEL
EDITOR WINDOW

function xnew=gauss_siedel
n=input('enter the number of equations, n:');
A=input('enter the n*n matrix :');
b=input('enter the RHS vector b :');
xold=input('enter the initial approx:');
eps=input('enter the error tolerance:');
nmax=input('enter maximum number of iterations:');
for k=1:nmax
fori=1:n
sum=0;
for j=1:n
if j<i
sum=sum+A(i,j)*xnew(j);
end
if j>i
sum=sum+A(i,j)*xold(j)
end
end
xnew(i)=(1./A(i,j))*[b(i)-sum];
end
if max(abs(xnew-xold))<eps
return;
else
xold=xnew
end
end
error('Method does not converge');
end

COMMAND WINDOW

>>xnew=gauss_siedel

enter the number of equations, n:3

enter the n*n matrix :[5 1 2; -3 9 4; 1 2 -7]

enter the RHS vector b :[10 -14 -33]

enter the initial approx:[0 0 0]

enter the error tolerance:0.00005

enter maximum number of iterations:40

??? Error using ==>gauss_scidel at 27

Method does not converge

You might also like