Matlab 6
Matlab 6
Matlab 6
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],"*",
[2. 3. 4.]
[2, 3, 4] = 2.0 * [1 0 0] + 3.0 * [0 1 0] + 4.0 * [0 0 1]
In [ ]:
(0, 2)
dependent
(0, 2)
Dependent
linear transformation
localhost:8888/notebooks/Untitled29.ipynb 1/7
25/10/2023, 21:11 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 2/7
25/10/2023, 21:11 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 3/7
25/10/2023, 21:11 Untitled29 - Jupyter Notebook
In [ ]:
bisection method
localhost:8888/notebooks/Untitled29.ipynb 4/7
25/10/2023, 21:11 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 5/7
25/10/2023, 21:11 Untitled29 - Jupyter Notebook
In [ ]:
runge-kutta
localhost:8888/notebooks/Untitled29.ipynb 6/7
25/10/2023, 21:11 Untitled29 - Jupyter Notebook
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
In [ ]:
localhost:8888/notebooks/Untitled29.ipynb 7/7