Python Lab - Ipynb - Colaboratory
Python Lab - Ipynb - Colaboratory
ipynb - Colaboratory
Loops
[ 1 2 3 4 5 6 7 8 9 10]
1
3
6
10
15
21
28
36
45
55
55
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20
Given m,x and n. Write a program to check wether x lies between m and n
https://colab.research.google.com/drive/156XP9PLhm4zuxCNCON6B6PTvcy7AZ2N7?authuser=2#scrollTo=PxycL_k1Tcf4&printMode=true 2/14
10/8/23, 7:25 PM Python Lab.ipynb - Colaboratory
if m<x<n:
print("X lies between m and n")
else:
print("X doesnot lie between m and n")
Given two numbers m and n. Write a program to print "HI" either m or n greater than 5
else print "Bye"
Create a function to find sum of two numbers and product of two numbers
def add(x,y):
z=x+y
print(z)
def mul(u,v):
w=u*v
print(w)
import numpy
print(dir(numpy))
import math
print(dir(math))
['ALLOW_THREADS', 'AxisError', 'BUFSIZE', 'CLIP', 'ComplexWarning', 'DataSource', 'ERR_CALL', 'ERR_DEFAULT', 'ERR_IGNORE', 'ERR_LOG
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil'
0.7071067811865475
-1.0
import sympy as sp
x = sp.symbols(x)
f= x**2+x-2
print(f.subs(x,5))
28
import sympy as sp
x,y = sp.symbols('x y')
f= x**2+y**2-(2*x*y)
print(f.subs(x,1).subs(y,-1))
https://colab.research.google.com/drive/156XP9PLhm4zuxCNCON6B6PTvcy7AZ2N7?authuser=2#scrollTo=PxycL_k1Tcf4&printMode=true 3/14
10/8/23, 7:25 PM Python Lab.ipynb - Colaboratory
Plot the function f(x) = sin(x) and f(x) = cos(x) in the interval (-10,10)
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(-10,10,500)
f1=np.sin(x)
f2 =np.cos(x)
plt.plot(x,f1)
plt.plot(x,f2)
[<matplotlib.lines.Line2D at 0x78b22cc107f0>]
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0,1.2,500)
y1=x**2
y2=x**(1/2)
plt.plot(x,y1,linestyle='--')
plt.plot(x,y2,linestyle='--')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0,1,500)
y1=x**2
y2=x**(1/2)
plt.plot(x,y1,linestyle='--')
plt.plot(x,y2,linestyle='--')
plt.show()
https://colab.research.google.com/drive/156XP9PLhm4zuxCNCON6B6PTvcy7AZ2N7?authuser=2#scrollTo=PxycL_k1Tcf4&printMode=true 4/14
10/8/23, 7:25 PM Python Lab.ipynb - Colaboratory
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0,0.5,500)
y1=x**2
y2=x**(1/2)
plt.plot(x,y1,linestyle='--')
plt.plot(x,y2,linestyle='--')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0,1.2,500)
y1=x**2
y2=x**(1/2)
plt.plot(x,y1,color='r',linestyle='--',label='y1=x**2')
plt.plot(x,y2,color='m',linestyle='-.',label='y1=x**(1/2)')
plt.legend()
plt.title('Plots of y=x^2 and y=x^1/2')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.fill_between(x,y1,y2,color='g')
plt.grid()
plt.show()
https://colab.research.google.com/drive/156XP9PLhm4zuxCNCON6B6PTvcy7AZ2N7?authuser=2#scrollTo=PxycL_k1Tcf4&printMode=true 5/14
10/8/23, 7:25 PM Python Lab.ipynb - Colaboratory
At (pi/4,0)
https://colab.research.google.com/drive/156XP9PLhm4zuxCNCON6B6PTvcy7AZ2N7?authuser=2#scrollTo=PxycL_k1Tcf4&printMode=true 6/14
10/8/23, 7:25 PM Python Lab.ipynb - Colaboratory
print(" Partial Derivative of",f,"w.r.t",x,"at point (pi/4,0)is",d1)
dfyy = diff(f,y,2)
d2 = dfyy.subs({x:pi/4,y:0})
print("Partial Derivative of",f,"w.r.t",y,"at point (pi/4,0)is",d2)
dfxy = diff(diff(f,x),y)
d3 = dfxy.subs({x:pi/4,y:0})
print("Partial Derivative of",f,"w.r.t", x and y,"is",d3)
2. f = logx
3. f = x
3
4. f = 1/1 + x
2
π/2
Evaluate ∫0 cos xdx
Integration of cos(x) is 1
2 1
Evaluate ∫0 ∫
0
xydxdy
1. f (t) = e
(2t)
2. f (t) = sint
https://colab.research.google.com/drive/156XP9PLhm4zuxCNCON6B6PTvcy7AZ2N7?authuser=2#scrollTo=PxycL_k1Tcf4&printMode=true 7/14
10/8/23, 7:25 PM Python Lab.ipynb - Colaboratory
3. f (t) = t
(3)
+ e
(−1)
+ cost
1. F (s) =
s−1
4
2. F (s) =
2
1
(s −1)(s+1)
3. F (s) =
s−1
1
+
4
2
s
4*exp(t)
exp(t) + 4*exp(-t)
⎡1 2 3 4⎤
0 2 4 5
⎣0 6⎦
0 5
https://colab.research.google.com/drive/156XP9PLhm4zuxCNCON6B6PTvcy7AZ2N7?authuser=2#scrollTo=PxycL_k1Tcf4&printMode=true 8/14
10/8/23, 7:25 PM Python Lab.ipynb - Colaboratory
⎡1 2 3 4⎤
0 2 4 5
⎣0 6⎦
0 5
Size of
from sympy the matrix
import * M is 3 X 4
M = Matrix([[1,2,3],[2,4,5],[0,5,6]])
display(M)
[m,n]= shape(M)
print('Size of the matrix M is',m,'X',n,'\n')
B=M.rref( )
print('Row reduced Echelon form of M is\n\n')
pprint(B)
display('Rank of the matrix M is',M.rank())
print('|M|=',det(M))
print('Trace of M is',trace(M))
print('Inverse if M is\n')
display(M.inv())
⎡1 2 3⎤
2 4 5
⎣0 6⎦
5
Size of the matrix M is 3 X 3
⎛⎡1 0 0⎤ ⎞
⎜⎢ ⎥ ⎟
⎜⎢0 1 0⎥, (0, 1, 2)⎟
⎜⎢ ⎥ ⎟
⎝⎣0 0 1⎦ ⎠
'Rank of the matrix M is'
3
|M|= 5
Trace of M is 11
Inverse if M is
⎡ −5 − 25 ⎤
1 3
5
− 12 6 1
⎣ 2 0 ⎦
5 5 5
−1
8
Inverse of the matris is
---------------------------------------------------------------------------
NonInvertibleMatrixError Traceback (most recent call last)
<ipython-input-47-0c8cc85fb958> in <cell line: 6>()
4 display(M)
5 print('Inverse of the matris is')
----> 6 display(M.inv())
3 frames
/usr/local/lib/python3.10/dist-packages/sympy/matrices/inverse.py in _inv_GE(M, iszerofunc)
243
244 if any(iszerofunc(red[j, j]) for j in range(red.rows)):
--> 245 raise NonInvertibleMatrixError("Matrix det == 0; not invertible.")
246
247 return M._new(red[:, big.rows:])
https://colab.research.google.com/drive/156XP9PLhm4zuxCNCON6B6PTvcy7AZ2N7?authuser=2#scrollTo=PxycL_k1Tcf4&printMode=true 9/14
10/8/23, 7:25 PM Python Lab.ipynb - Colaboratory
display(M[0:1:2,2])
display(M[1:,0:2])
[1 2]
5
[ ]
11
[1 2 3]
[2 4 5]
[4 8 11]
⎡1⎤
2
⎣4⎦
[3]
2 4
[ ]
4 8
Then find
1.Inverse of A
⎡2 1 0⎤
1 2 0
⎣0 1⎦
0
Inverse of matrix M is:
⎡ 0⎤
2
3
− 13
− 13 2
0
⎣ 0 1⎦
3
0
The eigen values of the matrix are
{3: 1, 1: 2}
The eigen vectors of the matrix are
https://colab.research.google.com/drive/156XP9PLhm4zuxCNCON6B6PTvcy7AZ2N7?authuser=2#scrollTo=PxycL_k1Tcf4&printMode=true 10/14
10/8/23, 7:25 PM Python Lab.ipynb - Colaboratory
⎡2 3 0 0⎤
2 3 0 0
0 0 2 3
⎣0 3⎦
0 2
The eigen values of the matrix are
{5: 2, 0: 2}
The eigen vectors of the matrix are
2 3
[ ]
1 2
{2 - sqrt(3): 1, sqrt(3) + 2: 1}
The eigen vectors of the matrix are
⎡⎛ ⎡⎡-√3⎤⎤⎞ ⎛ ⎡⎡√3⎤⎤⎞⎤
⎢⎜2 - √3, 1, ⎢⎢ ⎥⎥⎟, ⎜√3 + 2, 1, ⎢⎢ ⎥⎥⎟⎥
⎣⎝ ⎣⎣ 1 ⎦⎦⎠ ⎝ ⎣⎣1 ⎦⎦⎠⎦
x + y + 4z = 1
2x + 2y + 8z = 2
{x: 1 - y, z: 0}
x=0;y=0;z=0
for i in range(1,20):
x=1-y+2*z
y=1-x-4*z
z=(2-2*y-2*x)/8
display(x,y,z)
1.0
0.0
0.0
https://colab.research.google.com/drive/156XP9PLhm4zuxCNCON6B6PTvcy7AZ2N7?authuser=2#scrollTo=PxycL_k1Tcf4&printMode=true 11/14
10/8/23, 7:25 PM Python Lab.ipynb - Colaboratory
from numpy import *
x = [1,2,3,4,5]
y = [10,20,30,50,80]
m=float(input('enter the value of x: '))
n=len(x)-1
h=x[1]-x[0]
sum = y[-1]
t = (m-x[-1])/h
prod = 1
for i in range(0,n):
y = diff(y)
print(y)
prod = prod*(t+i)/(i+1)
sum = sum + prod*y[-1]
print('by NFI value of y(',m,')=',sum)
Simpson's 1/3
Simpson's 3/8
https://colab.research.google.com/drive/156XP9PLhm4zuxCNCON6B6PTvcy7AZ2N7?authuser=2#scrollTo=PxycL_k1Tcf4&printMode=true 12/14
10/8/23, 7:25 PM Python Lab.ipynb - Colaboratory
n= round((xn-x0)/h)
def f(x,y):
return sin(x*y)
for i in range(0,n):
y1p=y0+h*f(x0,y0)
for j in range(1,50):
y1c=y0+h/2*(f(x0,y0)+f(x0+h,y1p))
if abs(y1c-y1p<10**(-4)):
print("The approx. soln. after %d iteration is y(%f)=%f"%(j,x0+h,y1c))
break
y1p=y1c
x0=x0+h
y0=y1c
[1.96 0.14]
linear fit is y= 1.960*x+0.140
https://colab.research.google.com/drive/156XP9PLhm4zuxCNCON6B6PTvcy7AZ2N7?authuser=2#scrollTo=PxycL_k1Tcf4&printMode=true 13/14
10/8/23, 7:25 PM Python Lab.ipynb - Colaboratory
<matplotlib.legend.Legend at 0x7f1cb13bfe50>
https://colab.research.google.com/drive/156XP9PLhm4zuxCNCON6B6PTvcy7AZ2N7?authuser=2#scrollTo=PxycL_k1Tcf4&printMode=true 14/14