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

E19148 - EM 203 Lab Submission 2

The document contains code for 4 questions that demonstrate operations with NumPy and plotting functions. Question 1 defines matrices and performs subsetting. Question 2 plots cosine and sine waves in 3D. Question 3 models and plots the position and velocities of a two-body system. Question 4 calculates a nested summation using a for loop.

Uploaded by

sajeevanrs1216
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

E19148 - EM 203 Lab Submission 2

The document contains code for 4 questions that demonstrate operations with NumPy and plotting functions. Question 1 defines matrices and performs subsetting. Question 2 plots cosine and sine waves in 3D. Question 3 models and plots the position and velocities of a two-body system. Question 4 calculates a nested summation using a for loop.

Uploaded by

sajeevanrs1216
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Janani P.

E19148
EM203
Lab class 02

Question 1
import numpy as np
#main matrix
A=np.array([[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18],[19,20,21,22,23,24],
[25,26,27,28,29,30]]).reshape(5,6)
print(A)
#sub matrix
B=np.array([A[0:1,1:5],A[2:3,1:5],A[4:,1:5]]).reshape(3,4)
print (B)
Question 2
import numpy as np
import matplotlib.pyplot as
from mpl_toolkits import mplot3d
ax = plt.axes(projection='3d')
# Range of x
x = np.arange(0, 20*np.pi, 0.001)
# Functions
p= np.cos(x)
q = np.sin(x)
#plotting,Axis labelling and Tittle
ax.plot(x, p, q)
plt.xlabel('X values 0-20π')
plt.ylabel('Cos(x)')
ax.set_zlabel('Sin(x)'
plt.title('Cos(x) & Sin(x) in 3D')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits
import mplot3d
ax = plt.axes(projection='3d')
#Range of x
x = np.arange(0, 20*np.pi, np.pi/100)
p = np.cos(x)
q = np.sin(x)
#Functions
Z1 = x * p
Z2 = x * q
#plotting,Axis, labelling and tittle
ax.plot(x, p, Z1, label='xCos(x)')
ax.plot(x, q, Z2, label='xSin(x)')
plt.xlabel('X values 0-20π')
plt.ylabel('Sin & Cos Functions')
ax.set_zlabel('xSin(x) & xCos(x) Functions')
plt.title('xCos(x) & xSin(x) in 3D')
plt.legend()
plt.show()
Question3
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy.integrate import odeint
def twoBody(y, t, mu):
r = np.sqrt(y[0]**2 + y[1]**2 + y[2]**2)
ydot = np.empty((6,))
ydot[0] = y[3]
ydot[1] = y[4]
ydot[2] = y[5]
ydot[3] = (-mu/(r**3))*y[0]
ydot[4] = (-mu/(r**3))*y[1]
ydot[5] = (-mu/(r**3))*y[2]
return ydot
# In m and m/s
# first three are the (x, y, z) position
# second three are the velocities in those same directions respectively
Y0 = np.array([-5614924.5443320004,
-2014046.755686,
2471050.0114869997,
-673.03650300000004,
582.41158099999996,
1247.7034980000001])
mu = 3.986004418 * 10**14
solution = odeint(twoBody, Y0, np.linspace(0., 351., 100), args=(mu, ))

fig=plt.figure()
graph=fig.add_subplot(1,2,1,projection="3d")
x=np.linspace(0,351,100)
plt.title("position")
graph.plot(solution[:,0],solution[:,1],solution[:,2])

graph=fig.add_subplot(1,2,2,projection="3d")
plt.title("velocities")
graph.plot(solution[:,3],solution[:,4],solution[:,5])
plt.show()
Question 4
n=int(input('Enter value for n='))
x=1
m=0
for i in range(1,n+1):
m=0
for j in range(1,i+1):
m+=j*j*j
x*=m
print(x)
If n=25

You might also like