MTC-233 Python Programing Language I Slips Semester III ANSWER
MTC-233 Python Programing Language I Slips Semester III ANSWER
Studies in Mathematics
S.Y.B.Sc. (Computer Science) Mathematics
Practical Examination
MTC-233: Python Programming Language-I
(CBCS 2019 Pattern)(Semester-III )Slip No.:-1
2. Write Python program to find the 10 term of the sequence of function f (x) = x2 +x.
➔ x = 10
➔ f_x = x**2 + x
➔ print(f_x)
3. Generate all the prime numbers between 1 to 100 using Python code.
➔ def is_prime(num):
➔ if num < 2:
➔ return False
➔ for i in range(2, int(num**0.5) + 1):
➔ if num % i == 0:
➔ return False
➔ return True
➔
import numpy as np
print(result)
2. Write Python program to evaluate interpolate value f (3) of the given data by La-granges
method.
-> def lagrange_interpolation(x_values, y_values, x):
total = 0
n = len(x_values)
for i in range(n):
term = y_values[i]
for j in range(n
):
if i != j:
term *= (x - x_values[j]) / (x_values[i] - x_values[j])
total += term
return total
x_values = [0, 1, 2, 5]
y_values = [5, 13, 22, 129]
f_3 = lagrange_interpolation(x_values, y_values, 3)
print(f_3)
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
x 0 1 2 5
Y=f(x) 5 13 22 129
->
for _ in range(max_iter):
return c
b=c
else:
a=c
return c
root = regula_falsi(f, 2, 3)
print(root)
∫ 10 1
2. Write Python program to estimate the value of the integral 2 (1+x)
dx using Trape-
zoidal rule (n=8).
->
➔ r=7
➔ volume = (4/3) * math.pi * r**3
➔ print(f"Volume of the sphere: {volume}")
➔ string1 = "Hello"
➔ string2 = "World!"
➔ print(result)
b. string1 = Good, string2 = Morning
➔ string1 = "Good"
➔ string2 = "Morning"
➔ result = string1 + " " + string2
➔ print(result)
1. Use python code find value of f (−2), f (0), f (2) where f (x) = x2–5x + 6.
➔ def f(x):
➔ print(f"f(-2): {f(-2)}")
➔ print(f"f(0): {f(0)}")
➔ print(f"f(2): {f(2)}")
2. Write Python program to find the 10 term of the sequence of function f (x) = x3+5x.
➔ def f(x):
➔ n = 10
➔ result = f(n)
3. Using sympy module of python, find the eigenvalues and corresponding eigenvectors
422
of the matrix A = 2 4 2 .
2 2 4
➔ from sympy import Matrix
➔
➔ A = Matrix([[4, 2, 2], [2, 4, 2], [2, 2, 4]])
➔ eigenvals = A.eigenvals()
➔ eigenvects = A.eigenvects()
➔
➔ print(f"Eigenvalues: {eigenvals}")
➔ print(f"Eigenvectors: {eigenvects}")
def f(x):
return 1/(1 + x**2)
a, b = 0, 1
n=4
h = (b - a) / n
x = np.linspace(a, b, n + 1)
y = f(x)
for _ in range(max_iter):
return c
b=c
else:
a=c
return c
root = regula_falsi(2, 3)
2. Write Python program to evaluate interpolate value f (3.5) of the given data by
Lagranges method.
x 0 1 2 5
Y=f(x) 2 3 12 147
2. Repeat the following string 11 times using the string operator ‘*’ on Python.
a. LATEX
b. MATLAB
->
print("LATEX " * 11)
print("MATLAB " * 11)
3. Generate all the prime numbers between 1 to 100 using Python code.
-> primes = []
is_prime = True
if num % i == 0:
is_prime = False
break
if is_prime:
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
primes.append(num)
➔
import numpy as np
from scipy.integrate import simps
f = lambda x: np.sin(x)
a, b = 0, np.pi
x = np.linspace(a, b, 100)
y = f(x)
result = simps(y, x)
print("Approximate value of the integral:", result)
2. Write Python program to evaluate third order forward difference of the given data.
x = [0, 1, 2, 3]
y = [1, 0, 1, 10]
f = lambda x: 1/x
x = np.linspace(1, 10, 100)
y = f(x)
result = trapezoid(y, x)
print("Approximate value of the integral:", result)
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
2. Write python program which deals with concatenation and repetition of lists.
List1 = [15, 20, 25, 30, 35, 40]
List2 = [7, 14, 21, 28, 35, 42]
3. Write Python code to find the square of odd numbers from 1 to 20 using while loop.
➔ num = 1
if num % 2 != 0:
num += 1
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
Q.2. Attempt any two of the following. [10]
identity_matrix = np.identity(10)
print(identity_matrix)
2. Zero matrix of order 7 × 3.
➔ zero_matrix = np.zeros((7, 3))
print(zero_matrix)
3. Ones matrix of order 5 × 4.
➔ ones_matrix = np.ones((5, 4))
print(ones_matrix)
2. Find the data type of the following data by using Python code.
a. number
1 0 5 " #
2 5
A = 2 1 6 and B = .
−1 4
3 4 0
➔ import numpy as np
determinant_A = np.linalg.det(A)
determinant_B = np.linalg.det(B)
print("Determinant of A:",
determinant_A)
print("Determinant of B:",
determinant_B)
import numpy as np
import numpy
from scipy.integrate as np
import simps
from scipy.integrate
f = lambda x: x * np.sin(x)
import simps
a, b = 0, np.pi
x = np.linspace(a, b, 100)
y = f(x) f = lambda x: x *
result = np.sin(x)
simps(y, x)
print("Approximate value of the integral:", result)
a, b = 0, np.pi
x = np.linspace(a, b,
100)
2. Write Python
y = f(x) program to estimate a root of an equation f (x) = 3x cos(x) 1
using Newton–Raphson
result = simps(y, x)method correct up to four decimal places.
def f(x):
➔print("Approximate
returnvalue
3 * x *ofnp.cos(x)
the -1
integral:", result)
import numpy as np
def f_prime(x):
from scipy.integrate
returnimport
3 * np.cos(x)
simps- 3 * x * np.sin(x)
f = lambda x: x tolerance=1e-4,
def newton_raphson(x0, * max_iterations=100):
np.sin(x)
for _ in range(max_iterations):
a, b = 0, np.pi
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
x1 = x0 - f(x0) / f_prime(x0)
if abs(x1 - x0) < tolerance:
return round(x1, 4)
x0 = x1
return round(x1, 4)
1. Write Python program to find all positive prime numbers less then given number n.
➔ def find_primes(n):
primes = []
is_prime = True
if num % i == 0:
is_prime = False
break
if is_prime:
primes.append(num)
return primes
2. Write Python program to evaluate f(2.5) by forward difference formula of the given
data.
x 0 1 2 3
Y=f(x) 2 1 2 10
➔ x = [0, 1, 2, 3]
y = [2, 1, 2, 10]
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
return result
value = 2.5
print("f(2.5) =", forward_difference(x, y, value))
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
1. Using sympy module of python find the following for the matrices
−1 1 0 9 0 3
A = 8 5 2 and B = 1 4 1 .
2 −6 2 1 0 −
(a) 2A + B.
(b) 3A – 5B.
(c) A−1.
(d) B3.
(e) AT + BT .
->
3. Use Python code to generate the square root of numbers from 21 to 49.
➔ import math
➔ import numpy as np
identity_matrix = np.identity(10)
print(identity_matrix)
2. Zero matrix of order 7 × 3.
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
2. Using linsolve command in python, solve the following system of linear equations.
x − 2y + 3z = 7
2x + y + z = 4
−3x + 2y − 2z = −10
x, y, z = symbols('x y z')
equations = [x - 2*y + 3*z - 7, 2*x + y + z - 4, -3*x + 2*y -
2*z + 10]
solution = linsolve(equations, x, y, z)
print("Solution of the system:", solution)
3. Generate all relatively prime numbers to 111 which are less than 150 using Python
code.
while b:
a, b = b, a % b
return a
1. Write Python code to find eigenvalues and corresponding eigenvectors of the matrix
1 3 3
A= 2 2 3
4 2 1
➔
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
➔ from sympy import Matrix
eigenvalues = A.eigenvals()
eigenvectors = A.eigenvects()
print("Eigenvalues:", eigenvalues)
print("Eigenvectors:", eigenvectors)
P, D = A.diagonalize()
def f_prime(x):
return 6 * x + 4
def f(x):
c=a
if f(c) == 0.0:
break
b=c
else:
a=c
return round(c, 4)
root = regula_falsi(2, 3)
x 1 2 3 4 5
Y=f(x) 41 62 65 50 17
➔ x = [1, 2, 3, 4, 5]
➔ y = [41, 62, 65, 50, 17]
➔
➔ def forward_difference(x, y, value):
➔ n = len(x)
➔ diff_table = [[0 for _ in range(n)] for _ in range(n)]
➔ for i in range(n):
➔ diff_table[i][0] = y[i]
➔ for j in range(1, n):
➔ for i in range(n - j):
➔ diff_table[i][j] = diff_table[i + 1][j - 1] - diff_table[i][j - 1]
➔
➔ result = y[0]
➔ h = x[1] - x[0]
➔ p = (value - x[0]) / h
➔ term = 1
➔ for i in range(1, n):
➔ term *= (p - i + 1) / i
➔ result += term * diff_table[0][i]
➔
➔ return round(result, 4)
➔
➔ print("f(3.5) =", forward_difference(x, y, 3.5))
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
a. 23 modulus 2 + 9 - (3 +7) × 10 ÷ 2
b. 35 × 10 floor division 3 + 15 modulus 3
c. 35 − 25 + 4 floor division 7
-> # (a) 23 modulus 2 + 9 - (3 + 7) × 10 ÷ 2
result_a = 23 % 2 + 9 - (3 + 7) * 10 / 2
print("Result (a):", result_a)
2. Write Python code to list name and roll number of 5 students in B.Sc.
(Computer science).
->students = [
3. Write Python code to find maximum and minimum element in the given list.
[7, 8, 71, 32, 49, −5, 7, 7, 0, 1, 6]
max_num = max(numbers)
min_num = min(numbers)
1. Using Python code construct identity matrix of order 10 and hence find determinant,
trace and transpose of it.
->import numpy as np
identity_matrix = np.identity(10)
determinant = np.linalg.det(identity_matrix)
trace = np.trace(identity_matrix)
transpose = identity_matrix.T
print("Determinant:", determinant)
print("Trace:", trace)
print("Transpose:\n", transpose)
2. Write Python code to find the value of function f (x, y) = x2 − 2xy + 4 at the points
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
(2,0) (1,-1).
➔ def f(x, y):
return x**2 - 2 * x * y + 4
value_1 = f(2, 0)
value_2 = f(1, -1)
3. Find number between 1 to 200 which are divisible by 7 using Python code.
def f(x):
return np.sin(x)
a = 0 # Lower limit
b = np.pi # Upper limit
n = 5 # Number of intervals (must be even)
h = (b - a) / n
x = np.linspace(a, b, n + 1)
y = f(x)
" #
3 −2
A=
6 −4
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
➔ def f(x):
c=a
if f(c) == 0.0:
break
b=c
else:
a=c
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
return round(c, 4)
root = regula_falsi(2, 3)
a = 1 # Lower limit
b = 5 # Upper limit
n = 100 # Number of intervals
h = (b - a) / n
x = np.linspace(a, b, n + 1)
y = f(x)
a. z1 + z2
b. z1 − z2
c. z1 ∗ z2
➔ # Define complex numbers
z1 = 5 + 3j
z2 = -5 + 7j
# (a) z1 + z2
sum_result = z1 + z2
print("z1 + z2:", sum_result)
# (b) z1 - z2
diff_result = z1 - z2
print("z1 - z2:", diff_result)
# (c) z1 * z2
prod_result = z1 * z2
print("z1 * z2:", prod_result)
2. Repeat the following string 7 times using the string operator ‘*’ on Python.
a. Complex Number
b. Real Number
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
➔ # (a) Repeat "Complex Number" 7 times
complex_str = "Complex Number" * 7
print(complex_str)
1. Using Python code construct 0nes matrix of order 10 × 10 and hence find determi-
nant, trace and transpose of it.
➔ import numpy as np
determinant = np.linalg.det(ones_matrix)
trace = np.trace(ones_matrix)
transpose = ones_matrix.T
print("Determinant:", determinant)
print("Trace:", trace)
print("Transpose:\n", transpose)
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
2. Write Python code to obtained f (−1), f (0), f (1) of the f (x) = x3 − 4x − 9.
➔ def f(x):
return x**3 - 4*x - 9
f_neg1 = f(-1)
f_0 = f(0)
f_1 = f(1)
3. Generate all the prime numbers between 500 to 1000 using Python program.
➔ def is_prime(num):
if num < 2:
return False
if num % i == 0:
return False
return True
➔ import numpy as np
def f(x):
return x**3
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
a = 0 # Lower limit
b = 5 # Upper limit
n = 10 # Number of intervals (must be even)
h = (b - a) / n
x = np.linspace(a, b, n + 1)
y = f(x)
i
m
2. Writep Python program to evaluate interpolate value f (3) of the given data.
o
r ➔ from scipy.interpolate import lagrange
t
n
u
m
# Givenpdata points
y
x = [0, 1,a 2, 5]
s
y = [2, 3,
n 12, 147]
p
d
# Createe the Lagrange polynomial
f
polynomial
f = lagrange(x, y)
(
x
)
# :
Evaluate the polynomial at x = 3
r
f_3 = polynomial(3)
e
t
print("Interpolated
u value of f(3):", f_3)
r
n
x x 0 1 2 5
* Y=f(x) 2 3 12 147
*
3
a
=
0
#
L
o
w
e
r
l
i
m
i
t
b
=
5
#
U
p
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
2. Write Python program to evaluate f(2.8) using backward difference formula of the
given data.
x 0 1 2 3
Y=f(x) 1 0 1 10
➔ import numpy as np
return bd
return result
# Perform calculations
result_sum = a + c
result_product = a * b
result_cd = c * d
result_division = a / b
result_expression = a * (b + c)
# Print results
print("a + c =", result_sum)
print("ab =", result_product)
print("cd =", result_cd)
print("a/b =", result_division)
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
print("a(b + c) =", result_expression)
# Concatenate strings
result_a = string1_a + string2_a
print(result_a)
string1_b = "God"
string2_b = " is Great"
# Concatenate strings
result_b = string1_b + string2_b
print(result_b)
3. Write Python code to find area and circumference of circle with radius 14.
radius = 14
# Print results
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
print("Area of the circle =", area)
1. Using Python code logically verify associativity of matrices with respective to matrix
addition (use proper matrices).
# Define matrices
# Check associativity: (A + B) + C == A + (B + C)
left_side = np.add(A, B)
left_side = np.add(left_side, C)
right_side = np.add(B, C)
# Print results
print(left_side)
print(right_side)
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
# Verify if they are equal
3. Using Python code, find determinant and inverse of the matrix if exist.
4 2 2
A = 2 4 2
2 2 4
-> # Define matrix A
A = np.array([[4, 2, 2],
[2, 4, 2],
[2, 2, 4]])
# Calculate determinant
determinant = np.linalg.det(A)
# Print results
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
print("Determinant of A =", determinant)
if inverse is not None:
print("Inverse of A:\n", inverse)
else:
print("Inverse does not exist (determinant is zero).")
# Function to integrate
def f(x):
return 1 / (1 + 2 * x) # Example function
h = (b - a) / n
integral = f(a) + f(b)
integral *= h / 3
return integral
# Parameters
a=1
b=1
n=6
x = np.array([1, 2, 3, 4, 5])
def forward_difference(Y):
n = len(Y)
diff_table[:, 0] = Y
return diff_table
diff_table = forward_difference(Y)
print("The function has the same sign at the endpoints. No root found.")
return None
for i in range(max_iter):
return c
b=c
else:
a=c
return c
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
a=2
b=3
tolerance = 1e-6
max_iterations = 100
def f(x):
return 4 * (2 * x**2 - 4 * x + 1) # Define the function
a. 30 modulus 2 + 7 - (3 +9) × 20 ÷ 5
b. 30 × 10 floor division 3 + 30 modulus 3
c. 55 - 53 + 7 floor division 7
-> import math
(a) sin30
(b) pi
(c) e
(d) cos30
-> # Use math module for mathematical calculations
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
print("sin(30 degrees):", math.sin(math.radians(30))) # sin(30)
print("Value of pi:", math.pi) # pi
print("Value of e:", math.e) #e
print("cos(30 degrees):", math.cos(math.radians(30))) # cos(30)
string = 'MATHEMATICS'
3. Write Python code to verify (AB)−1 = B−1A−1 (Use proper matrices A and B).
[0, 1]])
print("Eigenvalues:", eigenvalues)
print("Eigenvectors:\n", eigenvectors)
Q.3. a. Attempt any one of the following. [7]
∫ 10
1. Write Python program to estimate the value of the integral 1 (x2 + 5x)dx using
Simpson’s ( 1 )rd rule (n=5).
->
from scipy.integrate import quad
2. Write Python program to evaluate interpolate value f (2.5) of the given data.
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
x 1 2 3 4
Y=f(x) 1 8 27 64
return None
for i in range(max_iter):
return c
b=c
else:
a=c
return c
a=2
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
b=3
tolerance = 1e-6
max_iterations = 100
2. Write Python program to evaluate fourth order forward difference of the given data.
-> # Given data
x_data = np.array([1, 2, 3, 4, 5])
y_data = np.array([40, 60, 65, 50, 18])
return diff_table
# b. Multiply List1 by 3
multiplied_list1 = 3 * List1
print("3 times List1:", multiplied_list1)
# c. Multiply List2 by 5
multiplied_list2 = 5 * List2
print("5 times List2:", multiplied_list2)
3. Write Python code to find area of triangle whose base is 10 and height is 15.
base = 10
height = 15
2. Write Python program to find the value of function f (x) = x2 + x, (−5 ≤ x ≤ 5).
1 0 5 " #
2 5
A = 2 1 6 and B = .
−1 4
3 4 0
# Define matrices A and B
A = np.array([[1, 0, 5],
[2, 1, 6],
[3, 4, 0]])
B = np.array([[2, 5],
[-1, 4]])
# Calculate determinants
det_A = np.linalg.det(A)
det_B = np.linalg.det(B)
print("Determinant of matrix
A:", det_A)
print("Determinant of matrix
B:", det_B)
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
2. Write Python program to evaluate interpolated value f (2.7) of the given data
f(2)=0.69315,f(2.5)=0.91629,f(3)=1.09861. # Given data for interpolation
3. x_data = [2, 2.5, 3]
4. y_data = [0.69315, 0.91629, 1.09861]
5.
6. # Interpolation using linear interpolation formula
7. def linear_interpolation(x, x_data, y_data):
8. for i in range(len(x_data) - 1):
9. if x_data[i] <= x <= x_data[i + 1]:
10. return y_data[i] + (y_data[i + 1] - y_data[i]) * (x - x_data[i]) /
(x_data[i + 1] - x_data[i])
11. return None
12.
13. # Estimate f(2.7)
14. interpolated_value = linear_interpolation(2.7, x_data, y_data)
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
15. print("Interpolated value f(2.7):", interpolated_value)->
return None
for i in range(max_iter):
return c
b=c
else:
a=c
return c
a=2
b=3
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
tolerance = 1e-6
max_iterations = 100
return total * h
# b. Concatenate strings
L = "XY" + "pqr"
print("Value of L:", L)
1 1 1
A = 0 1 1
0 0 1
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
(a) Eigen Value of A.
(b) determinant of A.
(c) inverse of A.
-> import numpy as np
# a. Eigenvalues of A
eigenvalues = np.linalg.eigvals(A)
print("Eigenvalues of A:", eigenvalues)
# b. Determinant of A
determinant_A = np.linalg.det(A)
print("Determinant of A:", determinant_A)
reversed_S = S[::-1]
2. Use linsolve command in python to solve the following system of linear equations.
x − 2y + 3z = 7
2x + y + z = 4
−3x + 2y − 2z = −10
->from sympy import symbols, Eq, linsolve
3. Generate all the prime numbers between 51 to 100 using Python program.
if num < 2:
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
return False
if num % i == 0:
return False
return True
∫ 10 x
1. Write Python program to estimate the value of the integral 0 e dx using Simpson’s
( 3 )th rule(Take h = 0.5).
->
def f_prime(x):
return 5*x**4 + 3 # Derivative of f
return None
for _ in range(max_iter):
return c
b=c
else:
a=c
return c
tolerance = 1e-6
max_iterations = 100
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
# Find the root using Regula-Falsi method
2. Write Python program to evaluate interpolate value f (153) of the given data.
-> # Given data for interpolation
x_data = [150, 152, 154, 155]
y_data = [12.247, 12.329, 12.410, 12.490]
# Estimate f(153)
interpolated_value_153 = linear_interpolation(153, x_data, y_data)
print("Interpolated value f(153):", interpolated_value_153)
x 150 152 154 155
Y=f(x) 12.247 12.329 12.410 12.490
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
a. 23 modulus 2 + 9 - (3 +7) × 10 ÷ 2
b. 35 × 10 floor division 3 + 15 modulus 3
c. 35 - 25 + 4 floor division 7
-> # a. Evaluate the expression
expr_a = 23 % 2 + 9 - (3 + 7) * 10 / 2
print("Result of Expression a:", expr_a)
2. Use while command on Python to find odd positive integer between 25 to 50.
-> # Find odd positive integers between 25 and 50 using a while loop
odd_numbers = []
num = 25
while num <= 50:
if num % 2 != 0: # Check if the number is odd
odd_numbers.append(num)
num += 1
4
1 0 5 −1 apply the following operations by using python.
3. For matrix A = 2 1 6 2
3 4 0
1 3 3
2 2 3 .
4 2 1
-> # Define the matrix for eigenvalues and
eigenvectors
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
B = np.array([[1, 3, 3],
[2, 2, 3],
[4, 2, 1]])
2. Write Python program to find the product of n natural numbers using while loop.
def product_of_n_natural_numbers(n):
product = 1
count = 1
product *= count
count += 1
return product
n = 5 # Change n as needed
result = product_of_n_natural_numbers(n)
if num < 2:
return False
if num % i == 0:
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
return False
return True
"
3 2#
−6 −4
# Diagonal matrix D
D = np.diag(eigenvalues_C)
P = eigenvectors_C
print("Matrix P (eigenvectors):\n", P)
print("Diagonal matrix D (eigenvalues):\n", D)
return None
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
for _ in range(max_iter):
return c
b=c
else:
a=c
return c
tolerance = 1e-6
max_iterations = 100
a. z1 + z2
b. z1 − z2
c. z1 ∗ z2
-> # Define complex numbers
z1 = 3 + 2j
z2 = -4 + 1j
# a. Addition
addition = z1 + z2
print("z1 + z2 =", addition)
# b. Subtraction
subtraction = z1 - z2
print("z1 - z2 =", subtraction)
# c. Multiplication
multiplication = z1 * z2
print("z1 * z2 =", multiplication)
2. Use Python code to find area and circumference of square whose length is 5.
-> # Define the length of the square
length = 5
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
3
-> # Reverse the string S
S = [1, 2, 3, 4, 5, 6, 7, 8, 9]
reversed_S = S[::-1]
print("Reversed string S:", reversed_S)
2. Write Python program to evaluate interpolate value f (5.5) of the given data.
-> import numpy as np
# Evaluate f(5.5)
interpolated_value = linear_interpolation(5.5, x_data, y_data)
print("Interpolated value f(5.5):", interpolated_value)
x 3 5 7 9
Y=f(x) 5 7 27 64
for _ in range(max_iter):
c = b - (f(b) * (a - b)) / (f(a) - f(b))
if abs(f(c)) < tol:
return c
if f(c) * f(a) < 0:
b=c
else:
a=c
return c
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
x 1 2 3 4 5
Y=f(x) 40 60 65 50 18
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
(a) print(a+c)
(b) print(a*b)
(c) print(c**d)
(d) print(a/b)
(e) Expression: 3 + ( 9 - 2) / 7 * 2 ** 2
➔ # Define the variables
a=4
b=6
c=8
d = 12
# (a) Print a + c
print(a + c)
# (b) Print a * b
print(a * b)
# (c) Print c ** d
print(c ** d)
# (d) Print a / b
print(a / b)
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
# (e) Evaluate and print the expression
expression_result = 3 + (9 - 2) / 7 * 2 ** 2
print(expression_result)
# b. String concatenation
string1_b = "Good"
string2_b = "Morning"
result_b = string1_b + " " + string2_b
print(result_b)
➔ # List of names
for i in names:
# 1. Show that A + B = B + A
addition_commutative = np.array_equal(A + B, B + A)
print("A + B = B + A:", addition_commutative)
# 2. Find A - B
difference = A - B
print("A - B:\n", difference)
4 2 2
A = 2 4 2 .
2 2 4
print("Eigenvalues:", eigenvalues)
print("Eigenvectors:", eigenvectors)
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
def f_prime(x):
x = initial_guess
for _ in range(max_iter):
return x_new
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
x = x_new
return None
initial_guess = 2
tolerance = 1e-6
max_iterations = 100
return None
for _ in range(max_iter):
return c
b=c
else:
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
a=c
return c
tolerance_rf = 1e-6
max_iterations_rf = 100
h = x_data[1] - x_data[0]
if idx == len(y_data) - 1:
x 1 2 3 4 5
Y=f(x) 30 50 65 40 18
.
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
(a) sin75
(b) pi/2
(c) e
(d) cos56
-> import math
# (c) Calculate e
e_value = math.e
print("e:", e_value)
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
3. Write Python program to find diameter, area, circumference of the circle with radius
is 5.
radius = 5
diameter = 2 * radius
# Print results
print("Diameter:", diameter)
print("Area:", area)
print("Circumference:", circumference)
1. Using Python code construct any three matrices A,B and C to show that
(A+B)+C=A+(B+C).
# Check (A + B) + C
left_side = A + B + C
# Check A + (B + C)
right_side = A + (B + C)
# Print results
2. Using python find the eigenvalues and corresponding eigenvectors of the matrix
" #
3 −2
.
6 −4
-> # Define the matrix
matrix = np.array([[3, -2], [1, 1]])
print("Eigenvalues:", eigenvalues)
print("Eigenvectors:\n", eigenvectors)
3. Generate all prime numbers between 1000 to 2000 using Python program.
return False
if n % i == 0:
return False
return True
def f_prime(x):
return 3 * (np.cos(x) - x * np.sin(x))
return None
for _ in range(max_iter):
return c
b=c
else:
a=c
return c
tolerance_rf = 1e-4
max_iterations_rf = 100
2. Write Python program to evaluate interpolate value f(2.2) of the given data
f(2)=0.593,f(2.5)=0.816,f(3)=1.078.
# Example usage
number = -42.5
print("Absolute value of", number, "is", absolute_value(number))
# (b) 7 * List1
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
result_multiply_7 = 7 * List1
print("7 * List1:", result_multiply_7)
# (c) 11 * List2
result_multiply_11 = 11 * List2
print("11 * List2:", result_multiply_11)
# Given radius
radius = 5
# Print results
1. Using Python code, find percentage of marks 70,80, 55, 78, 65 in five subject out of
100 each.
total_subjects = len(marks)
# Calculate percentage
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
percentage = sum(marks) / total_marks * 100
2. Using sympy module of python, find the following terms of vector x = [1, -5, 0] and
y = [2, 3, -1].
a. 5x
b. x+y
c. x-3y
-> import numpy as np
# Define vectors
x = np.array([1, -5, 0])
y = np.array([2, 3, -1])
# (a) 5x
result_5x = 5 * x
print("5x:", result_5x)
# (b) x + y
result_add = x + y
print("x + y:", result_add)
# (c) x - 3y
result_subtract = x - 3 * y
print("x - 3y:", result_subtract)
1 0 5 " #
2 5
A = 2 1 6 and B =
−1 4→
3 4 0
->
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
import numpy as np
# Print results
print("Determinant of A:", det_A)
print("Inverse of A:\n", inv_A)
print("Determinant of B:", det_B)
print("Inverse of B:\n", inv_B)
Q.3. a. Attempt any one of the following. [7]
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
∫π
1. Write Python program to estimate the value of the integral 0 sin(x)dx using Simp-
son’s ( 1 )rd rule (n=6).
->
import numpy as np
from scipy.integrate import simpson
def f_prime(x):
return 5*x**4 + 5
return None
for _ in range(max_iter):
return c
b=c
else:
a=c
return c
def f_regula(x):
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
return x**2 - 2*x - 1
tolerance_rf = 1e-4
max_iterations_rf = 100
integral *= h
return integral
wonderful = "wonderful"
def print_messages():
# Local variable
bad = "bad"
print(f"Python is {bad}")
print(f"Python is {wonderful}")
print_messages()
2. Write Python code to evaluate eigen value and eigen vector of the following matrix.
1 1 1
A = 0 1 1
0 0 1
# Print results
print("Eigenvalues:", eigenvalues)
print("Eigenvectors:\n", eigenvectors)
3. Write Python code, find a, b and c such that a2 + b2 = c2.(where 1 ≤ a, b, c ≤ 50)
-># Find Pythagorean triples
triples = []
1. Using Python code construct any two matrices A and B to show that
(AB)−1 = B−1A−1.
->import numpy as np
A = np.array([[1, 2],
[3, 4]])
B = np.array([[2, 0],
[1, 2]])
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
AB = np.dot(A, B)
AB_inv = np.linalg.inv(AB)
# Print results
2. Use linsolve code in python to solve the following system of linear equations.
x − 2y + 3z = 7
2x + y + z = 4
−3x + 2y − 2z = −10
# Define symbols
x, y, z = symbols('x y z')
# Define equations
eq1 = Eq(x - 2*y + 3*z, 7)
eq2 = Eq(2*x + y + z, 4)
eq3 = Eq(-3*x + 2*y - 2*z, -10)
# Print results
print("Trace of the matrix:", matrix_trace)
print("Transpose of the matrix:\n", matrix_transpose)
1. Write Python program to find f(3) of the functional value f(1)=2, f(2)=10, f(4)=68by
using Lagrange method.
for i in range(n):
term = y_values[i]
for j in range(n):
if j != i:
term *= (x - x_values[j]) / (x_values[i] - x_values[j])
total += term
return total
def f_prime_newton(x):
return 5*x**4 + 5
return None
for _ in range(max_iter):
return c
b=c
else:
a=c
return c
tolerance_rf = 1e-4
max_iterations_rf = 100
integral *= h
return integral
2. Use Python code to find hypotenuse of triangle whose sides are 12 and 5.
-> import math
# Given sides
side_a = 12
side_b = 5
3. Use Python code to remove all digits after decimal of the given Number 125312.3142.
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
-> # Given number
number = 125312.3142
number_without_decimal = int(number)
(a) A+B
(b) AT
(c) A−1
-> import numpy as np
B = np.array([[4, 3],
[5, 4]])
# (a) A + B
matrix_sum = A + B
# Print results
print("A + B:\n", matrix_sum)
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
print("Transpose of A:\n", A_transpose)
print("Inverse of A:\n", A_inverse)
2. Use while code on Python to find sum of first twenty natural number.
n=1
sum_natural_numbers = 0
sum_natural_numbers += n
n += 1
"
3 2#
−6 −4
# Diagonal matrix D
D = np.diag(eigenvalues)
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
# Matrix P (eigenvectors)
P = eigenvectors
# Print results
print("Diagonal matrix D:\n", D)
print("Matrix P (eigenvectors):\n", P)
h = (b - a) / n
integral = f(a) + f(b)
integral *= h / 3
return integral
# Function to integrate
def f(x):
return 1 # The integral of dx is simply 1
# Parameters
a=1
b=3
n = 4 # Even number
for i in range(n):
term = y_values[i]
for j in range(n):
if j != i:
term *= (x - x_values[j]) / (x_values[i] - x_values[j])
total += term
return total
# Estimate f(2.9)
f_2_9 = lagrange_interpolation(x_data, y_data, 2.9)
print("Interpolated value f(2.9):", f_2_9)
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
x 1 2 3 4
Y=f(x) 11 9 27 64
return None
for _ in range(max_iter):
return c
b=c
else:
a=c
return c
max_iterations_rf = 100
def f_trap_cos(x):
return np.cos(x)
integral *= h
return integral
def check_number(num):
if num == 0:
elif num % 2 == 0:
else:
result = check_number(number)
print(result)
3. Write Python code to list name and birth date of 5 students in your class.
students = [
print("List of Students:")
1 2 2
A= 2 1 2
2 2 1
-> import numpy as np
# Print results
print("Matrix A:\n", A)
print("Transpose of A:\n", A_transpose)
print("Inverse of A:\n", A_inverse)
5 2 5 4
A = 10 3 4 6
2 0 −1 11
# Print results
print("Matrix A:\n", A)
print("Row Echelon Form (Upper Triangular Matrix U):\n", U)
print("Rank of Matrix A:", rank_A)
2 −1 2 7
A= 4 7 3 4
4 2 0 −1
[4, 2, 0, -1]])
# LU decomposition
P, L, U = lu(A)
# Print results
print("Matrix A:\n", A)
h = (b - a) / n
integral = f(a) + f(b)
integral *= h / 3
return integral
# Parameters
a=0
b=1
n = 6 # Even number
0 1+x
return None
for _ in range(max_iter):
return c
b=c
else:
a=c
return c
tolerance_rf = 1e-4
max_iterations_rf = 100
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
integral *= h
return integral
2. Write python program to find sin(42)0 using Newton backward interpolation for-
mula for the data:
sin300 = 0.5, sin350 = 0.5736, sin400 = 0.6428, sin450 = 0.7071.
-> # Given data for interpolation
x_data = [30, 35, 40, 45]
y_data = [0.5, 0.5736, 0.6428, 0.7071]
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
2. Use Python code to find sum of square of first twenty five natural numbers.
-> # Function to find the sum of squares of the first n natural numbers
def sum_of_squares(n):
result = sum_of_squares(n)
print(f"The sum of squares of the first {n} natural numbers is: {result}")
def positive_divisors(n):
return divisors
divisors = positive_divisors(n)
1. Write python code to display tuple ‘I am Indian ’ and the second letter in this tuple
print("Tuple:", my_tuple)
2. Write python code to display the matrix whose all entries are 10 and order is (4,6).
print(matrix)
"
3 2#
−6 −4
# Diagonal matrix D
D = np.diag(eigenvalues)
# Matrix P (eigenvectors)
P = eigenvectors
# Display results
print("Matrix A:\n", A)
print("Matrix P (eigenvectors):\n", P)
print("Diagonal Matrix D (eigenvalues):\n", D)
import numpy as np
def f(x):
return np.cos(x)
h = (b - a) / n
integral = f(a) + f(b)
integral *= h / 3
return integral
# Parameters
a=3
b=5
n = 6 # Even number of intervals
for i in range(n):
term = y_data[i]
for j in range(n):
if j != i:
term *= (x - x_data[j]) / (x_data[i] - x_data[j])
total_sum += term
return total_sum
x 1 2 3 6
Y=f(x) 2 6 12 42
for _ in range(max_iter):
c = b - (f_rf(b) * (a - b)) / (f_rf(a) - f_rf(b))
if abs(f_rf(c)) < tol:
return c
if f_rf(c) * f_rf(a) < 0:
b=c
else:
a=c
return c
integral *= h
return integral
2. Write Python code to list name and birth date of 5 students in your class.
students = [
]
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
return 4 * a + b
2. Write python code to perform the R2 + 2R1 row operation on given matrix.
1 1 1
R= 2 2 2
3 3 3
[3, 3, 3]])
3. Write python code to find all the eigen values and the eigen vectors of the matrix.
2 −1 −1 0
−1 3 −1 −1
−1 −1 3 −1
−1 −1 −1 2
print("Eigenvalues:\n", eigenvalues)
print("\nEigenvectors:\n", eigenvectors)
def df_newton(x):
return 5*x**4 + 3 # Derivative of the function
# Parameters
x0 = -1 # Initial guess
tolerance = 1e-6
max_iterations = 100
2. Write a Python program to evaluate interpolate value f (3) of the given data.
-> # Given data points
x_data = np.array([1, 2, 3, 4])
y_data = np.array([11, 22, 33, 66])
for i in range(n):
term = y_data[i]
for j in range(n):
if j != i:
term *= (x - x_data[j]) / (x_data[i] - x_data[j])
total_sum += term
return total_sum
x 1 2 3 4
Y=f(x) 11 22 33 66
return None
for _ in range(max_iter):
return c
b=c
else:
a=c
return c
tolerance_rf = 1e-6
max_iterations_rf = 100
2. Write Python program to find sin(37)0 using Newton backward interpolation for-
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
mula for the data:
sin300 = 0.5, sin350 = 0.5736, sin400 = 0.6428, sin450 = 0.7071.
-> # Given data points
x_values = np.array([30, 35, 40, 45])
y_values = np.array([0.5, 0.5736, 0.6428, 0.7071])
(a) (x or y) and z.
(b) (x and y) or not z.
(c) (x and not y) or (x and z).
-> # Given boolean values
x = True
y = True
z = False
string = 'MATHEMATICS'
substring = string[3:9]
1. Write Python program that prints whether the given number is positive, negative
or zero.
def check_number(num):
if num > 0:
return "Positive"
return "Negative"
else:
return "Zero"
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
# Input number
result = check_number(number)
# Input value of n
n = int(input("Enter the value of n: "))
sum_n = sum_of_natural_numbers(n)
print(f"The sum of first {n} natural numbers is: {sum_n}")
1 −3 2 −4
− 3 9 −1 5
A=
5 −2 6 −3
−4 12 2 7
Find the Null space, Column space and rank of the matrix.
[3, 9, 1, 5],
rank = np.linalg.matrix_rank(matrix)
u, s, vh = np.linalg.svd(matrix)
# Display results
def df_newton_raphson(x):
for _ in range(max_iter):
x1 = x0 - f_newton_raphson(x0) / df_newton_raphson(x0)
return x1
x0 = x1
return None
# Parameters
tolerance = 1e-6
max_iterations = 100
2. Write Python program to evaluate interpolate value f (2) of the given data.
for i in range(n):
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
term = y_data[i]
for j in range(n):
if j != i:
term *= (x - x_data[j]) / (x_data[i] - x_data[j])
total_sum += term
return total_sum
x 11 12 13 14
Y=f(x) 21 19 27 64
return None
for _ in range(max_iter):
return c
b=c
else:
a=c
return c
# Parameters
max_iterations_rf = 100
1 2 3
A= 2 5 3
1 0 8
Find the transpose of the matrix, determinant, inverse of the matrix. Also reduce
the matrix to reduced row echelon form and diagonalize it.
[1, 0, 8]])
transpose_matrix = np.transpose(matrix)
determinant = np.linalg.det(matrix)
try:
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
inverse_matrix = np.linalg.inv(matrix)
except np.linalg.LinAlgError:
inverse_matrix = None
import sympy as sp
sympy_matrix = sp.Matrix(matrix)
rref_matrix, _ = sympy_matrix.rref()
# Display results
print("Eigenvalues:\n", eigenvalues)
print("Eigenvectors:\n", eigenvectors)
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
# Input value of n
n = int(input("Enter the value of n: "))
sum_n = sum_of_natural_numbers(n)
print(f"The sum of the first {n} natural numbers is: {sum_n}")
2. Write Python code to prints all integers between 1 to 100 that are divisible by 3
and 7.
-> # Printing all integers between 1 to 100 that are divisible by 3 and 7
if i % 3 == 0 and i % 7 == 0:
3. Write Python code to prints all integers between 1 to n, which are relatively prime
to n.
def relatively_prime(n):
result = []
if gcd(i, n) == 1:
result.append(i)
return result
# Input value of n
relatively_prime_numbers = relatively_prime(n)
1 2 3
A= 2 5 7
4 9 11
-> import numpy as np
# Display results
print("Determinant of the matrix A:",
determinant)
print("Transpose of the matrix A:\n",
transpose)
print("Inverse of the matrix A:\n", inverse)
2. Write Python program to find the roots of the quadratic equation ax2 + bx + c = 0.
# Input coefficients
roots = quadratic_roots(a, b, c)
3x − 7y − 2z = −7
−3x + 5y + z = 5
6x − 4y = 2
-> import numpy as np
from scipy.linalg import lu, solve
# LU Factorization
P, L, U = lu(A)
− −
3
-> import numpy as np
# Function to integrate
def f(x):
return 1 # The function f(x) = 1 is a constant function
h = (b - a) / n
integral = f(a) + f(b)
integral *= h / 3
return integral
for _ in range(max_iter):
c = b - (f_regula_falsi(b) * (a - b)) / (f_regula_falsi(a) - f_regula_falsi(b))
if abs(f_regula_falsi(c)) < tol:
return c
if f_regula_falsi(c) * f_regula_falsi(a) < 0:
b=c
else:
a=c
return c
# Parameters
a_rf = 0 # Lower bound
b_rf = 5 # Upper bound
tolerance_rf = 1e-6
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
max_iterations_rf = 100
integral *= h
return integral
2. Write Python program to find sin(35)0 using Newton backward interpolation for-
mula for the data:
sin300 = 0.5, sin350 = 0.5736, sin400 = 0.6428, sin450 = 0.7071.
-> # Given data
x_data = np.array([30, 35, 40, 45])
y_data = np.array([0.5, 0.5736, 0.6428, 0.7071])
# Interpolation
result = diff_table[n - 1][0]
for j in range(1, n):
term = diff_table[n - j - 1][j]
for m in range(j):
term
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
2. Use Python code to find the remainder after dividing by ‘n’ to any integers.
return num % n
# Input values
3. Write Python program to prints all integers between 1 to 50 that are divisible by 3
and 7.
if i % 3 == 0 and i % 7 == 0:
print(perfect_squares)
2. Write Python program to prints whether the given natural number is divisible by 5
and less than 100.
def check_number(num):
return True
return False
# Input value
else:
"
2 3#
−4 −6
# Display results
print("Matrix P (eigenvectors):\n", P)
print("Matrix D (eigenvalues diagonal matrix):\n", D)
# Function to integrate
def f(x):
return 3 * np.cos(x)
h = (b - a) / n
integral = f(a) + f(b)
integral *= h / 3
return integral
# Given data
x_data = np.array([1, 2, 3, 4])
y_data = np.array([11, 10, 15, 10])
# Backward differences
diff_table = np.zeros((n, n))
diff_table[:, 0] = y_data
# Interpolating value
result = diff_table[n - 1][0]
for j in range(1, k + 1):
term = diff_table[n - j - 1][j]
for m in range(j):
term *= (k - m) / (m + 1)
result += term
return result
# Evaluate f(1.9)
value_at_1_9 = backward_difference(1.9, x_data, y_data)
print(f"Value of f(1.9) using backward difference formula: {value_at_1_9:.2f}")
x 1 2 3 4
Y=f(x) 11 10 15 10
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
return None
for _ in range(max_iter):
return c
b=c
else:
a=c
return c
# Parameters
tolerance_rf = 1e-6
max_iterations_rf = 100
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
2. Write Python program to evaluate interpolate value f (17) of the given data.
# Interpolating value
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
result = y_data[0]
for j in range(1, k + 1):
term = diff_table[0][j]
for m in range(j):
term *= (k - m) / (m + 1)
result += term
return result
# Evaluate f(17)
value_at_17 = newton_forward_interpolation(17, x_data, y_data)
print(f"Interpolated value of f(17): {value_at_17:.2f
x 12 22 32 62
Y=f(x) 25 65 125 425
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
def check_divisibility(num):
divisors = [3, 5, 7]
return results
# Input value
divisible_by = check_divisibility(num)
if divisible_by:
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
print(f"{num} is divisible by: {', '.join(map(str, divisible_by))}")
else:
4 2 4 5 2 3
A = 4 −1 1 & B = 3 −7 5
2 4 2 3 1 −1
B = np.array([[5, 2, 3],
[3, 7, 5],
[3, 1, -1]])
# Adding A and B
# Note: Since A is 2x3 and B is
3x3, we cannot add them
directly.
# We will need to reshape A or
B to perform addition (if they
are compatible)
# Here we will perform addition
only if they are of the same
shape.
# Hence, we will print that they
are incompatible for addition.
try:
A_plus_B = A + B
print("A + B:\n", A_plus_B)
except ValueError as e:
print(f"Cannot add matrices
A and B: {e}")
# Multiplying A and B
B_times_A = np.dot(B, A) #
Matrix multiplication
print("B * A:\n", B_times_A)
1. Write Python program to find the area and circumference of a circle with radius r.
def circle_properties(radius):
2. Use Python code to solve the following system of equations by gauss elimination
method
x + y + 2z = 7
−x − 2y + 3z = 6
3x − 7y + 6z = 1
-> import
numpy as np
# Coefficient
matrix
A=
np.array([[1, 1,
2],
[-1, -2,
3],
[3, -7,
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
6]])
# Constants
B=
np.array([7, 6,
1])
# Solving the
system of
equations
using numpy
solution =
np.linalg.solve(
A, B)
print("Solution
(x, y, z):",
solution)
3. Write Python code to find eigen values, eigen vectors of the matrix and determine
whether the matrix is diagonalizable.
1 −1 1
A = −1 1 −1
1 −1 1
# Check if the matrix is diagonalizable (if there are n linearly independent eigenvectors)
is_diagonalizable = len(eigenvalues) == len(set(eigenvalues))
# Display results
print("Eigenvalues:", eigenvalues)
print("Eigenvectors:\n", eigenvectors)
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
[7]
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
1. Write Python program to find the approximate root of the equation f (x) = x2 − 50
by using Newton Raphson method.
-> # Function f(x) = x^2 - 50
def f(x):
return x**2 - 50
# Derivative f'(x) = 2x
def f_derivative(x):
return 2*x
# Newton-Raphson method
def newton_raphson(initial_guess, tolerance, max_iterations):
x = initial_guess
for _ in range(max_iterations):
x_new = x - f(x) / f_derivative(x)
if abs(x_new - x) < tolerance:
return x_new
x = x_new
return x
# Parameters
initial_guess = 7.0
tolerance = 1e-6
max_iterations = 100
2. Write Python program to evaluate f(2.4) by forward difference formula of the given
data.
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
-> import numpy as np
# Given data
n = len(x_data)
h = x_data[1] - x_data[0]
if k >= n:
diff_table[:, 0] = y_data
# Interpolating value
result = y_data[0]
term = diff_table[0][j]
MTC-233 Python Programming Language-I Savitribai Phule Pune University, Pune
for m in range(j):
term *= (k - m) / (m + 1)
result += term
return result
# Evaluate f(2.4)
x 0 1 2 3
Y=f(x) 11 10 11 21
# Function to integrate
def integrand(x):
return np.sin(np.pi * x) ** 2