0% found this document useful (0 votes)
68 views

NSM Practical Sem II1

The document contains programs and code snippets for various numerical methods problems: 1) Bisection and false position methods to solve a cubic equation between intervals. 2) Newton's forward, backward, and Lagrange interpolation methods. 3) Gauss-Seidel method to solve a system of equations. 4) Trapezoidal, Simpson's 1/3rd and 3/8th rules for numerical integration. 5) Euler's, modified Euler's, and Runge-Kutta methods to solve differential equations. 6) Programs to find covariance and Pearson's correlation coefficient from data sets.

Uploaded by

Mr.Hacker Anup
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

NSM Practical Sem II1

The document contains programs and code snippets for various numerical methods problems: 1) Bisection and false position methods to solve a cubic equation between intervals. 2) Newton's forward, backward, and Lagrange interpolation methods. 3) Gauss-Seidel method to solve a system of equations. 4) Trapezoidal, Simpson's 1/3rd and 3/8th rules for numerical integration. 5) Euler's, modified Euler's, and Runge-Kutta methods to solve differential equations. 6) Programs to find covariance and Pearson's correlation coefficient from data sets.

Uploaded by

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

FY B.Sc.

IT NSM Practical

1a Program to solve x3 -2x2 - 2x-1 between the interval (2, 3) by bisection method.

function [x]=bisect(a, b, f)
n=5;
while(n>0)
c=(a+b)/2;
disp(f(a),'name of f(a)');
disp(f(b),'name of f(b)');
if(f(a)*f(c)<0) then
b=c;
else
a=c;
end;
n=n-1;
disp(c,'c =');
disp(f(c),'name of f(c)');
end;
abort;
endfunction;

Scilab Console:
deff('[y]=f(x)','y=x^3-2*x^2-2*x-1')
bisect(2,3,f)
c = 2.84375

1b Program to solve x3 -2x2 - 2x-1 between the interval (2, 3) by false position method.
function [x]=method(a, b, g)
n=5;
while(n>0)
c=[a*f(b)-b*f(a)]/[f(b)-f(a)];
if(f(a)*f(c)<0) then
b=c;
else
a=c;
end;
n=n-1;
disp(c);
disp('name of f(x)',f(c));
end;
abort;
endfunction;

Scilab Console:
deff('[y]=f(x)','y=x^3-2*x^2-2*x-1')
method(2,3,f)
Ans-2.8311672
1c Program to solve y=x2 - 5 by taking initial root 2 by Newton Raphson method.

function [x]=newton(xx, f, fp)


n=5;
while(n>0)
xn=xx-f(xx)/fp(xx);
n=n-1;
xx=xn;
disp(xn);
end;
abort;
endfunction;

Scilab Console:
deff('[y]=f(x)','y=x^2-5')
deff('[y]=fp(x)','y=2*x')
newton(2,f,fp)
2.25
2.2361111
2.236068
2.236068
2.236068
2a Program for Newton's forward interpolation to find f (8) from the data.
x 5 10 15 20
y 50 70 100 145

function [yest]=newtonfor(x, y, xest)


n=length(y);
disp(n,'length is')
h=x(2)-x(1);
disp(h,'step size is')
p=(xest-x(1))/h;
disp(p,'k is')
//
for i=1:n-1
d(i,1)=y(i+1)-y(i);
end
for j=2:n-1
for i=1:n-j
d(i,j)=d(i+1,j-1)-d(i,j-1);
end

end
e(1)=p;
for j=2:n-1
e(j)=e(j-1)*(p+1-j)/j;
end
yest=0;
for i=1:n-1
yest=yest+e(i)*d(1,i);
end
yest=yest+y(1);

printf('estimated value of y=%g when value of x=%g \n',yest,xest);


endfunction;

Scilab Console:
x=[5,10,15,20]
y=[50,70,100,145]
xest=8
newtonfor(x,y,xest)
ans = 61.08
2b Program for Newton's backward interpolation to find f (3.6) from the data.
x 0 1 2 3 4
y 7 17 45 103 203

function [yest]=newtonback(x, y, xest)


n=length(y);
disp(n,'length is')
h=x(2)-x(1);
disp(h,'step size is')
p=(xest-x(n))/h;
disp(p,'k is')

for i=2:n
d(i,1)=y(i)-y(i-1);
End
for i=2:n-1
for j=i+1:n
d(j,i)=d(j,i-1)-d(j-1,i-1);
End
End
e(1)=p;
for i=2:n-1
e(i)=e(i-1)*(p+i-1)/i;
End
yest=0;
for i=1:n-1
yest=yest+e(i)*d(n,i);
End
yest=yest+y(n);
printf('estimated value of y=%g when value of x=%g \n',yest,xest);
endfunction;

Scilab Console:
x=[0,1,2,3,4]
y=[7,17,45,103,203]
xest=3.6
newtonback(x,y,xest)
ans =
157.192
2c Program for Lagrange's interpolation to find f (2.5) from the data.
x 1 2 3
y 7 18 35

clc;
n=input("no. of data :");
for i=1:n
printf("Enter x%d value",i)
x(i)=input("")
printf("Enter y%d value",i)
y(i)=input("")
end
a= input("Enter value of a :")
sum=0
for i=1:n
product=1
for j=1:n
if j==i then continue
end
product=product*(a-x(j))/(x(i)-x(j))
end
product=product*y(i)
sum=sum + product

end
printf("value of f(a)is %f",sum)

Scilab Console:
no. of data :3
Enter x1 value
1
Enter y1 value
7
Enter x2 value
2
Enter y2 value
18
Enter x3 value
3
Enter y3 value
35
Enter value of a :2.5
value of f(a)is 25.750000
3b Program for solving the following equations using gauss seidel methods (Carry 4 iterations).
28x+4y-z=32
2x+17y+4z=35
x+3y+10z=24
clc
funcprot(0)
for i=1:3
printf("Enter coefficient of x in%d equation",i)
a(i)=input("")
printf("Enter coefficient of y in%d equation",i)
b(i)=input("")
printf("Enter coefficient of z in%d equation",i)
c(i)=input("")
printf("Enter value of d in %d equation",i)
d(i)=input("")

end
k=input("no of iterations")
x=0
y=0
z=0
for i=1:k
x= (1/a(1))*(d(1)-b(1)*y-c(1)*z)
y= (1/b(2))*(d(2)-a(2)*x-c(2)*z)
z= (1/c(3))*(d(3)-a(3)*x-b(3)*y)
end
printf("value of x is %f \n",x)
printf("value of y is %f \n",y)
printf("value of z is %f \n",z)

Scilab Console:
Enter coefficient of x in1 equation
28
Enter coefficient of y in1 equation
4
Enter coefficient of z in1 equation
-1
Enter value of d in 1 equation
32
Enter coefficient of x in2 equation
2
Enter coefficient of y in2 equation
17
Enter coefficient of z in2 equation
4
Enter value of d in 2 equation
35
Enter coefficient of x in3 equation
1
Enter coefficient of y in3 equation
3
Enter coefficient of z in3 equation
10
Enter value of d in 3 equation
24
no of iterations4
value of x is 0.993301
value of y is 1.507016
value of z is 1.848565
4a Program for numerical integration of (4x+2) in the limits 1 to 4 using trapezoidal rule using
six strips.
function [x]=trapezoidal2(x0, xn, n, f)
h=(xn-x0)/n;
add=0;
z0=f(x0);
zn=f(xn);
for i=1:n-1
x0=x0+h;
add=add+(2*f(x0));
end
x=(h/2)*(z0 + zn + add)
endfunction

Scilab Console:
deff('[z]=f(x)','z=(4*x)+2')
trapezoidal2(1,4,6,f)
ans= 36

4b Program for numerical integration of (3x2 +2x) in the limits 1 to 3 using Simpson's 1/3rd
rule taking 4 strips.

function [i]=Simp13(a, b, n, f)
h=(b-a)/n;
x=(a:h:b);
y=f(x);
m=length(y);
i=y(1)+y(m);
for j=2:m-1
if((modulo(j,2)==0)) then
i=i+4*y(j);
else
i=i+2*y(j);
end
end
i=h*i/3;
return(i);
endfunction

Scilab Console:
deff('[z]=f(x)','z=(3*x^2)+2*x')
Simp13(1,3,4,f)
ans=34

4c Program for numerical integration of (4x-1) in the limits 1 to 4 using Simpson's 3/8th rule
using six strips.

function [i]=Simp38(a, b, n, f)
h=(b-a)/n;
x=(a:h:b);
y=f(x);
m=length(y);
i=y(1)+y(m);
for j=2:m-1
if((modulo(j-1,3)==0)) then
i=i+2*y(j);
else
i=i+3*y(j);
end
end
i=3*h*i/8;
return(i);
endfunction

Scilab Console:
deff('[z]=f(x)','z=(4*x)-1')
Simp38(1, 4, 6, f)
ans = 27.

5a Program to solve differential equation 1+y 2 using Euler's method


y(0)=0; Find y(0.1) & y(0.3). Carry 3 iterations.

clc;
clear;
function [z]=Euler(x0, y0, n, yest, f)
h=(yest-x0)/n;
for i=1:n
z=y0+(f(x0,y0)*h)
x0=x0+h;
y0=z;
end
endfunction

Scilab Console:
deff('[z]=f(x,y)','z=(1+(y^2))')
Euler(0,0,3,0.1,f)
ans = 0.1001853
Euler(0,0,3,0.3,f)
ans = 0.3050401

5b Program to solve differential equation x2 +y using Modified Euler's method


y(0)=1; Find y(0.02). Carry 2 iterations.
clc;
function [z1]=Mod_Euler(x0, y0, n, yest, f)
h=(yest-x0)/n;
for i=1:n
z=y0+(f(x0,y0)*h)
z1=y0+((h/2)*(f(x0,y0)+f(x0+h,z)));
x0=x0+h;
y0=z;
end
endfunction
Scilab Console:
deff('[z]=f(x,y)','z=((x^2)+y)')
Euler(0,1,2,0.02,f)
ans = 1.020101

5c Program to solve differential equation 1+y2 at x=0.2 using Runqe-kutta 2nd order method.
y(0)=0; Take step size=0.2.
clc;
clear;
function [z]=Rungekutta(a, b, x, h, f)
n=(x-a)/h;

for i=1:n
k1=h*f(a,b);
k2=h*f((a+(h/2)),(b+k1));
k=(k1+k2)/2;
z=b+k;
a=a+h;
b=z;
end
endfunction

Scilab Console:
deff('[z]=f(x,y)','z=(1+(y^2))')
Rungekutta(0,0,0.2,0.2,f)
ans = 0.204

5d Program to solve differential equation x+y at x=0.2 using Runqe-kutta 4th order method.
y(0)=1; Take step size=0.2.
clc;
clear;
function [z]=Rungekutta4(a, b, x, h, f)
n=(x-a)/h;
for i=1:n
k1=h*f(a,b);
k2=h*f((a+(h/2)),(b+k1/2));
k3=h*f((a+(h/2)),(b+k2/2));
k4=h*f((a+h),(b+k3));
k=(k1+2*k2+2*k3+k4)/6;
z=b+k;
a=a+h;
b=z;
end
endfunction
Scilab Console:
deff('[z]=f(x,y)','z=(x+y)')
Rungekutta4(0,1,0.2,0.2,f)
ans =1.2428

6a Program to find covariance from the following data.


x 6 2 10 4 8
Y 9 11 5 8 7

function [c]=cov(x, y)
n=length(x);
sx=sum(x);
sy=sum(y);
sxy=sum(x*y');
xmean=sx/n;
ymean=sy/n;

c=((sxy/n)-xmean*ymean);
if(c>0) then
printf(' x and y are positively correlated \n');
else
printf(' x and y are negatively correlated \n');
end;
return(c);
endfunction;

Scilab Console:
x=[6,2,10,4,8];
y=[9,11,5,8,7];
cov(x,y)
x and y are negatively correlated
ans = - 5.2

6b Program to find Karl Pearson's coefficient of correlation from the following data.
x 6 2 10 4 8
Y 9 11 5 8 7

function [r]=correlation(x, y)
n=length(x);
sx=sum(x);
sy=sum(y);
sxy=sum(x*y');
sxx=sum(x*x');
syy=sum(y*y');
xmean=sx/n;
ymean=sy/n;

sd1=sqrt((sxx/n)-xmean^2);
sd2=sqrt((syy/n)-ymean^2);
c=((sxy/n)-xmean*ymean);

r=c/(sd1*sd2);
if(r>0) then
printf(' x and y are positively related \n');
else
printf(' x and y are negatively related \n');
end;
return(r);
endfunction;

Scilab Console:
x=[6,2,10,4,8];
y=[9,11,5,8,7];
correlation(x,y)
x and y are negatively related
ans = - 0.9192388

7a In a sample of 45 persons from town A 20 are smokers and in another sample of 50 persons
from town B 24 are smokers. Use Z test and test the claim that proportion of smokers in
two towns do not differ significantly.
function [zcal, ztab]=doubleProp(n1, n2, x1, x2, altype, alpha)
p1=x1/n1;
p2=x2/n2;
pp=(x1+x1)/(n1+n2);
qq=1-pp;
zcal=(p1-p2)/sqrt(pp*qq*(1/n1+ 1/n2));

if (altype==2) then
ztab=cdfnor('X',0,1,1-alpha/2,alpha/2);
else
ztab=cdfnor('X',0,1,1-alpha,alpha);

end;
if (altype==2) then
if abs(zcal)>ztab then
printf('\n reject ho\n');
else
printf('\n do not reject ho\n');
end;
else
if(zcal>ztab) then
printf('\n reject ho\n');
else
if(zcal<-ztab) then
printf('\n reject ho\n');
else
printf('\n do not reject ho\n');
end;
end;
end;
printf("\n zcal is=%f \n ztab is =%f \n",zcal,ztab);
endfunction

Scilab Console:
n1=45;n2=50;x1=20;x2=24;altype=1;alpha=0.05;
doubleProp(n1,n2,x1,x2,altype,alpha);
do not reject ho
zcal is=-0.350469
ztab is =1.644854

7b A sample of 1000 items from population A have mean 171 and standard deviation 6.5
where as another sample of 1200 items from other population B have mean 170 and
standard deviation 6.3. Can we say that two populations have same variances?

function []=doubled(n1, n2, x1, x2, s1, s2, altype, alpha)

if length(x1)>1&length(x2)>1 then
s1=st_deviation(x1);
s2=st_deviation(x2);
n1=length(x1);
n2=length(x2);
end
zcal=(s1-s2)/sqrt(s1^2/(2*n1)+s2^2/(2*n2));

if (altype==2) then
ztab=cdfnor('X',0,1,1-alpha/2,alpha/2);
else
ztab=cdfnor('X',0,1,1-alpha,alpha);

end;
if (altype==2) then
if abs(zcal)>ztab then
printf('\n reject ho\n');
else
printf('\n do not reject ho\n');
end;
else
if(zcal>ztab) then
printf('\n reject ho\n');
else
if(zcal<-ztab) then
printf('\n reject ho\n');
else
printf('\n do not reject ho\n');
end;
end;
end;
printf("\n zcal is=%f \n ztab is =%f \n",zcal,ztab);
endfunction
Scilab Console:
doubled(1000,1200,171,170,6.5,6.3,2,0.05);
do not reject ho
zcal is =1.030565
ztab is =1.959964

8a Program to fit curve by principle of least squares for the following data
x 1 2 3 4 5 6
y 2 4 3 6 8 9

function []=curvefit1(x, y)
[rx,cy]=size(y);
n=cy;

sx=sum(x);
sy=sum(y);
sxy=sum(x*y');
sxx=sum(x*x');
b=(n*sxy-sx*sy)/(n*sxx-sx*sx);
a=(sy-b*sx)/n;
printf("value of a is=%f \n",a);
printf("value of b is=%f \n",b);
printf("equation is y=%f + %fx \n",a,b);
endfunction

Scilab Console:
x=[1,2,3,4,5,6];
y=[2,4,3,6,8,9];
curvefit1(x,y)
value of a is=0.333333
value of b is=1.428571
equation is y=0.333333 + 1.428571x

You might also like