Mathematics - II for CSE stream: Lab Component
Instructions for Lab IA Test
Before the Test:
1. Reporting Time:
Be present at least 10 minutes before the scheduled start of the test.
Batch 2 and Batch 3 students must be ready at least 30 minutes before their allotted time.
2. Materials Required:
Bring your Lab IA answer booklet with the cover page duly filled.
Ensure you have your laptop in working condition.
Test Procedure:
1. Program Selection:
Each student must randomly pick a lot containing two programs, one from Part A and one from Part B.
Refer to the program list provided below.
2. Write-Up:
Complete the write-up parts for both programs within 30 minutes.
Get your write-up validated by the examiner(s) before proceeding to execution.
3.Execution:
Code and execute the selected programs after validation.
Ensure proper outputs and error handling before submission.
4.Misconduct:
Any form of malpractice or misconduct will be taken seriously.
Method of Evaluation
1. Marks Distribution:
The Lab IA Test is conducted for 50 marks, which will be proportionally reduced to 10 marks for CIE.
The final Lab Assessment for CIE is 25 marks, split as:
DAM (15 marks)
Lab IA (10 marks)
2. Breakdown of Lab IA 50 Marks:
25 marks per program, distributed as:
Write-Up: 10 marks
Execution: 10 marks
Viva Voce: 5 marks
3. Change of Question:
A provision for changing the question is available if required.
The revised program will be evaluated for a maximum of 15 marks only.
4. Eligibility for SEE:
A student must score a minimum of 20 marks in Lab IA to become eligible for the Semester End Examination (SEE).
Programs with short code:
Part A
𝑥 2 𝑦2 𝑎 𝑏𝑎 √𝑎2−𝑥2
1. Find the area of an ellipse
𝑎2 + 𝑏2 = 1 by double integration [Take 𝑎 = 3 and 𝑏 = 2]. Hint: 𝐴 = 4 ∫0 ∫0 𝑑𝑦𝑑𝑥
In [1]: 1 from sympy import *
2 x,y=symbols('x y')
3 a=3
4 b=2
5 A=4*integrate(1,(y,0,(b/a)*sqrt(a**2-x**2)),(x,0,a))
6 display(A)
6.0𝜋
2. Find the area enclosed by the cardioid 𝑟 = 𝑎(1 + cos𝜃) above the initial line.
In [2]: 1 from sympy import *
2 r,t,a=symbols('r t a')
3 A=integrate(r,(r,0,a*(1+cos(t))),(t,0,pi))
4 display(A)
3𝜋𝑎2
4
𝑥 + 𝑦 + 𝑧 = 1, by using double
3. Find the volume of the tetrahendron bounded by the coordinate planes and the plane
integration.
𝑎 𝑏 𝑐
𝑉 = ∫ ∫ 𝑎 𝑐 (1 − 𝑥𝑎 − 𝑦𝑏 ) 𝑑𝑦𝑑𝑥
𝑎 𝑏(1− 𝑥 )
Hint:
0 0
In [3]: 1 from sympy import *
2 x,y,a,b,c=symbols('x y a b c')
3 V=integrate(c*(1-x/a-y/b),(y,0,b*(1-x/a)),(x,0,a))
4 display(V)
𝑎𝑏𝑐
6
4. Find gradient of 𝜙 = 𝑥2 𝑦𝑧
In [4]: 1 from sympy.vector import *
2 from sympy import symbols
3 N=CoordSys3D('N') #Setting the coordinate system
4 x,y,z=symbols('x y z')
5 A=N.x**2*N.y*N.z
6 print(f'\n Gradient of [A] is \n')
7 display(gradient(A))
Gradient of [A] is
(2𝐱𝐍 𝐲𝐍 𝐳𝐍 )𝐢𝐍̂ + (𝐱𝐍 2 𝐳𝐍 )𝐣𝐍̂ + (𝐱𝐍 2 𝐲𝐍 )𝐤𝐍̂
5. Find div (𝐹 )⃗ and curl (𝐹 )⃗ 𝐹 ⃗ = 𝑦2 ̂ + 2𝑥2𝑦𝑧 ̂ − 3𝑦 2𝑘̂
, if 𝑥 𝑖 𝑗 𝑧
In [5]: 1 from sympy.vector import *
2 from sympy import symbols
3 N=CoordSys3D('N')
4 x,y,z=symbols('x y z')
5 A=(N.x*N.y**2)*N.i+(2*N.x**2*N.y*N.z)*N.j-(3*N.y*N.z**2)*N.k
6 print(f"\n Divergence of {A} is \n")
7 display(divergence(A))
8 print(f"\n Curl of {A} is \n")
9 display(curl(A))
Divergence of N.x*N.y**2*N.i + 2*N.x**2*N.y*N.z*N.j + (-3*N.y*N.z**2)*N.k is
2𝐱𝐍 2 𝐳𝐍 + 𝐲𝐍 2 − 6𝐲𝐍 𝐳𝐍
Curl of N.x*N.y**2*N.i + 2*N.x**2*N.y*N.z*N.j + (-3*N.y*N.z**2)*N.k is
(−2𝐱𝐍 2 𝐲𝐍 − 3𝐳𝐍 2 )𝐢𝐍̂ + (4𝐱𝐍 𝐲𝐍 𝐳𝐍 − 2𝐱𝐍 𝐲𝐍 )𝐤𝐍̂
Part B
1. Verify that 𝛽(𝑚,𝑛) = Γ(𝑚)Γ(𝑛)
Γ(𝑚+𝑛) for m=5 and n=7 using inbuilt functions.
In [6]: 1 from sympy import beta,gamma
2 m=5;
3 n=7;
4 m=float(m);
5 n=float(n);
6 b=beta(m,n);
7 g=(gamma(m)*gamma(n))/gamma(m+n);
8 print(b,g)
9 if (abs(b-g)<=0.00001):
10 print('beta and gamma are related')
11 else:
12 print('given values are wrong')
0.000432900432900433 0.000432900432900433
beta and gamma are related
𝑑𝑦
𝑑𝑥 − 2𝑦 = 3𝑒 with 𝑦(0) = 0 using Taylor series method find 𝑦 at 𝑥 = 0.1,0.2,0.3 up to 4th degree terms.
2. Solve: 𝑥
In [7]: 1 from numpy import *
2 def taylor( deriv , x , y , xStop , h ):
3 X = []
4 Y = []
5 X.append( x )
6 Y.append( y )
7 while x < xStop:
8 D = deriv( x , y )
9 H = 1.0
10 for j in range( 4 ):
11 H = H*h/(j+1)
12 y = y + D[j]*H
13 x = x + h
14 X.append( x )
15 Y.append( y )
16 return array(X) , array(Y)
17 def deriv( x , y ):
18 D = zeros(( 4 , 1 ) )
19 D[0] = 3*exp(x) + 2*y[0]
20 D[1] = 3*exp(x) + 2*D[0]
21 D[2] = 3*exp(x) + 2*D[1]
22 D[3] = 3*exp(x) + 2*D[2]
23 return D
24 x = 0.0
25 xStop = 0.3
26 y = array([0.0])
27 h = 0.1
28 X,Y = taylor( deriv , x , y , xStop , h )
29 print("The required values are:")
30 for i in range(len(X)):
31 print("At x=%0.2f , y=%0.5f " %( X[i] , Y[i] ) )
The required values are:
At x=0.00 , y=0.00000
At x=0.10 , y=0.34869
At x=0.20 , y=0.81125
At x=0.30 , y=1.41674
3. Using modified Euler’s method, find an approximate value of
𝑑𝑦 = 𝑥 + 𝑦 , and 𝑦 = 1 when
𝑦 when 𝑥 = 0.2, given that 𝑑𝑥
𝑥 = 0 taking ℎ = 0.1
In [8]: 1 from sympy import *
2 def Modi_Euler( g , x0 , h , y0 , xn ):
3 x , y = symbols('x,y')
4 f = lambdify( [x , y] , g )
5 xt = x0 + h
6 Y = [y0]
7 while xt <= xn:
8 k1 = h * f( x0 , y0 )
9 k2 = h * f( x0 + h , y0 + k1 )
10 y1 = y0 + 1/2 * ( k1 + k2 )
11 Y.append( y1 )
12 print('y(%3.2f'%xt,') is %3.5f'%y1)
13 x0 = xt
14 y0 = y1
15 xt = xt + h
16 return Y
17 Y = Modi_Euler( 'x+y' , 0 , 0.1 , 1 , 0.2 )
y(0.10 ) is 1.11000
y(0.20 ) is 1.24205
𝑑𝑦 𝑦
4. Apply the Runge Kutta method to find the solution of
𝑑𝑥 = 1 + 𝑥 at 𝑦(2) taking ℎ = 0.2 , Given that 𝑦(1) = 2
In [9]: 1 from sympy import *
2 def RungeKutta( g , x0 , h , y0 , xn ):
3 x , y = symbols('x,y')
4 f = lambdify( [x , y] , g )
5 xt = x0 + h
6 Y = [y0]
7 while xt <= xn:
8 k1 = h * f( x0 , y0 )
9 k2 = h * f( x0 + h/2 , y0 + k1/2 )
10 k3 = h * f( x0 + h/2 , y0 + k2/2 )
11 k4 = h * f( x0 + h , y0 + k3 )
12 y1 = y0 + 1/6 * ( k1 + 2*k2 + 2*k3 + k4 )
13 Y.append( y1 )
14 print('y(%3.4f'%xt,') is %3.4f'%y1)
15 x0 = xt
16 y0 = y1
17 xt = xt + h
18 return Y
19 Y = RungeKutta( '1+y/x' , 1 , 0.2 , 2 , 2 )
y(1.2000 ) is 2.6188
y(1.4000 ) is 3.2710
y(1.6000 ) is 3.9520
y(1.8000 ) is 4.6580
y(2.0000 ) is 5.3863
𝑑𝑦 = 𝑥2 + 𝑦 at 𝑦(1.4) , Given that 𝑦(1) = 2 , 𝑦(1.1) = 2.2156 ,
𝑑𝑥
𝑦(1.2) = 2.4649 , 𝑦(1.3) = 2.7514. Use corrector formula thrice. 2
5. Apply Milne’s predictor and corrector method to solve
In [10]: 1 from sympy import *
2 def Milne( g , x0 , h , y0 , y1 , y2 , y3 ):
3 x , y = symbols('x,y')
4 f = lambdify( [x , y] , g )
5 x1 = x0 + h
6 x2 = x1 + h
7 x3 = x2 + h
8 x4 = x3 + h
9 y10 = f( x0 , y0 )
10 y11 = f( x1 , y1 )
11 y12 = f( x2 , y2 )
12 y13 = f( x3 , y3 )
13 y4p = y0 + 4*h/3 * ( 2*y11 - y12 + 2*y13 )
14 print('predicted value of y4: %3.4f' %y4p )
15 y14 = f( x4 , y4p )
16 for i in range(1 , 4):
17 y4 = y2 + h/3 * ( y14 + 4*y13 + y12 )
18 print('corrected value of y4 at iteration %d:'%i, '%3.4f'%y4 )
19 y14 = f( x4 , y4 )
20 Milne( 'x**2+y/2' , 1 , 0.1 , 2 , 2.2156 , 2.4649 , 2.7514 )
predicted value of y4: 3.0793
corrected value of y4 at iteration 1: 3.0794
corrected value of y4 at iteration 2: 3.0794
corrected value of y4 at iteration 3: 3.0794