0% found this document useful (0 votes)
92 views28 pages

Name. Kishankumar Goud Roll No. 37 Class. Fybsc - It Div. A: Numerical and Statistical Method

This document contains the code and output for several programming assignments completed by the student. It includes: 1) Four programs that perform basic math operations of addition, subtraction, multiplication and division on user-input numbers. 2) A program that accepts a mark from the user and displays the corresponding grade. 3) Programs that find the roots of quadratic and other equations using techniques like bisection method, Regula Falsi method, and Newton Raphson method. 4) Programs that perform numerical integration using techniques like trapezoidal rule, Simpson's 1/3rd rule and Simpson's 1/8th rule. 5) A program that solves a differential equation using Euler

Uploaded by

Kajal Goud
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)
92 views28 pages

Name. Kishankumar Goud Roll No. 37 Class. Fybsc - It Div. A: Numerical and Statistical Method

This document contains the code and output for several programming assignments completed by the student. It includes: 1) Four programs that perform basic math operations of addition, subtraction, multiplication and division on user-input numbers. 2) A program that accepts a mark from the user and displays the corresponding grade. 3) Programs that find the roots of quadratic and other equations using techniques like bisection method, Regula Falsi method, and Newton Raphson method. 4) Programs that perform numerical integration using techniques like trapezoidal rule, Simpson's 1/3rd rule and Simpson's 1/8th rule. 5) A program that solves a differential equation using Euler

Uploaded by

Kajal Goud
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/ 28

F.Y.B.SC. IT SEMESTER 2 ROLL NO.

37

Name. kishankumar Goud


Roll no. 37
Class. Fybsc.it
div. a

Practical no.1
Part-A

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

Aim: Write A Program In Scilab To Add, Sub, Multiply And Divide Two Numbers
Taken From Users.
Input:
A=10
B=20
C=A+B
disp("addition of the two numbers is",C)
x=40
y=20
z=x-y
disp(z)
p=10
q=40
r=p*q
disp(r)
e=20
f=40
g=e/f
disp(g)

output:

Practical no.1
Part-B
Aim: write a program in scilab to accept mark from user and display its grade using
switch case.
NUMERICAL AND STATISTICAL METHOD
F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

Input:
m=input("ENTER A MARK : ")
if(m<35)
c=1
end
if(m>=35) & (m<75)
c=2
end
if(m>=75) & (m<=100)
c=3
end
if(m>100)
c=4
end
select c
case 1 then disp("fail")
case 2 then disp("a grade")
case 3 then disp("o grade")
case 4 then disp("invalid marks")

end

output:

Practical no.1
Part-C
Aim. Write a program in scilab to find root of a quadratic equation.
Input:
NUMERICAL AND STATISTICAL METHOD
F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

a=input("enter a:")
b=input("enter b:")
c=input("enter c:")
if(a<=0)
disp("invalid value for a")
else
x=b^2-4*a*c
r1=(-b+sqrt(x))/(2*a)
r2=(-b-sqrt(x))/(2*a)
disp("the roots are::")
disp(r1)
disp(r2)
end

Output:

Practical no.1
Part-D
Aim: write a program in scilab to evaluate ex
Input:
NUMERICAL AND STATISTICAL METHOD
F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

x=input("enter x:")
n=input("enter n:")
sum=1
i=1
while(i<=n)
sum=sum+((x^i)/factorial(i))
i=i+1
end
disp("sum",sum)

output:

Practical no.2

part-A

Aim. Write a program in scilab to find root of an equation using bisection method.

program.

deff('y=f(x)',['y=x^3-x-4'])

x1=input("enter x1:")
x2=input("enter x2:")

if(f(x1)*f(x2)<0)
for i=1:1:8
x3=(x1+x2)/2
if(f(x1)*f(x3)<0)
x2=x3
else
x1=x3
end
disp(x3)
end
end

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

output:

Practical No.2

Part-B

Aim. Write A Program To Find A Root Of An Equation Using Regula Falsi Method

Program.

deff('y=f(x)',['y=x^3-4*x-9'])
x1=input("enter x1:")
x2=input("enter x2:")

if(f(x1)*f(x2)<0)
for i=1:1:8
x3=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1))
if(f(x1)*f(x3)<0)
x2=x3
else
x1=x3
end
disp(x3)
end
end

output:

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

Practical No.2

Part-C

Aim. Write A Program To Find A Root Of An Equation Using Newton Raphson Method.

Program:

deff('y=f(x)',['y=x^3-3*x+1'])
x1=input("enter x1:")

for i=1:1:5
x2=x1-(f(x1)/numderivative(f,x1))
disp(x2)
x1=x2
end

output:

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

Practical No.3

Part-A

Aim. Write A Program To Find Linear Interpolation

input:

c=[40 50 60 70 80;31 73 124 159 190]


x=input ("enter value to be interpolated:")
z=interpln(c,x)
disp(z)

output:

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

Practical No.3

Part-B

Aim. Write A Program To Find Newton Forward Difference Interpolation

input:

x=input("enter value for x:")


y=input("enter value for y:")
xg=input("enter value for interpolated:")
n=length(x)
h=x(2)-x(1)
u=(xg-x(1))/h
disp("forward difference table is")

for i=1:1:n-1
disp(diff(y,i))
end
yx=y(1)
p=u
for i=1:1:n-1

d=diff(y,i)

yx=yx+(p*d(1))

p=p*((u-1)/(i+1))

end

disp("yx value:",yx)

output:

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

Practical No.3

Part-C

Aim. Write A Program To Find Newton backward Difference Interpolation

input:

x=input("enter x value:")
y=input("enter y value:")
xg=input("enter value for interpolated:")
n=length(x)
h=x(2)-x(1)
u=(xg-x(n))/h
disp("backward difference table is")

for i=1:1:n-1
disp(diff(y,i))
end
yx=y(n)
p=u
for i=1:1:n-1

d=diff(y,i)

yx=yx+(p*d(n-i))

p=p*((u+i)/(i+1))

end

disp("yx value:",yx)

output:

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

Practical No.4

part-A

Aim. write a program in scilab to find the solution of simultaneous linear algebraic equation

using gauss Jordan method.

equation. x+y+z=9 , 6x-y+z=13 , 10x+y-z=19

program.

A=zeros(3,3)
for i=1:1:3
for j=1:1:3
v1=input("enter value for A matrix= ")
A(i,j)=v1
end
end
disp("A matrix=",A)

B=zeros(3,1)
for i=1:1:3
for j=1:1:1
v2=input("enter value for B matrix=")
B(i,j)=v2
end
end
disp("B matrix=",B)

Ai=inv(A)
X=Ai*B
disp("A inverse is=",Ai)
disp("By gauss jorden method",X)

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

disp("x=",X(1,1))
disp("y=",X(2,1))
disp("z=",X(3,1))

output.

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

Practical No.4

part-B

Aim. write a program in scilab to find the solution of simultaneous linear algebraic equation

using gauss seidel method.

equations. 7x-y+z=7.3 , -2x+8y+z=6.4 , x+2y+9z=13.6

program.

deff('y=f(x)',['7*x-y+z==7.3'])
deff('y=f(x)',['-2*x+8*y+z==6.4'])
deff('y=f(x)',['x+2*y+9*z==13.6'])
y=0
z=0
for i=1:1:5
x=(7.3+y-z)/7
y=(6.4+2*x-z)/8
z=(13.6-x-2*y)/9
//disp("x=",x)
//disp("y=",y)
//disp("z=",z)
printf("x=%f\n",x)
printf("y=%f\n",y)
printf("z=%f\n",z)
end

output.

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

practical no.5

part-A

aim. write a program in scilab to find the numerical integration using trapezoidal rule.

program.

deff('y=f(x)',['y=1/(1+x^5)'])
a=input("enter lower limit = ")
b=input("enter upper limit = ")
n=input("enter no of iterations = ")
h=(b-a)/n
add1=0
add2=0
disp("x values y values")
for i=0:1:n
x=a+i*h
y=f(x)
disp([x,y])
if ((i==0)|(i==n))
add1=add1+y

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

else
add2=add2+y
end
end
I=(h/2)*(add1+(2*add2))
disp("I=",I)

output.

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

Practical No.5

Part-B

Aim. Write The Program In Scilab To Find Numerical Integration Using Simpsons

1/3rd Rule.

input.

deff('y=f(x)',['y=1/(1+x^5)'])
a=input("enter lower limit=")
b=input("enter upper limit=")
n=input("enter number of interations=")
h=(b-a)/n
add1=0
add2=0
add3=0
disp("x values y values")
for i=0:1:n
x=a+i*h
y=f(x)
disp([x,y])
if((i==0)|(i==n))
add1=add1+y
else if(modulo(i,2)==0)
add2=add2+y
else
add3=add3+y
end
end
end
i=(h/3)*(add1+4*add3+2*add2)
disp("i=",i)

output.

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

Practical no.5
Part-c
aim. Write The Program In Scilab To Find Numerical Integration Using
Simpsons 1/8th Rule.

input.

deff('y=f(x)',['y=1/(1+x^5)'])
a=input("enter lower limit=")
b=input("enter upper limit=")
n=input("enter number of interations=")
h=(b-a)/n
add1=0
add2=0
add3=0
disp("x values y values")
for i=0:1:n
x=a+i*h
y=f(x)
disp([x,y])
if((i==0)|(i==n))
add1=add1+y
else if(modulo(i,3)==0)
add2=add2+y
else
add3=add3+y
end
end
end
i=(3*h/8)*(add1+3*add3+2*add2)
disp("i=",i)

output.

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

Practical no.6
Part-a
aim. Write The Program In Scilab To solve differential
equation using Euler’s method

Input.
deff('y=f(x,y)',['y=x+3*y'])
xo=input("enter initial value of xo: ")
yo=input("enter the value of yo: ")
h=input("enter the value of h: ")
xn=input("enter the final value of xn: ")
n=(xn-xo)/h
disp("x values y values")
disp([xo,yo])
for i=1:1:n
y1=yo+h*f(xo,yo)
x1=xo+h
disp([x1,y1])

yo=y1

xo=x1
end

Output.

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

Practical no.6
Part-B
Aim. Write The Program In Scilab To solve differential
equation using modified Euler’s method.

Program.
deff('y=f(x,y)',['y=x+y'])
xo=input("enter initial value of xo : ")
yo=input("enter initial value of yo : ")
h=input("enter the value of h: ")
xn=input("enter final value of xn: ")
n=(xn-xo)/h
printf("x values y values")
disp([xo,yo])
for i=1:1:n
y1=yo+h*f(xo,yo)
x1=xo+h
disp([x1,y1])
for j=1:1:3
ym1=yo+(h/2)*[f(xo,yo)+f(x1,y1)]
printf("modified val=%f\t",ym1)
y1=ym1
end
yo=ym1
xo=x1
end

Output.

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

Practical no.7
Part-a
Aim. Write a program in scilab for linear regression for the given
data set.
Program.
x=[1 2 3 4 6 8]
y=[2.4 3 3.6 4 5 6]
sum_x=sum(x)
sum_y=sum(y)
disp("summation of x=",sum_x)
disp("summation of y=",sum_y)
n=length(x)

for i=1:1:n
x2(i)=x(i)^2
end
sum_x2=sum(x2)
disp("sum of x squares=",sum_x2)

for i=1:1:n
xy(i)=x(i)*y(i)
end
sum_xy=sum(xy)
disp("sum of xy product =",sum_xy)
A=[n,sum_x; sum_x,sum_x2]

B=[sum_y; sum_xy]

Ai=inv(A)
X=Ai*B
disp("value of a and b is",X)
printf("y=%f+%fx",X(1,1),X(2,1))

Output.

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

Practical no.8
Part-a
Aim. Write a program in scilab to solve following question
using Binomial distribution
A coin is tossed 5 times, an event takes place such that
a) At least 2 times heads are shown
b) At most 3 tails are shown
c) Exactly 4 heads are shown

program.
a) At least 2 times heads are shown
n-input ("enter value of n=")
p=input("enter value of p=")
q=1-p
sumx=0
for x=2:1:n

ncx=factorial(n)/(factorial(x)*factorial(n-x))
px=ncx*p^x*q^(n-x)
sumx=sumx+px

end
disp(sumx)

output.

b) At most 3 tails are shown


n-input ("enter value of n=")

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

p=input("enter value of p=")


q=1-p
sumx=0
for x=0:1:2
ncx=factorial(n)/(factorial(x)*factorial(n-x))
px=ncx*p^x*q^(n-x)
sumx=sumx+px

end
disp(sumx)

output.

c) Exactly 4 heads are shown


n-input ("enter value of n=")
p=input("enter value of p=")
q=1-p
x=0
ncx=factorial(n)/(factorial(x)*factorial(n-x))
px=ncx*p^x*q^(n-x)

disp(px)

output.

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

Practical no.8
Part-b
Aim. Write a program in scilab to solve following
question using Poisson distribution
Monthly average no. of plane crash is 3.5. An event
takes place such that
a) There will be at least 2 such accidents in next month
b) Exactly 4 accidents
c) At most 3 accidents

program.
a) There will be at least 2 such accidents in next month
lam=3.5
sumx=0

for x=0:1:1
px=((exp(-lam)*(lam^x))/factorial(x))
sumx=sumx+px
end
pb=1-sumx

disp (pb)

output.

b) Exactly 4 accidents
NUMERICAL AND STATISTICAL METHOD
F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

output.

c) At most 3 accidents
sumx=0
lam=3.5
for x=0:1:3
px=((exp(-lam)*(lam^x))/factorial (x))
sumx=sumx+px
end

disp (sumx)

output.

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

Practical no.9
Part-A
AIM. Write a program in scilab to solve following question using Discrete
Uniform distribution Roll a six faced fair die. Calculate pmf of random variable
X, mean and variance using uniform distribution

INPUT.
n=6
pmf=1/n
disp("Pmf=",pmf)
mean=(n+1)/2
disp("mean", mean)
var=(n^2-1)/12

disp("variance", var)

OUTPUT.

NUMERICAL AND STATISTICAL METHOD


F.Y.B.SC. IT SEMESTER 2 ROLL NO.37

PRACTICAL NO.9
PRACTICE
AIM. Roll a six faced fair die. Suppose X denote the number appear on the top
of a die. The probability that the number appear on the top of the die is less than
3 is
P(X<3)=P(X=1)+P(X=2)
=1/6+1/6
=2/6
=0.3333

INPUT.
n=6
sumx=0
for x=1:1:2
pmf=1/n
sumx=sumx+pmf
end
disp(sumx)

OUTPUT.

NUMERICAL AND STATISTICAL METHOD

You might also like