0% found this document useful (0 votes)
2 views

Python assignments

Assignment math

Uploaded by

akshsydoor
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python assignments

Assignment math

Uploaded by

akshsydoor
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Python assignments:

EX-1.Write a Python script to solve the following system of linear equations using
Gaussian elimination with partial pivoting:

3x+4y+5z+6w=4

4x+5y+6z+7w=5

8x+9y+4z+3w=6

3x+5y+6z+7w=7

Solution:

#Name-Shradha. Nagur , USN-01FE22BAR044.

#Gaussian Elimination Method.

import numpy as np
a=np.array([[3,4,5,6],[4,5,6,7],[8,9,4,3],[3,5,6,7]],float)
b=np.array([4,5,6,7],float)
print(a)
print(b)
n=len(b)
print(n)
for k in range(n-1):
for i in range(k+1,n):
if a[i,k]==0:continue
factor=a[k,k]/a[i,k]
for j in range (k,n):
a[i,j]=a[k,j]-a[i,j]*factor
b[i]=b[k]-b[i]*factor
print(a)
print(b)
x=np.zeros(n,float)
x[n-1]=b[n-1]/a[n-1,n-1]
for i in range(n-2,-1,-1):
sum=0
for j in range(i+1,n):
sum=sum+a[i,j]*x[j]
x[i]=(b[i]-sum)/a[i,i]
Output:[[3. 4. 5. 6.]
[4. 5. 6. 7.]
[8. 9. 4. 3.]
[3. 5. 6. 7.]]
[4. 5. 6. 7.]
4
[[ 3. 4. 5. 6. ]
[ 0. 0.25 0.5 0.75]
[ 0. 0. -0.9 -1.2 ]
[ 0. 0. 0. 0.6 ]]
[ 4. 0.25 -0.45 -2.25]

EX-2. Given the matrix AAA and vector BBB below, write a Python script to perform the
following tasks:

1. Solve the system of linear equations AX=B to find the solution vector X.
2. Calculate and print the rank of matrix A.
3. Determine and print the eigenvalues and eigenvectors of matrix A.
Solution:
#Name-Shradha. Nagur , USN-01FE22BAR044.

#Linear equations, Rank , Eigenvalues and Eigenvector.

import numpy as np
from numpy.linalg import matrix_rank
from numpy.linalg import eig
A=np.array([[6,-2,2],[-2,3,-1],[2,-1,3]])
B=np.array([5,6,7])
X=np.linalg.solve(A,B)
print("Matrix:\n A=",A,"\n B=",B,"\n x=",x)
print("Rank:",matrix_rank(A))
w,v=eig(A)
print('E-value:',w)
print('E-vector:',v)

output: Matrix:
A= [[ 6 -2 2]
[-2 3 -1]
[ 2 -1 3]]
B= [5 6 7]
x= [-2. 1.25 5.5 -3.75]
Rank: 3
E-value: [8. 2. 2.]
E-vector: [[ 0.81649658 -0.57735027 -0.11547005]
[-0.40824829 -0.57735027 0.57735027]
[ 0.40824829 0.57735027 0.80829038]]

EX-3. Implement the Power Method to find the largest eigenvalue and its corresponding
eigenvector for a given matrix A:
Solution :
#Name-Shradha. Nagur , USN-01FE22BAR044.

#Power Method(largest eigenvalue, eigenvector)


import numpy as np
def normalize(x):
fac=abs(x).max()
x_n=x/x.max()
return fac,x_n
x=np.array([1,0,0])
a=np.array([[8,-6,2],[-6,7,4],
[2,-4,3]])
print('Iteration eigenvalue, eigenvector')
for i in range(10):
x=np.dot(a,x)
lambda_1,x=normalize(x)
print(i+1," ",lambda_1," ",x)
print('Largest EigenValue:',lambda_1)
print('EigenVector:',x)

output:
Iteration eigenvalue, eigenvector
1 8 [ 1. -0.75 0.25]
2 13.0 [ 1. -0.78846154 0.44230769]
3 13.615384615384615 [ 1. -0.71610169 0.4759887 ]
4 13.248587570621469 [ 1. -0.68752665 0.4749467 ]
5 13.075053304904051 [ 1. -0.68167216 0.47226933]
6 13.034571605622777 [ 1. -0.6814668 0.47132325]
7 13.031447280515847 [ 1. -0.68181027 0.47115541]
8 13.033172447997401 [ 1. -0.68195601 0.47115983]
9 13.034055700780398 [ 1. -0.68198671 0.47117364]
10 13.034267528783237 [ 1. -0.68198787 0.47117859]
Largest EigenValue: 13.034267528783237
EigenVector: [ 1. -0.68198787 0.47117859]

EX-4. Given the following system of linear equations, use the iterative method to find the
solution:
{20x1+x2−2x3=17
−2x1+20x2+x3=−18
2x1−3x2+20x3=25}

Implement a Python script that iteratively solves the system using the following steps:

1. Initialize the variables x1, x2, and x3 to 0.


2. Define a convergence criterion with a tolerance of 0.001.
3. Perform the iteration for a maximum of 50 steps or until convergence is reached.

Solution:

#Name-Shradha. Nagur , USN-01FE22BAR044.

# Iterative method.

import numpy as np
x1=0
x2=0
x3=0
e=np.array([0.001,0.001,0.001])
x_old=np.array([x1,x2,x3])
for k in range(1,50):
x1=(17-x2+2*x3)/20
x2=(-18-2*x1+x3)/20
x3=(25-2*x1-3*x2)/20
x=np.array([x1,x2,x3])
dx=np.array([x-x_old])
print('%d,%f,%f,%f'%(k,x1,x2,x3))
print(dx)
if dx.all()<e.all():
converge=True
print('converged')
break
x_old=x
else:
print('not converged')

output: 1,0.850000,-0.985000,1.312750
[[ 0.85 -0.985 1.31275]]
2,1.030525,-0.937415,1.287560
[[ 0.180525 0.047585 -0.02519025]]
3,1.025627,-0.938185,1.288165
[[-0.00489827 -0.00076969 0.00060528]]
4,1.025726,-0.938164,1.288152
[[ 9.90122750e-05 2.03627850e-05 -1.29556452e-05]]
5,1.025723,-0.938165,1.288152
[[-2.31370378e-06 -4.16411885e-07 2.93832160e-07]]
6,1.025723,-0.938165,1.288152
[[ 5.02038102e-08 9.67122726e-09 -6.47106502e-09]]
7,1.025723,-0.938165,1.288152
[[-1.13066756e-09 -2.10486628e-10 1.44639634e-10]]
8,1.025723,-0.938165,1.288152
[[ 2.49882337e-11 4.73310280e-12 -3.20854454e-12]]
9,1.025723,-0.938165,1.288152
[[-5.57776048e-13 -1.04583009e-13 7.12763182e-14]]
10,1.025723,-0.938165,1.288152
[[ 1.24344979e-14 2.33146835e-15 -1.55431223e-15]]
11,1.025723,-0.938165,1.288152
[[0. 0. 0.]]
Converged

EX-5. Given the following system of linear equations, use the iterative method (Gauss-
Seidel method) to find the solution:
{20x1+x2−2x3=17
−2x1+20x2+x3=−18
2x1−3x2+20x3=25}

Implement a Python script that iteratively solves the system using the following steps:

1. Initialize the variables x1, x2, and x3 to 0.


2. Define a convergence criterion with a tolerance of 0.001.
3. Perform the iteration for a maximum of 50 steps or until convergence is reached.

Solution:
#Name-Shradha. Nagur , USN-01FE22BAR044.

# Iterative method (Gauss-Seidel method)

import numpy as np
x1=0
x2=0
x3=0
e=np.array([0.0001,0.0001,0.0001])
x_old=np.array([x1,x2,x3])
for k in range(1,50):
x1=(17-x2+2*x3)/20
x2=(-18-2*x1+x3)/20
x3=(25-2*x1-3*x2)/20
x=np.array([x1,x2,x3])
dx=np.array([x-x_old])
print('%d,%f,%f,%f'%(k,x1,x2,x3))
print(dx)
if dx.all()<e.all():
converge=True
print('converged')
break
x_old=x
else:
print('not converged')
output: 1,0.850000,-0.985000,1.312750
[[ 0.85 -0.985 1.31275]]
2,1.030525,-0.937415,1.287560
[[ 0.180525 0.047585 -0.02519025]]
3,1.025627,-0.938185,1.288165
[[-0.00489827 -0.00076969 0.00060528]]
4,1.025726,-0.938164,1.288152
[[ 9.90122750e-05 2.03627850e-05 -1.29556452e-05]]
5,1.025723,-0.938165,1.288152
[[-2.31370378e-06 -4.16411885e-07 2.93832160e-07]]
6,1.025723,-0.938165,1.288152
[[ 5.02038102e-08 9.67122726e-09 -6.47106502e-09]]
7,1.025723,-0.938165,1.288152
[[-1.13066756e-09 -2.10486628e-10 1.44639634e-10]]
8,1.025723,-0.938165,1.288152
[[ 2.49882337e-11 4.73310280e-12 -3.20854454e-12]]
9,1.025723,-0.938165,1.288152
[[-5.57776048e-13 -1.04583009e-13 7.12763182e-14]]
10,1.025723,-0.938165,1.288152
[[ 1.24344979e-14 2.33146835e-15 -1.55431223e-15]]
11,1.025723,-0.938165,1.288152
[[0. 0. 0.]]
converged
EX.6- Given the following data points for y at different x:
x:0,1,2,3,4,5
y:−3,3,11,27,57,107

1. Construct the forward difference table for the given data points.
2. Determine the Newton's forward interpolation polynomial for the given data.
3. Using the interpolating polynomial, find the value of y at x=0.5.

Solution:
#Name-Shradha. Nagur , USN-01FE22BAR044.

# Newton's forward interpolation Method.

import numpy
import math
import sympy
X=sympy.Symbol('X')
x=[0,1,2,3,4,5]
n=len(x)
y=[[0 for i in range(n)]
for j in range(n)]
y[0][0]=-3
y[1][0]=3
y[2][0]=11
y[3][0]=27
y[4][0]=57
y[5][0]=107
sum=y[0][0]
for i in range(1,n):
for j in range(n-i):
y[j][i]=y[j+1][i-1]-y[j][i-1]
print('Forward difference table')
print('******************')
print('x y 1st 2nd 3rd 4th 5th')
for i in range(n):
print(x[i],end='\t')
for j in range(n):
print((y[i][j]),end='\t')
print('')
p=(X-x[0])/(x[1]-x[0])
def p_cal(p,n):
temp=p
for i in range(1,n):
temp=temp*(p-i)
return temp
for i in range(1,n):
sum = sum+(p_cal(p,i)*y[0][i])/math.factorial(i)
print('\n the interpolating polynomial is ',sympy.simplify(sum))
X=float(input("Enter value of x"))
p=(X-x[0])/(x[1]-x[0])
sum=y[0][0]
for i in range(1,n):
sum=sum+(p_cal(p,i)*y[0][i])/math.factorial(i)
print("\n Valur at x=",X,"is",round(sum,6))
output: Forward difference table
******************
x y 1st 2nd 3rd 4th 5th
0 -3 6 2 6 0 0
1 3 8 8 6 0 0
2 11 16 14 6 0 0
3 27 30 20 0 0 0
4 57 50 0 0 0 0
5 107 0 0 0 0 0

the interpolating polynomial is X**3 - 2*X**2 + 7*X - 3


Enter value of x0.5

Valur at x= 0.5 is 0.125

EX-7. Given the following data points for y at different x:


x:0,1,2,3,4,5
y:−3,3,11,27,57,107.
1. Construct the backward difference table for the given data points.
2. Determine the Newton's backward interpolation polynomial for the given data.
3. Using the interpolating polynomial, find the value of y at a specified x.

Solution:

#Name-Shradha. Nagur , USN-01FE22BAR044.

# Newton's backward interpolation Method.

import numpy
import math
import sympy
X=sympy.Symbol('X')
x=[0,1,2,3,4,5]
n=len(x)
y=[[0 for i in range(n)]
for j in range(n)]
y[0][0]=-3
y[1][0]=3
y[2][0]=11
y[3][0]=27
y[4][0]=57
y[5][0]=107
sum=y[n-1][0]
for i in range(1,n):
for j in range(n-i):
y[j][i]=y[j+1][i-1]-y[j][i-1]
print('backward difference table')
print('**********************************************')
print('x y 1st 2nd 3rd 4th ')
for i in range(n):
print(x[i],end='\t')
for j in range(n):
print((y[i][j]),end='\t')
print('')
p=(X-x[n-1])/(x[1]-x[0])
def p_cal(p,n):
temp=p
for i in range(1,n):
temp=temp*(p+i)
return temp
for i in range(1,n):
sum = sum+(p_cal(p,i)*y[n-1][i])/math.factorial(i)
print('\n the interpolating polynomial is ',sympy.simplify(sum))
X=float(input("Enter value of x"))
p=(X-x[n-1])/(x[1]-x[0])
sum=y[0][0]
for i in range(1,n):
sum=sum+(p_cal(p,i)*y[n-1][i])/math.factorial(i)
print("\n Valur at x=",X,"is",round(sum,6))

output: backward difference table


**********************************************
x y 1st 2nd 3rd 4th
0 -3 6 2 6 0 0
1 3 8 8 6 0 0
2 11 16 14 6 0 0
3 27 30 20 0 0 0
4 57 50 0 0 0 0
5 107 0 0 0 0 0

the interpolating polynomial is 107

You might also like