0% found this document useful (0 votes)
74 views6 pages

Mathematics Lab

Pdf file

Uploaded by

poojarideeksha16
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)
74 views6 pages

Mathematics Lab

Pdf file

Uploaded by

poojarideeksha16
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

In [2]:

#1.Prove that R R(x2+y2)dydx =R R(x2+y2)dxdy


from sympy import *
x=Symbol ( 'x')
y=Symbol ( 'y')
z=Symbol ( 'z')
w3 =integrate (x ** 2+y** 2 , y ,x )
pprint ( w3 )
w4 =integrate (x ** 2+y** 2 , x ,y )
pprint ( w4 )

3 3
x ⋅y x⋅y
──── + ────
3 3
3 3
x ⋅y x⋅y
──── + ────
3 3

In [3]:
#2.Find the area of the cardioid r=a(1 + cosθ) by double integration
from sympy import *
r=Symbol ( 'r')
t=Symbol ( 't')
a=Symbol ( 'a')
#a=4
w3 =2*integrate ( r ,( r ,0 , a*(1+cos (t ))) ,(t ,0 , pi ) )
pprint ( w3 )

2
3⋅π⋅a
──────
2

In [4]:
#3 Find the volume of the tetrahedron bounded by the planes x=0,y=0 and z=0
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
print ( w2 )

a*b*c/6

In [6]: #4 Find Beta(3,5), Gamma(5)


from sympy import beta , gamma
m= input ('m : ');
n= input ('n : ');
m= float (m);n= float (n);
s=beta(m , n);
t=gamma ( n)
print ('gamma( ',n , ') is %3.3f '%t )
print ('Beta (',m ,n , ') is %3.3f '%s )

m : 3
n : 5

gamma( 5.0 ) is 24.000


Beta ( 3.0 5.0 ) is 0.010

In [8]:
#5 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 )

m : 2.5
n : 3.5

gamma( 3.5 ) is 3.323


Beta ( 2.5 3.5 ) is 0.037

In [9]: # To find gradient of scalar point function .


# 6. To find gradient of ϕ=x2y+ 2xz −4.
from sympy . vector import *
from sympy import symbols
N=CoordSys3D ( 'N')# Setting the coordina t e system
x,y,z=symbols('x y z')
A=N.x** 2*N.y+2*N.x*N.z-4# Variab le s x ,y , z to be u sed with c oordinat
delop =Del () # Del operator
display ( delop ( A)) # Del operator applied to A
gradA =gradient ( A) # Gradient function is used
print ( f" \ n Gradient of {A}is \n" )
display ( gradA )

(Derivative(N.x**2*N.y + 2*N.x*N.z - 4, N.x))*N.i + (Derivative(N.x**2*N.y + 2*N.x*N.z - 4,


N.y))*N.j + (Derivative(N.x**2*N.y + 2*N.x*N.z - 4, N.z))*N.k

\ n Gradient of N.x**2*N.y + 2*N.x*N.z - 4is

(2*N.x*N.y + 2*N.z)*N.i + N.x**2*N.j + 2*N.x*N.k

In [10]:
#7.To find divergence of F=x2yzˆi+y2zxˆj+z2xyˆ
from sympy.vector import *
from sympy import symbols
N=CoordSys3D( 'N')
x ,y , z=symbols('x y z')
A=N.x** 2*N.y*N.z*N.i+N.y** 2*N.z*N.x*N.j+N.z** 2*N.x*N.y*N.k
delop =Del ()
divA=delop . dot (A)
display ( divA )
print ( f" \n Divergence of {A}is \n" )
display ( divergence (A ))

Derivative(N.x*N.y*N.z**2, N.z) + Derivative(N.x*N.y**2*N.z, N.y) +


Derivative(N.x**2*N.y*N.z, N.x)
Divergence of N.x**2*N.y*N.z*N.i + N.x*N.y**2*N.z*N.j + N.x*N.y*N.z**2*N.kis

6*N.x*N.y*N.z

In [11]:
# 8.To f ind curl of F= xy ^ 2i + 2x ^ 2yzj - 3yz ^ 2k
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)

Given vector point function is

x*y**2*v.x + 2*x**2*y*z*v.y - 3*y*z**2*v.z

curl of F=

(-2*x**2*y - 3*z**2)*v.x + (4*x*y*z - 2*x*y)*v.z

In [12]:
# 9 Solvedx −2y= 3ex with y(0) = 0 using Taylor series method at x= 0.1(0.1

import numpy as np
from numpy import array

def taylor(deriv, x, y, xStop, h):


X = []
Y = []
X.append(x)
Y.append(y)
while x < xStop:
D = deriv(x, y) # Derivatives of y
H = 1.0
for j in range(3): # Build Taylor series
H = H * h / (j + 1)
y = y + D[j] * H # H = h^j/j!
x = x + h
X.append(x) # Append results to lists X and Y
Y.append(y)
return array(X), array(Y) # Convert lists into arrays

def deriv(x, y):


D = np.zeros((4, 1))
D[0] = [2 * y[0] + 3 * np.exp(x)]
D[1] = [4 * y[0] + 9 * np.exp(x)]
D[2] = [8 * y[0] + 21 * np.exp(x)]
D[3] = [16 * y[0] + 45 * np.exp(x)]
return D
x = 0.0 # Initial value of x
xStop = 0.3 # last value
y = array([0.0]) # Initial values of y
h = 0.1 # Step size
X, Y = taylor(deriv, x, y, xStop, h)

print("The required values are:")


for i in range(len(X)):
print(f"at x = {X[i]:.2f}, y = {Y[i][0]:.5f}")

The required values are:


at x = 0.00, y = 0.00000
at x = 0.10, y = 0.34850
at x = 0.20, y = 0.81079
at x = 0.30, y = 1.41590

In [13]:
# 10 Solve y′=−ky with y(0) = 100 using modified Euler’s method at x= 100,
import numpy as np
import matplotlib . pyplot as plt
def modified_euler (f , x0 , y0 , h , n) :
x=np . zeros (n+1)
y=np . zeros (n+1)
x[0] = x0
y[0] = y0
for i in range (n):
x[i+1] = x[i] + h
k1 =h*f(x[i], y [i])
k2 =h*f(x[i+1], y [i] + k1 )
y[i+1] = y[i] + 0.5 *( k1 +k2 )
return x,y
def f (x , y ):
return -0.01 *y# OD E dy / dx = - ky
x0 =0.0
y0 =100.0
h=25
n=4
x , y =modified_euler(f, x0 , y0 , h, n )
print (" The required value at x= %0.2f , y= %0.5f "%(x[4],y [4]) )
print ("\n\n")
# Plotting the resultsplt .
plt.plot( x , y , 'bo-')
plt . xlabel('x')
plt . ylabel('y')
plt . title ( 'Solution of dy / dx = - ky using Modified Euler \'s Method')
plt . grid(True)
plt . show ()

The required value at x= 100.00 , y= 37.25290


In [14]:
# 11 Apply the Runge Kutta method to find the solution of dy/dx = 1 + (y/x)
#takingh= 0.2. Given that y(1) = 2.
from sympy import *
import numpy as np
def RungeKutta (g ,x0 ,h , y0 , xn ):
x , y=symbols('x ,y ')
f=lambdify([x , y ],g )
xt =x0 +h
Y= [ y0 ]
while xt <= xn :
k1 =h*f ( x0 , y0 )
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)
y1 =y0 +(1/6 )*( k1 +2*k2 +2*k3 +k4 )
Y.append( y1 )
# print ( 'y(%3 . 3f '%xt , ') is %3 . 3f '%y1 )
x0 =xt
y0 =y1
xt =xt +h
return np . round (Y ,2 )
RungeKutta( '1 +( y / x) ',1 , 0.2 , 2 ,2 )

Out [14]: array([2. , 2.62, 3.27, 3.95, 4.66, 5.39])

In [15]:
#12 Apply Milne’s predictor and corrector method to solve dy/dx =x2+ (y/2)
#Given that y(1)=2, y(1.1)=2.2156, y(1.2)=2.4649, y(1.3)=2.7514. Use correc
x0 =1
y0 =2
y1 =2.2156
y2 =2.4649
y3 =2.7514
h=0.1
x1 =x0 +h
x2 =x1 +h
x3 =x2 +h
x4 =x3 +h
def f (x ,y ):
return x** 2+(y/2)
y10 =f ( x0 , y0 )
y11 =f ( x1 , y1 )
y12 =f ( x2 , y2 )
y13 =f ( x3 , y3 )
y4p =y0 +(4*h/3 ) *(2 *y11-y12+2*y13 )
print ('predicted value of y4 is %3.3f '%(y4p))
y14 =f (x4 , y4p ) ;
for i in range (1 , 4) :
y4 =y2 +(h/3 )*( y14 +4*y13 +y12 ) ;
print ('corrected value of y4 after \t iteration %d is \t %3.5f \t '%(i
y14=f (x4 , y4 ) ;

predicted value of y4 is 3.079


corrected value of y4 after iteration 1 is 3.07940
corrected value of y4 after iteration 2 is 3.07940
corrected value of y4 after iteration 3 is 3.07940

In [ ]:

You might also like