Maths
Maths
Maths
DEPARTMENT OF MATHEMATICS
NAME : ________________________________
REG.NO : ________________________________
SUBJECT : ________________________________
SEMESTER : ________________________________
PAVENDAR BHARATHIDASAN COLLEGE OF ARTS AND SCIENCE
DEPARTMENT OF MATHEMATICS
Certified that this bonafide recorded for the _______________ practical work
done by ____________________ for ___________ semester in the subject
___________________________________ During the academic year ______________
Register No:___________________________
1 Linear Interpolation
2 Liner Regression
3 Curve Fitting
function[]=trapezoidal_Rule()
format compact
clc
y=inline('1/(1+x.*x)');
x0=input('enter x0:');
xn=input('enter xn:');
h=(xn-x0)/n;
s=y(x0)+y(xn);
for i=0:n-1
s=s+2.*y(x0+i.*h);
end
end
TRAPEZOIDAL RULE OF INTEGRATION
OUTPUT
enter x0:0
enter xn:6
function[]=simpson_rule()
clc
y=inline('1/(1+x.*x)');
x0=input('Enter x0:');
xn=input('enter xn:');
h=(xn=x0)/n;
s=y(x0)+y(xn)+4.*y(x0+h);
for i=3:2:n-1
s=s+4*y(x0+i.*h)+2.*y(x0+(i-1).*h);
end
end
SIMPSON’S 1/3 RULE OF INTEGRATION
OUTPUT
Enter x0:8
enter xn:9
OUTPUT
Enter the function in the form of variable x:
x^3-x-1
Enter the intial guess:
1.5
Enter the allowed error:
0.0001
iteration no 1 x=1.347826
iteration no 2 x=1.325200
iteration no 3 x=1.324718
iteration no 4 x=1.324718
root =
1.3247
GAUSS – ELIMINATION METHOD OF SOLVING SIMULTANEOUS
EQUATIONS
function[]=gauss_elimination_method()
clc
N=4
a=input('Enter the element of matrics:\n')
for j=1:N-1
for i=j+1:N
t=a(i,j)/a(j,j);
for k=1:N+1
a(i,k)=a(i,k)-a(j,k)*t;
end
end
end
for i=1:N
for j=1:N+1
fprintf('%8.4f',a(i,j));
end
fprintf('\n');
end
for i=N:-1:1
s=0;
for j=i+1:N
s=s+a(i,j+1).*x(j);
end
x(i)=(a(i,N)-s)/a(i,i);
end
for i=1:N
fprintf('x(%d)=%6.4f\n',i,x(i))
end
GAUSS – ELIMINATION METHOD OF SOLVING SIMULTANEOUS
EQUATIONS
OUTPUT
N=
a=
10 -7 3 5 6
-6 8 -1 -4 5
3 1 4 11 2
5 -9 -2 4 7
function[]=Gauss_Seidal_method()
clc
a=input('enter the element of matrix:\n');
aerr=input('enter the allowed error');
maxitr=input('enter the maximum no of iterations:');
N=4;
x=zeros(1,N);
fprintf('iterations x[1] x[2] x[3]\n');
for itr=1:maxitr
maxerr=0;
for i=1:N-1
s=0;
for j=1:N-1
if(j~=i)
s=s+a(i,j)*x(j);
end
end
end
end
t=(a(i,N)-s)/a(i,i);
err=abs(x(i)-t);
if(err>maxerr)
maxerr=err;
end
GAUSS –SEIDAL METHOD OF SOLVING SIMULTANEOUS
EQUATIONS
OUTPUT
function[]=Gauss_Kutta_Method()
clc
format compact
format short g
f=inline('x+y*y');
x0=input('enter the value x0:');
y0=input('enter the value y0:');
h=input('enter the value h:');
xn=input('enter the value xn:');
x=x0;
y=y0;
while(1)
if(x==xn)
break
end
k1=h*f(x,y);
k2=h*f(x+h/2,y+k1/2);
k3=h*f(x+h/2,y+k2/2);
k4=h*f(x+h,y=k3);
k=(k1+(k2+k3)*2+k4)/6;
x=x+h;
y=y+h;
fprintf('when x=%f y=%f\n',x,y)
end
end
R-K FOURTH ORDER METHOD OF SOLVING DIFFERENT
EQUATIONS
OUTPUT
enter the value x0:0
enter the value y0:1
enter the value h:0.2
enter the value xn:0.2
when x=0.200000 y=0.475832
LAGRANGE’S METHOD OF INTERPOLATION
function[]=lagrange_interpolation_formula
clc
max=100;
n=input('enter value of n:');
ax=input('enter value of x:');
ay=input('enter value of y:');
x=input('enter value of x for which y is wanted:');
y=0;
for i=1:n+1
dr=1;
nr=1;
for j=1:n+1
if(j~=i)
nr=nr*(x-ax(j));
dr=dr*(ax(i)-ax(j));
end
end
y=y+((nr/dr)*ay(i));
end
fprintf('when x=%4.1fy=%4.1f\n',x,y);
end
LAGRANGE’S METHOD OF INTERPOLATION
OUTPUT
function[y0,a,b]=linear_regression(x,y,x0)
x=[71 73 64 65 61 70 65 72 63 67 64]
y=[160 183 154 168 159 180 145 210 132 168 141]
[y0,a,b]=linear_regression(x,y,70)
%number of known points
n=lenght(x);
%intialization
j=0;k=0;l=0;p=0;
%accumulate intermediate sums
j=sum(x);
k=sum(y);
l=sum(x.^2);
p=sum(x.*y);
%compute cure co-effiecients
b=(n*p-k*j)/(n*l-j^2);
a=(k-b*j)/n;
%interpolation value
y0=a+b*x0;
LINEAR REGRESSION
COMMAND:
x=[71 73 64 65 61 70 65 72 63 67 64];
y=[160 183 154 168 159 180 145 210 132 168 141];
[y0,a,b]=linear_regression(x,y,70)
RESULT:
y0=176.5139
a=-106.7917
b=4.0472
COMMAND:
[y0]=linear_regression(x,y,72)
RESULT:
y0=184.6083
CURVE FITTING
clear all
clc
format compact
%define coordinates
x=[1 2 3 4 5.5 7 10];
y=[3 7 9 15 22 21 21]
%plot given data in red
plot(x,y,'ro','linewidth',2)
hold on
%find polynominal fits of different orders
p1=polyfit(x,y,1)
p2=polyfit(x,y,2)
p4=polyfit(x,y,4)
p5=polyfit(x,y,5)
%see interpolated values of fits
xc=1:.1:10;
%plot the first order polynominal
y1=polyval(p1,xc);
plot(xc,y1,'m.-')
%plot the second order polynominal
y1=polyval(p2,xc);
plot(xc,y2,'g.-')
%plot the 4th order polynominal
y1=polyval(p4,xc);
plot(xc,y4,'linewidth',2)
%plot the 4th order polynominal
y1=polyval(p5,xc);
plot(xc,y5,'k.','linewidth',2)
grid
title('cure fitting')
legend('original data','1st order fit','2nd order fit','4th order','5th
order','location','north west')
CURVE FITTING
OUTPUT
CURVE FITTING
COMMAND WINDOW
y=
3 7 9 15 22 21 21
p1 =
2.1763 3.8960
p2 =
-0.3863 6.3983 -4.1596
p4 =
0.0334 -0.7377 5.0158 -8.4137 7.5779
p5 =
0.0213 -0.5022 4.1330 -14.5708 25.3233 -11.3510
LINEAR INTERPOLATION
clear all
clc
format compact
x=0:1.0:5;
xi=0:0.1:5;
yi=interp1(x,y,xi,'linear');
subplot(1,2,1)
plot(x,y,'ro',xi,yi,'g.-');
legend('y','yi');
x=[0:10];
y=cos(x);
xj=[0:.25:10];
subplot(1,2,2)
yj=interp1(x,y,xj,'linear');
plot(x,y,'ro',xj,yj,'b.-');
legend('y','yj');
x(i)=t;
fprintf('%5d\t\t',itr)
for i=1:N-1
fprintf('%f\t',x(i))
end
fprintf('\n')
if(maxerr<aerr)
for i=0:N-1
fprintf('x(%d)=%2.4f\n',i,x(i))
end
return
end
return
LINEAR INTERPOLATION
OUTPUT
LINEAR INTERPOLATION
LINEAR INTERPOLATION