Record Py
Record Py
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.
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
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.
import numpy as np
def func(x):
return (np.cos(x)-x)
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
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)
x1 = x0 - (y0/yd0)
print(x0,x1 )
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.
import numpy as np
def fun(x):
y = np.exp(x)*np.sin(x)
return y
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))
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
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))
RESULTS:
c) Program & Flowchart (Simpson’s 3/8)
import numpy as np
def fun(x):
y = np.exp(x)*np.sin(x)
return y
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))
print(y)
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.
def fun(y):
return (y**2-1)
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
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:
RESULTS: