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

Lab Programs

Uploaded by

manjunatha2055
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab Programs

Uploaded by

manjunatha2055
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

>>>>>>>>>>>>gradient

from sympy.physics.vector import *


from sympy import var
var('x, y, z')
v = ReferenceFrame('v')
F = v[0]**2 * v[1] * v[2]
G = gradient(F, v)
F = F.subs({v[0]: x, v[1]: y, v[2]: z})
print("Given scalar function F=")
display(F)
G = G.subs({v[0]: x, v[1]: y, v[2]: z})
print("Gradient of F=")
display(G)

>>>>>>>>>>Curl
from sympy.physics.vector import *
from sympy import var
var('x, y, z')
v = ReferenceFrame('v')
F = v[0]*v[1]**2*v.x + 2*v[0]**2*v[1]*v[2]*v.y - 3*v[1]*v[2]**2*v.z
G = curl(F, v)
F = F.subs({v[0]: x, v[1]: y, v[2]: z})
print("Given vector point function is")
display(F)
G = G.subs({v[0]: x, v[1]: y, v[2]: z})
print("curl of F=")
display(G)

>>>>>>>>>>>>Tylore series
import numpy as np
from sympy import exp
x0 = 0
y0 = 0
h = 0.1
x1 = x0 + h
D = np.zeros((4, 1))
D[0] = [2 * y0 + 3 * exp(x0)]
D[1] = [4 * y0 + 9 * exp(x0)]
D[2] = [8 * y0 + 21 * exp(x0)]
D[3] = [16 * y0 + 45 * exp(x0)]
print("D matrix:")
print(D)
y = y0 + D[0] * h + (D[1] * h**2) / 2 + (D[2] * h**3) / 6 + (D[3] * h**4) / 24
print(f"The value of y at given x is y({h})=",y)
>>>>>>>>>>RK Methode
import numpy as np
def f(x, y):
return 3 * x + (y / 2)
y0 = 1
x0 = 0
h = 0.2
x1 = x0 + h
k1 = h * f(x0, y0)
k2 = h * f(x0 + (h / 2), y0 + (k1 / 2))
k3 = h * f(x0 + (h / 2), y0 + (k2 / 2))
k4 = h * f(x0 + h, y0 + k3)
print("k1=%.4f \t k2=%.4f \t k3=%.4f \t k4=%.4f" % (k1, k2, k3, k4))
y = y0 + (1 / 6) * (k1 + 2 * k2 + 2 * k3 + k4)
print("The value of y at given x is: %.4f" % y)

>>>>>>>>>Find the area of the ellipse 𝑥2𝑎2+𝑦2𝑏2=1


from sympy import *
x= Symbol ('x')
y= Symbol ('y')
a=4
b=6
w3=4* integrate (1 ,(y,0 ,(b/a)* sqrt (a ** 2-x ** 2)) ,(x,0,a))
print (w3)

>>>>>>>>>>> Find the volume of the tetrahedron bounded by the planes x=0, y=0 and z=0, 𝑥𝑎+𝑦𝑏+𝑧𝑐=1
from sympy import *
x= Symbol ('x')
y= Symbol ('y')
z= Symbol ('z')
a= Symbol ('a')
b= Symbol ('b')
c= Symbol ('c')
w2= integrate (1 ,(z,0,c*(1-x/a-y/b)) ,(y,0,b*(1-x/a)) ,(x,0,a))
print (w2)

𝒐𝒐
>>>>>>>>>>>>1) Evaluate ∫𝟎 𝒆 ^ − 𝒙dx
from sympy import *
x= symbols ('x')
w1= integrate (exp (-x) ,(x,0, float ('inf ')))
print ( simplify (w1))
>>>>>>>>>> Calculate Beta(5/2,7/2) and Gamma(5/2).
from sympy import beta , gamma
m= float ( input ('m : '));
n= float ( input ('n :'));
s= beta (m,n);
t= gamma (n)
print ('gamma (',n,') is %3.3f '%t)
print ('Beta (',m,n,') is %3.3f '%s)

>>>>>>>>Find a root of the equation 3x = cos x+ 1, near 1, by Newton Raphson method. Perform 5 iterations
from sympy import *
x= Symbol ('x')
g = input ('Enter the function ')
f= lambdify (x , g )
dg = diff ( g ) ;
df= lambdify (x , dg )
x0= float ( input ('Enter the initial approximation ') ) ;
n= int( input ('Enter the number of iterations ') ) ;
for i in range (1 , n+1 ):
x1 =( x0 - ( f ( x0 )/df ( x0 ) ) )
print ('iteration %d \t the root %0.3f \t function value %0.3f \n'%(i , x1 , f ( x1 ) ) ) ;
x0 = x1

>>>>>>>>>>Simpson's 1/3 Rule


def f(x):
return 1 / (1 + x**2)
# Get user input for limits and number of iterations
a = float(input("Enter lower limit of a: "))
b = float(input("Enter upper limit of b: "))
n = int(input("Enter no of iterations: "))
def simp(x0, xn, n):
h = (xn - x0) / n
inte = f(x0) + f(xn)
for i in range(1, n):
k = x0 + i * h
if i % 2 == 0:
inte = inte + 2 * f(k)
else:
inte = inte + 4 * f(k)
inte = inte * h / 3
return inte
result = simp(a, b, n)
print("The Approximate value of integral: %0.4f" % result)
>>>>>>>>>Simpson's 3/8 Rule
def f(x):
return 1 / (1 + x**2)
a = float(input("Enter lower limit of a: "))
b = float(input("Enter upper limit of b: "))
n = int(input("Enter no of iterations: "))
def simp(x0, xn, n):
h = (xn - x0) / n
inte = f(x0) + f(xn)
for i in range(1, n):
k = x0 + i * h
if i % 3 == 0:
inte = inte + 2 * f(k)
else:
inte = inte + 3 * f(k)
inte = inte * (3 * h / 8)
return inte
result = simp(a, b, n)
print("The Approximate value of integral: %0.4f" % result)

You might also like