LAB 1 To LAB 10 - Jupyter Notebook
LAB 1 To LAB 10 - Jupyter Notebook
In [ ]: 1 #Program to plot curve connecting the points using plot and scatter function
2
3 from pylab import * #Library which comprise of numpy and matplotlib
4
5 x=[2,3,4,5,7] # x - values
6 y=[4,2,3,7,10] # y - values
7
8 if len(x)!=len(y):
9 print("number of values of x and y doesnot match")
10 else:
11 scatter(x,y) #plot points in the plane
12 plot(x,y) #plot curve connecting the points
13 grid()
14 xlabel("x-axis")
15 ylabel("y-axis")
16 title("Scatter grph")
17 xlim(0,10)
18 ylim(0,10)
19 axvline(color="red")
20 axhline(color="red")
21 show()
In [ ]: 1 #LAB 2
In [ ]: 1 #LAB 4
In [ ]: 1 # Maxima and Minima of function with 2 Variables
2 #f(x, y) = x^4 + y^4 - 2x^2 − 2y^2 + 4xy.
3 from sympy import *
4 x,y=symbols('x,y')
5
6 f=(x**4)+(y**4)-(2*x**2)+(4*x*y)-(2*y**2)
7 fx = diff(f,x)
8 fy = diff(f,y)
9
10 points=solve([Eq(fx,0),Eq(fy,0)],[x,y])
11
12 a1=[]
13 b1=[]
14 if type(points)!=list:
15 a1.append(points[x])
16 b1.append(points[y])
17 print(points)
18 print(a1,b1)
19 else:
20 for i in range(len(points)):
21 (a,b)=points[i]
22 if im(a)==0 and im(b)==0:
23 a1.append(a)
24 b1.append(b)
25 print(a1,b1)
26
27 fxx=diff(fx,x)
28 fxy=diff(fx,y)
29 fyy=diff(fy,y)
30
31 for i in range(len(a1)):
32 A=fxx.subs({x:a1[i],y:b1[i]})
33 B=fxy.subs({x:a1[i],y:b1[i]})
34 C=fyy.subs({x:a1[i],y:b1[i]})
35 print('A=',A,'B=',B,'C=',C)
36
37 delta=A*C-B**2
38 print("AC-B^2=",delta)
39
40 if delta>0 and A>0:
41 print('(%0.2f,%0.2f)'%(a1[i],b1[i]),'is the minimum point')
42 elif delta>0 and A<0:
43 print('(%0.2f,%0.2f)'%(a1[i],b1[i]),'is the maximum point')
44 elif delta<0:
45 print('(%0.2f,%0.2f)'%(a1[i],b1[i]),'is the saddle point')
46 elif delta==0:
47 print("At (%0.2f,%0.2f)"%(a1[i],b1[i]),' Further investigation is required
In [ ]: 1 # Programs to find MacLaurin series of f(x)=exp(x)
2 from sympy import *
3 x=Symbol("x")
4 y=exp(x)
5 ys=y.subs({x:0})
6 n=int(input("Number of terms you want: "))
7 for i in range(1,n):
8 ys+=x**i*diff(y,x,i).subs({x:0})/factorial(i)
9 display("Maclaurin's series is", ys)
10
11 ys=lambdify(x,ys)
12
13 from pylab import *
14 x=arange(0,5,0.001)
15 plot(x,exp(x),color="red")
16 plot(x,ys(x),color="green")
17 axvline()
18 axhline()
19 show()
In [ ]: 1 #LAB 6
In [ ]: 1 # Program to Check whether the following system of homogenous linear equation
2 # has non-trivial solution.
3 # x1 + 2x2 − x3 = 0, 2x1 + x2 + 4x3 = 0, 3x1 + 3x2 + 4x3 = 0.
4 from sympy import *
5
6 A=Matrix([[1,2,-1],[2,1,4],[3,3,4]])
7
8 ρA=A.rank()
9 n=shape(A)
10 print("The coefficient Matrix A is: ")
11 display(A)
12 print(f"Rank of the matrix is {ρA}")
13
14 if ρA==n[1]:
15 print("System has trivial solution")
16 else:
17 print("System has non trivial solution")
In [ ]: 1 #LAB 7
In [ ]: 1 #Solve the system of equations using Gauss-Seidel method:
2 #20 x + y − 2 z = 17; 3 x + 20 y − z = −18; 2 x − 3 y + 20 z = 25.
3
4 f1=lambda x,y,z: (17-y+2*z)/20
5 f2=lambda x,y,z: (-18-3*x+z)/20
6 f3=lambda x,y,z: (25-2*x+3*y)/20
7
8 x0,y0,z0=0,0,0
9
10 count=1
11 e=0.0001 #the tolerable error
12
13 print("\nCount\t x\t\t y\t\t z\n")
14
15 condition=True
16 while condition:
17 x1=f1(x0,y0,z0)
18 y1=f2(x1,y0,z0)
19 z1=f3(x1,y1,z0)
20 print("%d\t%.5f \t%.5f\t%.5f\n"%(count,x1,y1,z1))
21 count=count+1
22 e1=abs(x0-x1)
23 e2=abs(y0-y1)
24 e3=abs(z0-z1)
25 x0,y0,z0=x1,y1,z1
26 condition = e1>=e and e2>=e and e3>=e
27 print("\n Solution: x=%0.5f, y=%0.5f and z=%0.5f"%(x1,y1,z1))
In [ ]: 1 #LAB 8
In [ ]: 1 #LAB 9
In [ ]: 1 #LAB 10
In [ ]: 1 #Program to solve the linear congruence 4 x congruent 2 (mod 6);
2 #use module gcd() of sympy
3
4 from sympy import *
5 a=4
6 b=2
7 n=6
8 g=gcd(a,n)
9 print(f"GCD{a,n} =",g)
10 if b%g != 0:
11 print("The congruence has no integer solution")
12 else:
13 if g==1:
14 print("The congruence has unique solution")
15 for i in range(1,n-1):
16 x=(n*i+b)/a
17 if x//1==x:
18 x0=x//1
19 print("Solution of the given congruence is:",x0)
20 break
21 else:
22 print(f"The congruence has {g} solutions")
23 for i in range(1,n-1):
24 x=(n*i+b)/a
25 if x//1==x:
26 x0=x//1
27 break
28 z=[]
29 for i in range(g):
30 x=(x0+i*(n//g))%n
31 z.append(int(x))
32 print("Solutions of the given congruence are:",z)
In [ ]: 1