#Angles Are in Degrees: EGR2313 HW SOLUTIONS (2021)
#Angles Are in Degrees: EGR2313 HW SOLUTIONS (2021)
HW1
#%% QUESTION 1
y = float(input("ENTER ANGLE IN DEGREES : " )) #ANGLES ARE IN DEGREES
x =( y*(3.142/180)) #ANGLES ARE CONVERTED FROM DEGREE TO RADIAN
a=(x-((x**3)/(3*2))+((x**5)/(5*4*3*2))-((x**7)/(7*6*5*4*3*2)))
b=(1-((x**2)/2)+((x**4)/(4*3*2))-((x**6)/(6*5*4*3*2)))
# a and b above are trig. functions in power series
sin = a
print("The SIN of angle " + str(y) + " is: " + str(sin))
cos = b
print("The COS of angle " + str(y) + " is: " + str(cos))
tan = a/b
print("The TAN of angle " + str(y) + " is: " + str(tan))
cot = b/a
print("The COT of angle " + str(y) + " is: " + str(cot))
sec = 1/b
print("The SEC of angle " + str(y) + " is: " + str(sec))
cosec = 1/a
print("The COSEC of angle " + str(y) + " is: " + str(cosec))
#%% QUESTION 2
import numpy as np
A = np.eye(3)*3
print =(A)
# 2(ii)
B = np.ones((3,3),float)*0.33
print = (B)
# 2(iii)
C = np.exp([[1,0,4],[2,3,6],[-1,0,-6]])
print = (C)
#%% QUESTION 3
import numpy as np
import matplotlib.pyplot as plt
d = np.linspace(0,2*np.pi,100)
x =(16*np.sin(d)**3)
y =((13*np.cos(d))-(5*np.cos(2*d))-(2*np.cos(3*d))-(np.cos(4*d)))
plt.plot(x,y)
plt.xlabel("X-AXIS")
plt.ylabel("Y-AXIS")
plt.show()
Page 1 of 4
HW2
#%% QUESTION 1
a=float(input("ENTER ANGLE: ")) #ALL ANGLES ARE IN DEGREE
x=a*((3.142)/(180)) #CONVERSION OF ANGLES FROM DEGREE TO RADIAN
sin=x - ((x**3)/(6)) + ((x**5)/(120)) - ((x**7)/(5040))
cos=1 - ((x**2)/(2)) + ((x**4)/(24)) - ((x**6)/(720))
print("The SIN of angle " + str(a) + " is: " + str(sin))
print("The COS of angle " + str(a) + " is: " + str(cos))
tan=sin/cos
print("The TAN of angle " + str(a) + " is: " + str(tan))
if (a==0):
print("The COT of angle 0.0 is: undefined")
print("The SEC of angle 0.0 is: undefined")
print("The COSEC of angle 0.0 is: undefined")
else:
cot=cos/sin
print("The COT of angle " + str(a) + " is: " + str(cot))
sec=1/cos
print("The SEC of angle " + str(a) + " is: " + str(sec))
cosec=1/sin
print("The COSEC of angle " + str(a) + " is: " + str(cosec))
#%% QUESTION 2
x= [[1,2,3], [4,5,6], [7,8,9]]
a=[2]
c=[a[2] for a in x]
print(c)
b=[1]
D=[(b[1]**2)*2 for b in x]
print(D)
#Q2b
import numpy as np
A=np.array([[3,0,-2], [6,-3.6,1.5], [0.7,-4.5,1]])
B=np.array([[5,-6,0]])
B=B.T
X=np.linalg.solve(A,B)
print(X)
Page 2 of 4
#%% QUESTION 3
import numpy as np
import matplotlib.pyplot as plt
A=np.linspace(0,2*np.pi)
B=(7/10)*A
x=np.linspace(0,2*np.pi)
# creating csv file
np.savetxt("COM_00001.csv",np.column_stack([A,B,x]),fmt='%.4g',delimiter=",")
#%% 3b
# loading from csv file
data = np.loadtxt('COM_00001.csv', delimiter=',', skiprows=0)
A1 = data[:,0] #FIRST COLUMN
B1 = data[:,1] #FIRST COLUMN
X1 = data[:,2] #FIRST COLUMN
y1 = np.cos(A1)* np.cos(B1)
y2 = 1/2*(np.cos(A1-B1))+ 1/2*(np.cos(A1+B1))
Page 3 of 4
HW3
#%% QUESTION 1
import mytrig
y = float(input("ENTER ANGLE : " )) #ANGLES ARE IN DEGREES
x =(y*(3.142/180)) #ANGLES ARE CONVERTED FROM DEGREE TO RADIAN
mytrig.trig_values(x,y)
------------------------------------------------------------------------------
#mytrig.py
def trig_values(x,y):
a=(x-((x**3)/(3*2))+((x**5)/(5*4*3*2))-((x**7)/(7*6*5*4*3*2)))
b=(1-((x**2)/2)+((x**4)/(4*3*2))-((x**6)/(6*5*4*3*2)))
# a and b above are trig. functions in power series
try:
sin = a
print("The SIN of angle " + str(y) + " is: " + str(sin))
cos = b
print("The COS of angle " + str(y) + " is: " + str(cos))
tan = a/b
print("The TAN of angle " + str(y) + " is: " + str(tan))
cot = b/a
print("The COT of angle " + str(y) + " is: " + str(cot))
sec = 1/b
print("The SEC of angle " + str(y) + " is: " + str(sec))
cosec = 1/a
print("The COSEC of angle " + str(y) + " is: " + str(cosec))
except ZeroDivisionError:
print("The Cotangent and Cosecant of zero is undefine!")
------------------------------------------------------------------------------
#%% QUESTION 2
def mean(data):
total = 0
for value in data:
total += value
return total/len(data)
def variance(data):
avg = mean(data)
total = 0
for value in data:
total += (value - avg)**2
return total/len(data)
def std(data):
return variance(data)**0.5
Page 4 of 4