Final File - 5th Semester
Final File - 5th Semester
Final File - 5th Semester
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
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
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
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
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
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
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
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
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
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