Mathlab 5
Mathlab 5
linear combination
In [1]: import numpy as np
x=np.array([[1,0,0],[0,1,0],[0,0,1]])
y=([2,3,4])
scalars=np.linalg.solve(x.T,y)
print(scalars)
print(y,"=",scalars[0],"*",x[0],"+",scalars[1],"*",x[1],"+",scalars[2],"*",x[2
[2. 3. 4.]
[2, 3, 4] = 2.0 * [1 0 0] + 3.0 * [0 1 0] + 4.0 * [0 0 1]
In [ ]:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[2], line 5
3 import numpy as np
4 m=np.array([[1,0,0],[2,0,0],[0,1,1]])
----> 5 _,inds=sympy.Matrix(m).T.rref()
6 print(inds)
7 if len(inds)==3:
In [ ]:
linear transformation
localhost:8888/notebooks/Untitled29.ipynb?kernel_name=python3#heading 1/7
24/10/2023, 13:55 Untitled29 - Jupyter Notebook
matrix of LT
In [ ]: from sympy import *
var('x,y,a,b')
T=lambda x:[2*x[0]+3*x[1],4*x[0]-5*x[1]]
X=[x,y]
print("Given Transformation T(x,y)= ",T(X))
u1=[1,0];u2=[0,1]
v1=[1,0];v2=[0,1]
B1=[u1,u2]
B2=[v1,v2]
m=n=2
A=zeros(n,m)
for i in range(0,m):
eq=Matrix([v1,v2,T(B1[i])]).T
soln=solve_linear_system(eq,a,b)
A[:,i]=Matrix([soln[a],soln[b]])
print("\n Associated Matrix of Linear Transformation:")
pprint(A)
In [ ]:
localhost:8888/notebooks/Untitled29.ipynb?kernel_name=python3#heading 2/7
24/10/2023, 13:55 Untitled29 - Jupyter Notebook
In [ ]:
rank-nullity theorem
In [ ]: import numpy as np
from scipy.linalg import null_space
A=np.array([[1,2,3],[4,5,6],[7,8,9]])
rank=np.linalg.matrix_rank(A)
print("Rank=",rank)
ns=null_space(A.T)
print("Nullspace=",ns)
nullity=ns.shape[1]
print("nullity=",nullity)
if rank + nullity == A.shape[0]:
print("verified")
else:
print("not verified")
In [ ]: #alternate method
localhost:8888/notebooks/Untitled29.ipynb?kernel_name=python3#heading 3/7
24/10/2023, 13:55 Untitled29 - Jupyter Notebook
In [ ]:
bisection method
localhost:8888/notebooks/Untitled29.ipynb?kernel_name=python3#heading 4/7
24/10/2023, 13:55 Untitled29 - Jupyter Notebook
In [ ]:
newton raphson
In [ ]: from sympy import*
def f(x):
return x**3-2*x-5
def g(x):
return 3*x**2-2
x0=float(input("x0="))
x1=float(input("x1="))
if f(x0)*f(x1)>0.0:
print("bye")
else:
print("root lies btw a and b")
i=int(input("i="))
step=1
while (step<=i):
if g(x0)==0.0:
break
x=x0-(f(x0)/g(x0))
print('Iteration: %d, x=%0.6f and f(x)=%0.6f'%(step,x,f(x)))
x0=x
step= step+1
print("required root is %0.6f"%x)
In [ ]:
Regular falsi
localhost:8888/notebooks/Untitled29.ipynb?kernel_name=python3#heading 5/7
24/10/2023, 13:55 Untitled29 - Jupyter Notebook
In [ ]:
runge-kutta
In [13]: from sympy import*
def f(x,y):
return x+y**2
x0=float(input("x0="))
y0=float(input("y0="))
xn=float(input("xn="))
h=float(input("h="))
step=int(input("steps="))
def rk4(x0,y0,xn,n):
print('xo \t y0 \t k1 \t k2 \t k3 \t k4 \t yn')
for i in range(n):
k1=h*(f(x0,y0))
k2=h*(f(((x0+h)/2),(y0+k1/2)))
k3=h*(f(((x0+h)/2),(y0+k2/2)))
k4=h*(f((x0+h),(y0+k3)))
k=(k1+2*k2+2*k3+k4)/6
yn=y0+k
print(' %.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f'%(x0,y0,k1,k2,k3,k4,y
y0=yn
x0=x0+h
print(' x=%.4f,y=%.4f'%(xn,yn))
rk4(x0,y0,xn,step)
x0=0
y0=1
xn=0.4
h=0.2
steps=2
xo y0 k1 k2 k3 k4 yn
0.0000 1.0000 0.2000 0.2620 0.2758 0.3655 1.2735
0.2000 1.2735 0.3644 0.4638 0.4933 0.7043 1.7707
x=0.4000,y=1.7707
localhost:8888/notebooks/Untitled29.ipynb?kernel_name=python3#heading 6/7
24/10/2023, 13:55 Untitled29 - Jupyter Notebook
In [ ]:
localhost:8888/notebooks/Untitled29.ipynb?kernel_name=python3#heading 7/7