0% found this document useful (0 votes)
2 views2 pages

Note 2

The document contains a series of Python code snippets utilizing NumPy and Matplotlib for various mathematical computations and visualizations. It includes matrix operations, function definitions for trigonometric identities, summation, data writing and loading, numerical differentiation, and plotting of functions. Additionally, it demonstrates the use of SciPy for solving equations involving sine functions.

Uploaded by

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

Note 2

The document contains a series of Python code snippets utilizing NumPy and Matplotlib for various mathematical computations and visualizations. It includes matrix operations, function definitions for trigonometric identities, summation, data writing and loading, numerical differentiation, and plotting of functions. Additionally, it demonstrates the use of SciPy for solving equations involving sine functions.

Uploaded by

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

question 1

from numpy import array,dot,matrix


from numpy.linalg import det,eig,inv
?
A = matrix([ [11,0,3,5],[13,1,0,4],[14,2,8,2],[16,3,6,4] ])
B = matrix([ [5,-1,13,5],[3,1,10,-4],[2,2,-14,1],[2,1,-16,0] ])
?
evalA,eigvA = eig(A)
print ("2A - 6B = ",2*A-6*B)
print ("A x B = ",dot(A,B))
print ("Det of B =",det (B))
print ("eigenvalues of A = ",evalA)

qestion 2
from numpy import sin,cos,pi
?
def sincos (x):
return (sin(x)) **2+(cos(x)) **2
?
x = 45*pi/180
print ("Result = ",sincos(x))

question 3
from numpy import arange
?
def fsum(n):
val = arange(1,n+1,1)
return sum(val)
?
print ("sum up to n = 22 is: ",fsum(22))

Quest 4a
from numpy import arange
?
x = arange(5,30,1)
y = x**3+5
f = open('xcube.dat', 'w')
for i in range (0,len(x)):
print(x[i], y[i], sep=' ', file=f)
f.close()

Quest 4b
from numpy import loadtxt
?
x,y=loadtxt('xcube.dat',unpack=True)
?
import matplotlib.pyplot as plt
?
plt.plot(x,y,'--r')
plt.xlabel('x')
plt.ylabel('y')
plt.savefig('xcube.pdf')

Quest 5
from numpy import arctan,sqrt
?
def fda(f,x,h):
return (f(x+h) - f(x))/h
def f1(x):
return arctan(x)
?
exact = 1/13
h=0.0001
?
ans = fda(f1,sqrt(12),h)
print("Answer = ",ans)
print("Exact = ",exact)
print("Error = ",exact-ans)

Quest 6
from numpy import sqrt,pi,exp,linspace
import matplotlib.pyplot as plt

def h (x) :
yout = (1/(sqrt(2*pi)))*(exp(-0.5*x))
return yout

x = linspace(-4,4,200)
plt.plot(x,h(x))
plt.xlabel('x')
plt.ylabel('h(x)')
plt.savefig('FISH.png')
plt.show()

Quest 7
from scipy.optimize import fsolve
from numpy import sin

def f(x):
return sin(x)-0.1*x

x=fsolve(f,[2,6,10])
print ("Solution = ",x)

#from numpy import arange


#x = arange(0,12,0.1)
#plt.plot(x,f(x))

You might also like