0% found this document useful (0 votes)
42 views7 pages

Mathlab 5

The document discusses and provides code examples for several numerical methods: 1) It shows how to find the linear combination of vectors that equals another vector using NumPy's linalg.solve function. 2) It demonstrates checking for linear dependence or independence of vectors and discusses the rank of a matrix. 3) Methods for solving equations numerically are presented, including bisection, Newton-Raphson, and Regular Falsi. 4) The Runge-Kutta method for numerically solving differential equations is introduced.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views7 pages

Mathlab 5

The document discusses and provides code examples for several numerical methods: 1) It shows how to find the linear combination of vectors that equals another vector using NumPy's linalg.solve function. 2) It demonstrates checking for linear dependence or independence of vectors and discusses the rank of a matrix. 3) Methods for solving equations numerically are presented, including bisection, Newton-Raphson, and Regular Falsi. 4) The Runge-Kutta method for numerically solving differential equations is introduced.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

24/10/2023, 13:55 Untitled29 - Jupyter Notebook

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 [ ]: ​

linear dependence and independence ¶


In [2]: from math import*
from sympy import*
import numpy as np
m=np.array([[1,0,0],[2,0,0],[0,1,1]])
_,inds=sympy.Matrix(m).T.rref()
print(inds)
if len(inds)==3:
print("independent")
else:
print("dependent")

---------------------------------------------------------------------------
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:

NameError: name 'sympy' is not defined

In [ ]: ​

linear transformation

localhost:8888/notebooks/Untitled29.ipynb?kernel_name=python3#heading 1/7
24/10/2023, 13:55 Untitled29 - Jupyter Notebook

In [ ]: from sympy import*


from operator import add
var('x y')
var('x1, x2, y1, y2, c')
T= lambda X: [X[0]+X[1],X[1]]
X=[x,y]
print("given transformation T(x,y)=",T(X))
x=[x1,y1]
y=[x2,y2]
l1=T(list(map(add, x,y)))
r1=list(map(add,T(x),T(y)))
l2=T(list(map(lambda x: c*x,x)))
r2=list(map(lambda x: expand(c*x),T(x)))
if l1==r1 and l2==r2:
print("T is linear transformation")
else:
print("not LT")

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 [ ]: ​

basis and dimensions

localhost:8888/notebooks/Untitled29.ipynb?kernel_name=python3#heading 2/7
24/10/2023, 13:55 Untitled29 - Jupyter Notebook

In [ ]: from sympy import*


A=Matrix([[1,1,1],[1,2,3],[-1,0,1]])
D=A.det()
if D==0:
print("linearly dependent hence not basis")
else:
print("linearly independent hence forms basis")
B=A.echelon_form()
r=A.rank()
print("basis of subspace generated by given vector: ")
for i in range(0,r):
print(B.row(i))
print("dimensions of subspace:",r)

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 [ ]: from sympy import *


var('x,y,z,a,b')
T=lambda x: [x[1]-x[0],x[1]-x[2]]
X=[x,y,z]
print("Given transformation T(x,y,z)=",T(X))
u1=[1,0,0];u2=[0,1,0];u3=[0,0,1]
v1=[1,0];v2=[0,1]
B1=[u1,u2,u3]
B2=[v1,v2]
m=3
n=2
A=Matrix([T(u1),T(u2),T(u3)])
r=A.rank()
B=A.echelon_form()
print("R(T) is generated by:")
for i in range(0,r):
pprint(B[i,:])
print("rank=",r)
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]])
K=A.nullspace()
print("Null space generated by : ",*K)
n=Matrix(K).shape[1]
print("Nullity=",n)
if m==(r+n):
print("Rank nullity theorem verified")
else:
print("Rank nullity theorem is not verified")

In [ ]: ​

bisection method

localhost:8888/notebooks/Untitled29.ipynb?kernel_name=python3#heading 4/7
24/10/2023, 13:55 Untitled29 - Jupyter Notebook

In [ ]: from sympy import*


def f(x):
return x**3-4*x-9
a=float(input("a="))
b=float(input("b="))
if f(a)*f(b)>0.0:
print("bye")
else:
print("root lies btw a and b")
i=int(input("i="))
step=1
while (step<=i):
x=(a+b)/2
print('Iteration: %d, x=%0.6f and f(x)=%0.6f'%(step,x,f(x)))
if f(a)*f(x)<0:
b=x
else:
a=x
step= step+1
print("required root is %0.6f"%x)

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 [ ]: from sympy import*


def f(x):
return (cos(x)+1-3*x)
a=float(input("a="))
b=float(input("b="))
if f(a)*f(b)>0.0:
print("bye")
else:
print("root lies btw a and b")
i=int(input("i="))
step=1
while (step<=i):
x=(a*f(b)-b*f(a))/(f(b)-f(a))
print('Iteration: %d, x=%0.6f and f(x)=%0.6f'%(step,x,f(x)))
if f(a)*f(x)<0:
b=x
else:
a=x
step= step+1
print("required root is %0.6f"%x)

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

You might also like