You are on page 1of 3

Question 1

"""
Program to obtain the values of currents I1, I2, I3 and I4 from the equations below
I1 - 2I2 - I3 + 3I4 = 4
2I1 + 2I2 + I3 - 4I4 = 3
3I1 - I2 - 2I3 + 2I4 = 6
I1 + 3I2 - I3 + I4 = 8
"""
import numpy as np
A=np.array([[1,-2,-1,3],[2,1,1,-4],[3,-1,-2,2],[1,3,-1,1]])
B=np.array([4,3,6,8])
I=np.linalg.solve(A,B)
print('I1 = '+str(format(I[0],'.2f'))) # formatting answers to two decimal places
print('I1 = '+str(format(I[1],'.2f')))
print('I1 = '+str(format(I[2],'.2f')))
print('I1 = '+str(format(I[3],'.2f')))

Question 2
"""
Program to obtain the co-ordinates of the point of intersection (x, y and z) from the equations
below
x + y + 2z = 1
3x + 6y - z = 0
-x - y - 4z = 3
"""
import numpy as np
A=np.array([[1,1,2],[3,6,-1],[-1,-1,-4]])
B=np.array([1,0,3])
X=np.linalg.solve(A,B)
print('x = '+str(format(X[0],'.2f'))) # formatting answers to two decimal places
print('y = '+str(format(X[1],'.2f')))
print('z = '+str(format(X[2],'.2f')))
print('Point of intersection = ('+str(format(X[0],'.2f'))+','+str(format(X[1],'.2f'))
+','+str(format(X[2],'.2f'))+')')

Question 3
"""
A programme to obtain the fourier series of the function:
3 for 0<x>5
f(x)= -3 for -5<x>10
"""
from sympy import fourier_series, Piecewise, lambdify
from sympy.abc import x
import numpy as np
import matplotlib.pyplot as plt
f=fourier_series(Piecewise((3, x<5), (-3, x>5)), (x,0,10))
print(f.truncate(9))
# Graphical analysis
for k in range (1,9):
f_n=lambdify(x, f.truncate(n=k))
xs=np.linspace(0,10,1000)
plt.plot(xs, f_n(xs), label=f'$n={k}$')
plt.autoscale(enable=True, axis='x', tight=True)
plt.legend()
plt.show()

Additional interesting detail


Graph plot from the third question:

You might also like