Mathematics Lab
Mathematics Lab
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
m : 3
n : 5
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
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 ))
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)
curl of F=
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
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 ()
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 ) ;
In [ ]: