0% found this document useful (0 votes)
71 views10 pages

Record Py

The document describes several Python programs for solving numerical techniques problems including linear equations, non-linear equations, integration, and differential equations using methods like Jacobi iteration, Gauss-Seidel, bisection, Newton-Raphson, trapezoidal rule, Simpson's rule, Euler's method, and Runge-Kutta method. It provides the code for each program along with sample input/output results.
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)
71 views10 pages

Record Py

The document describes several Python programs for solving numerical techniques problems including linear equations, non-linear equations, integration, and differential equations using methods like Jacobi iteration, Gauss-Seidel, bisection, Newton-Raphson, trapezoidal rule, Simpson's rule, Euler's method, and Runge-Kutta method. It provides the code for each program along with sample input/output results.
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/ 10

Lab 5 - Numerical Techniques 1.

Aim
a) Construct a python program for Jacobi Iteration method and solve the linear equation.
b) Construct a python program for Gauss Seidel method and solve the linear equation.

a) Program & Flowchart (Jacobi Iteration)

import numpy as np
a=np.array([[2,-1,2],[2,-1,1],[1,3,-1]])
b=np.array([6,3,4])

x=np.zeros(len(a[0]))
xnew=np.zeros(len(a[0]))
n=len(a[0])
iterlimit=10
err = 0.1

for iterate in range(iterlimit):


for i in range(n):
sum = 0
for j in range(n):
if (j!=i):
sum=sum + (a[i,j] * x[j])
xnew[i] = -(1/a[i,i]) * (sum - b[i])
diff = abs(x-xnew)
if (max(diff) < err):
break
x = list(xnew)
print(xnew)

print(iterate)
print(xnew)

RESULTS:
b) Program & Flowchart (Gauss Seidel)

import numpy as np
a=np.array([[2,-1,2],[2,-1,1],[1,3,-1]])
b=np.array([6,3,4])

x=np.zeros(len(a[0]))

n=len(a[0])
iterlimit=1000
Err = 0.1
y=list(x)
for iterate in range(iterlimit):
for i in range(n):
sum = 0
for j in range(n):
if (j!=i):
sum=sum + (a[i,j] * x[j])
x[i] = -(1/a[i,i]) * (sum - b[i])
diff = abs(x-y)
if (max(diff) < err):
break
y = list(x)
print x

print(iterate)
print(x)

RESULTS:
Lab 6 - Numerical Techniques 2.
Aim
a) Construct a python program for Bisection method and solve the non linear equation.
b) Construct a python program for Newton Raphson method and solve the non linear
equation.

a) Program & Flowchart (Bisection method)

import numpy as np
def func(x):
return (np.cos(x)-x)

x0,x1 = input("Enter the initial values").split()


x0 =float(x0)
x1 =float(x1)

e = input("Enter the required accuray")

y0 = func(x0)
y1 = func(x1)

if y0*y1>0:
print("Invalid values")
else:
while abs((x1-x0)/x1) >=e:
x2 =(x0+x1)/2
y2 = func(x2)
if y0*y2 <0:
x1 = x2
y1 = y2

else:
x0 = x2
y0 = y2

print("Root of the equation is", x2)

RESULTS:
b) Program & Flowchart (Newton Raphson)

import numpy as np
def func(x):
return ((x**2)-3*x+2)
def fund(x):
return (2*x-3)
x0 = input("Enter the initial value")
x0 =float(x0)

e = input("Enter the required accuray")


y0 = func(x0)
yd0 = fund(x0)

x1 = x0 - (y0/yd0)
print(x0,x1 )

while abs((x1-x0)/x1) >=e:


y0 = func(x1)
yd0 = fund(x1)
x2 = x1 - (y0/yd0)
x0 = x1
x1 = x2
print(x1,x2)
print(x2)

RESULTS:
Lab 7 - Numerical Techniques 3.
Aim
a) Construct a python program for Trapezoidal rule and solve the integral equation.
b) Construct a python program for Simpsons’s (1/3)rd rule and solve the integral equation.
c) Construct a python program for Simpsons’s (3/8)rd rule and solve the integral equation.

a) Program & Flowchart (Trapezoidal)

import numpy as np
def fun(x):
y = np.exp(x)*np.sin(x)
return y

x0,xn,n = input("Enter the limits and no. of intervals").split()


x0 =float(x0)
xn =float(xn)

sum1 = 0.0
sum2 = 0.0

h = (xn-x0)/n

y0 = fun(x0)

yn = fun(xn)

for i in range(1,n):

sum1 = sum1+fun(x0+(i*h))
print fun(x0+(i*h))

y = (h/2) * (y0+(2*sum1) +yn)

print(y)

RESULTS:
b) Program & Flowchart (Simpson’s 1/3)

import numpy as np
def fun(x):
y = np.exp(x)*np.sin(x)
return y

x0,xn,n = input("Enter the limits and no. of


intervals").split()
x0 =float(x0)
xn =float(xn)

s1 = 0.0
s2 = 0.0

h = (xn-x0)/n

y0 = fun(x0)

yn = fun(xn)

for i in range(1,n):
if i%2==0:
s1 = s1+fun(x0+(i*h))

else:
s2 = s2+fun(x0+(i*h))

y = (h/3) * (y0+(4*s2)+(2*s1) +yn)

print(y) y = (h/3) * (y0+(4*s2)+(2*s1) +yn)

RESULTS:
c) Program & Flowchart (Simpson’s 3/8)
import numpy as np
def fun(x):
y = np.exp(x)*np.sin(x)
return y

x0,x1,n = input("Enter the limits and no. of


intervals").split()

s1 = 0.0
s2 = 0.0

h = (x1-x0)/n
y0 = fun(x0)
yn = fun(x1)

for i in range(1,n):
if i%3==0:
s1 = s1+fun(x0+(i*h))

else:
s2 = s2+fun(x0+(i*h))

y = (3*h/8) * (y0+(3*s2)+(2*s1) +yn)

print(y)

y = (3*h/8) * (y0+(3*s2)+(2*s1) +yn)

RESULTS:
Lab 8 - Numerical Techniques 4.
Aim
a) Construct a python program for Euler’s method and solve the differential equation.
b) Construct a python program for Runge-Kutta method and solve the differential equation.

a) Program & Flowchart (Euler’s method)

def fun(y):
return (y**2-1)

x0,xn,y0,h= input("Enter x0, xn, y, stepsize").split()

while (x0<xn-h):
f = fun(y0)
y0 = y0+(h*f)
x0 = x0+h
print(x0,y0)

RESULTS:
b) Program & Flowchart (Runge-Kutta method)

def fun(x,y):
val = ((5*x*x) - y) / np.exp(x+y)
return(val)

x0,xn,h,y0=input("x0,xn,h,y0").split()

n=int((xn-x0)/h)

x=x0
y=y0

for i in range(n):
k1=h*fun(x,y)
k2=h*fun(x+(h/2.),y+(k1/2.))
k3=h*fun(x+(h/2.),y+(k2/2.))
k4=h*fun(x+h,y+k3)
y=y+((k1+(2*k2)+(2*k3)+k4)/6.)
x=x+h
print(x,y)

print(x,y)

RESULTS:
Lab 9 - Numerical Techniques 5.
Aim

a) Construct a python program to solve free fall of a body using Euler’s method
b) Construct a python program to solve simple harmonic motion using Euler’s method

a) Program & Flowchart (Free fall)

y0,t0,tn,v0,n = input("Enter initial position,velocity,


time,no. of intervals and final time").split()

g=9.8

h = (tn-t0)/n

while (t0<tn-h):

y0 = y0+(h*v0)
v0 = v0+(h*g)
t0 = t0+h
print(t0,y0,v0)

RESULTS:

b) Program & Flowchart (SHM)

x0,tn,t0,v0,n = input("Enter initial values").split()


h = (tn-t0)/n
k=1
m=1
while(t0<tn-h):
a=(-k/m)*x0
v0 = v0+(h*a)
x0 = x0+(h*v0)
t0 = t0+h
print(t0,x0,v0,a)

RESULTS:

You might also like