Python Practical solution
Python Practical solution
In [ ]: #Slip 1
#Q1.2
In [1]: string1="python"
>>> print(string1*9)
pythonpythonpythonpythonpythonpythonpythonpythonpython
In [ ]: string="mathematics"
>>> print(string*9)
In [ ]: #Q2.1
[[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]
Out[4]: ⎡0 0 0⎤
⎢0 0 0⎥
⎢ ⎥
⎢0 0⎥
⎢ 0 ⎥
⎢ ⎥
⎢0 0 0⎥
⎢ ⎥
⎢0 0⎥
⎢ 0 ⎥
⎢ ⎥
⎢0 0 0⎥
⎣0 0 0⎦
1 of 3 22/11/23, 13:51
Slip1 http://localhost:8888/nbconvert/html/Slip1.ipynb?dow...
Out[5]: ⎡1 1 1 1⎤
⎢1 1 1 1⎥
⎢ ⎥
⎢
⎢1 1 1 1⎥
⎥
⎢ ⎥
⎢1 1 1 1⎥
⎣1 1 1 1⎦
In [ ]: #Q2.3
1
3
7
9
11
13
17
19
21
23
27
29
31
33
37
39
41
43
47
49
51
53
57
59
61
63
67
69
71
73
77
79
81
83
87
89
91
93
97
99
In [ ]: #Q3.a.1
2 of 3 22/11/23, 13:51
Slip1 http://localhost:8888/nbconvert/html/Slip1.ipynb?dow...
Out[7]: 2.0008631896735363
In [ ]: #Q3.b.1
iteration = 0
while iteration < max_iter:
c = (a * func(b) - b * func(a)) / (func(b) - func(a))
iteration += 1
root = regula_falsi(a, b)
In [ ]:
3 of 3 22/11/23, 13:51
Untitled33 http://localhost:8888/nbconvert/html/Untitled33.ipyn...
In [4]: #Slip2
#Q1.1
def calculate_sphere_volume(radius):
volume = (4/3) * math.pi * (radius ** 3)
return volume
r = 7
sphere_volume = calculate_sphere_volume(r)
print("The volume of the sphere with radius", r, "is:", sphere_volume)
In [ ]: #Q1.3
In [ ]: #Q2.1
f(-2) = 20
f(0) = 6
f(2) = 0
In [ ]: #Q2.2
In [ ]: #Q3.a.1
1 of 2 22/11/23, 14:26
Untitled33 http://localhost:8888/nbconvert/html/Untitled33.ipyn...
sum_odd = sum(y[1:-1:2])
sum_even = sum(y[2:-2:2])
In [ ]: #Q3.b.1
2 of 2 22/11/23, 14:26
In [ ]: #Slip 3
#Q1.1
LATEXLATEXLATEXLATEXLATEXLATEXLATEXLATEXLATEXLATEXLATEX
MATLABMATLABMATLABMATLABMATLABMATLABMATLABMATLABMATLABMATLABMATLAB
In [ ]: #Q1.2
Enter a number: 5
Number is divisible by 5
In [ ]: #Q2.1
Out[4]: [(-1,
1,
[Matrix([
[0.5],
[ 1]])]),
(0,
1,
[Matrix([
[0.666666666666667],
[ 1]])])]
In [ ]: #Q2.2
Out[7]: ⎡0 0 0⎤
⎢0 0 0⎥
⎢ ⎥
⎢0 0⎥
⎢ 0 ⎥
⎢ ⎥
⎢0 0 0⎥
⎢ ⎥
⎢0 0⎥
⎢ 0 ⎥
⎢ ⎥
⎢0 0 0⎥
⎣0 0 0⎦
Out[9]: ⎡1 1 1 1⎤
⎢1 1 1 1⎥
⎢
⎢ ⎥
⎢ 1 1 1 1⎥
⎥
⎢
⎢1 ⎥
1 1 1⎥
⎣1 1 1 1⎦
In [ ]: #Q3.a.1
In [14]: import numpy as np
def func(x):
return np.sin(x)
h = (b - a) / n
x = np.linspace(a, b, n+1)
y = func(x)
integral *= h / 3
return integral
a = 0
b = np.pi
n = 6
estimated_integral = simpsons_one_third(a, b, n)
print(f"The estimated value of the integral using Simpson's 1/3 rule (n={n}) is approxima
The estimated value of the integral using Simpson's 1/3 rule (n=6) is approximately:
2.000863
In [ ]: #Q.3.b.2
def f(x):
return 1/(1 + x)
a = 2
b = 10
n = 5
h = (b - a) / n
s = f(a) + f(b)
integral = (h/2) * s
In [ ]:
In [ ]: #Slip 4
#Q1.1
In [ ]: #Q1.3
1
9
25
49
81
121
169
225
289
361
In [ ]: #Q2.1
[[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]
Out[6]: ⎡0 0 0⎤
⎢0 0 0⎥
⎢ ⎥
⎢0 0⎥
⎢ 0 ⎥
⎢ ⎥
⎢0 0 0⎥
⎢ ⎥
⎢0 0⎥
⎢ 0 ⎥
⎢ ⎥
⎢0 0 0⎥
⎣0 0 0⎦
In [ ]: #Q2.2
In [13]: type('number')
Out[13]: str
In [14]: type(31.25)
Out[14]: float
In [15]: type('Mathematics')
Out[15]: str
In [16]: type(49)
Out[16]: int
In [ ]: #Q3.a.1
def func(x):
return x * np.sin(x)
h = (b - a) / n
x = np.linspace(a, b, n+1)
y = func(x)
integral *= h / 3
return integral
estimated_integral = simpsons_one_third(a, b, n)
print(f"The estimated value of the integral using Simpson's 1/3 rule (n={n}) is approxima
The estimated value of the integral using Simpson's 1/3 rule (n=6) is approximately:
3.142949
In [ ]: #Q3.b.1
p = 2
while p * p <= n:
if primes[p]:
for i in range(p * p, n + 1, p):
primes[i] = False
p += 1
result = sieve_of_eratosthenes(given_number)
print(f"Prime numbers less than {given_number} are: {result}")
Prime numbers less than 50 are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 4
7]
In [ ]:
In [ ]: #Slip 5
#Q1.2
[1, 2, 3, 4]
pqr
make in india
In [ ]: #Q1.3
In [ ]: #Q2.1
In [ ]: #Q2.2
x, y, z = symbols('x y z')
eq1 = x - 2 * y + 3 * z - 7
eq2 = 2 * x + y + z - 4
eq3 = -3 * x + 2 * y - 2 * z + 10 # Changed the sign to be consistent
In [ ]: #Q3.a.1
A = np.array([[1, 3, 3],
[2, 2, 3],
[4, 2, 1]])
print("Eigenvalues:")
print(eigenvalues)
print("\nEigenvectors:")
print(eigenvectors)
P = eigenvectors
D = np.diag(eigenvalues)
diagonalized_matrix = np.dot(np.linalg.inv(P), np.dot(A, P))
Eigenvectors:
[[ 5.77350269e-01 4.08248290e-01 -1.11022302e-16]
[ 5.77350269e-01 4.08248290e-01 -7.07106781e-01]
[ 5.77350269e-01 -8.16496581e-01 7.07106781e-01]]
In [ ]: #Q3.b.2
iter_count = 0
while iter_count < max_iter:
c = (a * func(b) - b * func(a)) / (func(b) - func(a))
iter_count += 1
print("The method did not converge within the maximum number of iterations.")
return None
root = regula_falsi(func, a, b)
In [ ]:
slip11 http://localhost:8888/nbconvert/html/slip11.ipynb?do...
In [ ]: Slip No 6
# Q1.2
In [22]: students = [
... {'name': 'John', 'roll_number': 1},
... {'name': 'Steve', 'roll_number': 2},
... {'name': 'Karen', 'roll_number': 3},
... {'name': 'Sophia', 'roll_number': 4},
... {'name': 'Alice', 'roll_number': 5}
... ]
>>> for student in students:
... print(f"Name: {student['name']}, Roll number: {student['roll_number'
In [ ]: #Q1.3
In [ ]: #Q2.1
In [ ]:
1 of 2 22/11/23, 13:20
slip11 http://localhost:8888/nbconvert/html/slip11.ipynb?do...
In [ ]: #Q2.2
In [17]: list = []
>>> for x in range(1,201):
... if x % 7 == 0:
... list.append(x)
...
>>> print(list)
[7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105, 112, 119, 12
6, 133, 140, 147, 154, 161, 168, 175, 182, 189, 196]
In [ ]: #Q3.a.2
In [ ]:
2 of 2 22/11/23, 13:20
Slip 7 http://localhost:8888/nbconvert/html/Slip%207.ipynb...
In [ ]: #Slip 7
#Q1.1
result_a = z1 + z2
print("a. z1 + z2 =", result_a)
result_b = z1 - z2
print("b. z1 - z2 =", result_b)
result_c = z1 * z2
print("c. z1 * z2 =", result_c)
a. z1 + z2 = 10j
b. z1 - z2 = (10-4j)
c. z1 * z2 = (-46+20j)
In [ ]: #Q1.2
print(result_a)
print(result_b)
In [ ]: #Q2.1
1 of 4 22/11/23, 15:39
Slip 7 http://localhost:8888/nbconvert/html/Slip%207.ipynb...
Determinant: 0.0
Trace: 10.0
Transpose:
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
In [ ]: #Q2.2
2 of 4 22/11/23, 15:39
Slip 7 http://localhost:8888/nbconvert/html/Slip%207.ipynb...
In [ ]: def f(x):
return x**(-4*x-9)
In [ ]: #Q3.a.1
h = (b - a) / n
x = a
integral = func(a) + func(b)
integral *= h / 3
return integral
integral = simpsons_one_third(func, a, b, n)
print("Approximated integral using Simpson's 1/3 rule:", integral)
In [ ]: #Q3.b.2
3 of 4 22/11/23, 15:39
Slip 7 http://localhost:8888/nbconvert/html/Slip%207.ipynb...
h = x_vals[1] - x_vals[0]
backward_diff = [y_vals]
res = y_vals[-1]
fact = 1
s = (x - x_vals[-1]) / h
return res
X = [0, 1, 2, 3]
Y = [0, 1, 8, 27]
x_val = 2.8
In [ ]:
4 of 4 22/11/23, 15:39
Untitled27 http://localhost:8888/nbconvert/html/Untitled27.ipyn...
In [ ]: #Slip 8
#Q1.1
In [1]: a = 5
b = 7
c = 9
d = 11
result_1 = a + c
result_2 = a *
result_3 = c ** d
result_4 = a / b
result_5 = a * (b + c)
print(f"a + c = {result_1}")
print(f"a * b = {result_2}")
print(f"c ^ d = {result_3}")
print(f"a / b = {result_4}")
print(f"a * (b + c) = {result_5}")
a + c = 14
a * b = 35
c ^ d = 31381059609
a / b = 0.7142857142857143
a * (b + c) = 80
In [ ]: #Q1.3
circumference = 2 * 3.14 * 14
In [ ]: #Q2.1
1 of 3 22/11/23, 15:50
Untitled27 http://localhost:8888/nbconvert/html/Untitled27.ipyn...
A = np.array([[1, 2],
[3, 4]])
B = np.array([[5, 6],
[7, 8]])
C = np.array([[9, 10],
[11, 12]])
if np.array_equal(result1, result2):
print("(A + B) + C is equal to A + (B + C)")
print("Matrices are associative with respect to addition.")
else:
print("(A + B) + C is NOT equal to A + (B + C)")
print("Matrices are NOT associative with respect to addition.")
(A + B) + C is equal to A + (B + C)
Matrices are associative with respect to addition.
In [ ]: #Q2.2
In [5]: a, b = 0, 1
print(a)
print(b)
for _ in range(8):
c = a + b
print(c)
a, b = b, c
0
1
1
2
3
5
8
13
21
34
In [ ]: #Q3.a.1
2 of 3 22/11/23, 15:50
Untitled27 http://localhost:8888/nbconvert/html/Untitled27.ipyn...
integral *= h / 3
return integral
a = 0
b = 1
n = 6
result = simpsons_one_third_rule(func, a, b, n)
print(f"The estimated value of the integral using Simpson's 1/3 rule with n=6:
The estimated value of the integral using Simpson's 1/3 rule with n=6: 0.7
853979452340107
In [ ]: #Q3.b.2
integral *= h / 2
return integral
a = 2
b = 4
n = 5
result = trapezoidal_rule(func, a, b, n)
print(f"The estimated value of the integral using the trapezoidal rule with n=5:
The estimated value of the integral using the trapezoidal rule with n=5: 1
5.440000000000001
In [ ]:
3 of 3 22/11/23, 15:50
In [ ]: #Slip9
#Q1.2.a
In [ ]: #Q1.2.b
In [ ]: #Q1.2.c
In [ ]: #Q1.2.d
In [ ]: #Q1.3
In [ ]: #Q2.1
second_character = text[1]
fifth_character = text[4]
eighth_character = text[7]
print("Second character:", second_character)
print("Fifth character:", fifth_character)
print("Eighth character:", eighth_character)
Second character: A
Fifth character: E
Eighth character: T
In [ ]: #Q2.3
In [9]: import numpy as np
A = np.array([[2, 3],
[4, 5]])
B = np.array([[1, 0],
[0, 1]])
AB = np.dot(A, B)
AB_inverse = np.linalg.inv(AB)
B_inverse = np.linalg.inv(B)
A_inverse = np.linalg.inv(A)
left_side = AB_inverse
right_side = np.dot(B_inverse, A_inverse)
if np.array_equal(left_side, right_side):
print("(AB)^(-1) = B^(-1) * A^(-1) is verified.")
else:
print("(AB)^(-1) = B^(-1) * A^(-1) is not verified.")
In [ ]: #Q3.a.2
In [ ]: #Q3.b.1
In [ ]:
In [1]: #Slip no 10
#Q.1(1)
result_b = 60 * 20 // 3 + 15 % 3
print("Result of expression b:", result_b)
result_c = 27 - 23 + 8 // 4
print("Result of expression c:", result_c)
In [4]: #Q.1(3)
In [5]: base = 10
height = 15
In [6]: #Q.2(1)
identity_matrix = np.identity(10)
print("Identity matrix of order 10x10:")
print(identity_matrix)
In [8]: #Q.2(2)
start_x = -5
end_x = 5
print("Values of f(x) = x^2 + x for x in the range (-5 <= x <= 5):")
print("x | f(x)")
print("--------------")
for x in range(start_x, end_x + 1):
result = f(x)
print(f"{x} | {result}")
Values of f(x) = x^2 + x for x in the range (-5 <= x <= 5):
x | f(x)
--------------
-5 | 20
-4 | 12
-3 | 6
-2 | 2
-1 | 0
0 | 0
1 | 2
2 | 6
3 | 12
4 | 20
5 | 30
In [10]: #Q.3(A2)
In [11]: from scipy.interpolate import interp1d
x_interpolated = 2.7
y_interpolated = interp_func(x_interpolated)
In [12]: #Q.3(B1)
for i in range(max_iter):
c = (a * func(b) - b * func(a)) / (func(b) - func(a))
a = 1
b = 3
root = regula_falsi(a, b)
In [ ]:
In [ ]: #Slip11
#Q1.1.a
In [1]: M = [1, 2, 3, 4, 5, 6, 7]
length_of_M = len(M)
print(f"The length of the list M is: {length_of_M}")
In [ ]: #Q1.1.b
In [ ]: #Q1.1.c
In [ ]: #Q1.3
In [ ]: #Q2.1
In [8]: teachers = {
'Teacher 1': 'Mathematics',
'Teacher 2': 'Physics',
'Teacher 3': 'Biology',
'Teacher 4': 'History',
'Teacher 5': 'Computer Science'
}
print("List of Teachers and Subjects:")
for teacher, subject in teachers.items():
print(f"{teacher} teaches {subject}")
In [ ]: #Q2.3
In [10]: 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
start = 51
end = 100
print(f"Prime numbers between {start} and {end}:")
for number in range(start, end + 1):
if is_prime(number):
print(number)
In [ ]: #Q3.a.2
In [ ]: #3.b.1
In [14]: def f(x):
return x**3 - 4*x - 9
def regula_falsi_method(func, a, b, tolerance, max_iterations):
if func(a) * func(b) >= 0:
return "Root is not within the given interval."
iteration = 0
while iteration < max_iterations:
c = (a * func(b) - b * func(a)) / (func(b) - func(a))
if abs(func(c)) < tolerance:
return round(c, 6)
if func(c) * func(a) < 0:
b = c
else:
a = c
iteration += 1
return "Method did not converge within the given iterations."
a = 2
b = 3
tolerance = 0.000001
max_iterations = 1000
root = regula_falsi_method(f, a, b, tolerance, max_iterations)
print(f"Approximate real root of the equation x^3 - 4x - 9 = 0: {root}")
In [ ]:
In [ ]: #Slip12
#Q1.1.a
In [1]: result = 23 % 2 + 9 - (3 + 7) * 10 / 2
print("Result:", result)
Result: -40.0
In [ ]: #Q1.1.b
In [2]: result = 35 * 10 // 3 + 15 % 3
print("Result:", result)
Result: 116
In [ ]: #Q1.1.c
In [3]: result = 35 - 25 + 4 // 7
print("Result:", result)
Result: 10
In [ ]: #Q1.2
In [4]: number = 25
print("Odd positive integers between 25 and 50:")
while number <= 50:
if number % 2 != 0:
print(number)
number += 1
In [ ]: #Q2.2
In [ ]: #Q3.a.2
In [ ]: #Q3.b.1
a = 2
b = 3
tolerance = 0.000001
max_iterations = 1000
root = regula_falsi_method(f, a, b, tolerance, max_iterations)
print(f"Approximate real root of the equation x^3 - 2x - 5 = 0 within [2, 3]: {root}")
Approximate real root of the equation x^3 - 2x - 5 = 0 within [2, 3]: 2.094551
In [ ]:
In [1]: #Slip No 13
#Q.1(1)
In [2]: z1 = 3 + 2j
z2 = -4 + 1j
addition_result = z1 + z2
print("Addition Result:", addition_result)
subtraction_result = z1 - z2
print("Subtraction Result:", subtraction_result)
multiplication_result = z1 * z2
print("Multiplication Result:", multiplication_result)
In [3]: #Q.1(2)
In [5]: #Q.2(1)
In [6]: S = [1, 2, 3, 4, 5, 6, 7, 8, 9]
reversed_S = S[::-1]
print("Reversed List:", reversed_S)
In [7]: #Q.2(3)
In [8]: start_number = 50
end_number = 100
In [9]: #Q.3(A1)
In [10]: import numpy as np
def f(x):
return np.sqrt(1 + x**3)
return result * h / 3
a = 0
b = 5
n = 10
integral_estimate = simpsons_one_third_rule(f, a, b, n)
print("Estimated value of the integral:", integral_estimate)
In [11]: #Q.3(B1)
for i in range(max_iter):
c = (a * func(b) - b * func(a)) / (func(b) - func(a))
a = 1
b = 3
root = regula_falsi(a, b)
In [ ]:
In [1]: #slip no 16
#Q.1(1)
In [14]: int_num=-25
float_num=-10.50
print("The absolute value of an integer number is:",abs(int_num))
print("The absolute value of an float number is:",abs(float_num))
In [15]: #Q.1(2)
In [20]: list1=[5,10,15,20,25,30]
list2=[7,14,21,28,35,42]
list1+list2
Out[20]: [5, 10, 15, 20, 25, 30, 7, 14, 21, 28, 35, 42]
In [21]: 7*list1
Out[21]: [5,
10,
15,
20,
25,
30,
5,
10,
15,
20,
25,
30,
5,
10,
15,
20,
25,
30,
5,
10,
15,
20,
25,
30,
5,
10,
15,
20,
25,
30,
5,
10,
15,
20,
25,
30,
5,
10,
15,
20,
25,
30]
In [22]: 11*list2
Out[22]: [7,
14,
21,
28,
35,
42,
7,
14,
21,
28,
35,
42,
7,
14,
21,
28,
35,
42,
7,
14,
21,
28,
35,
42,
7,
14,
21,
28,
35,
42,
7,
14,
21,
28,
35,
42,
7,
14,
21,
28,
35,
42,
7,
14,
21,
28,
35,
42,
7,
14,
21,
28,
35,
42,
7,
14,
21,
28,
35,
42,
7,
14,
21,
28,
35,
42]
In [23]: #Q.2(1)
In [30]: marks=[70,80,55,78,65]
percentage=(sum(marks)/500)*100
print("Percentage of marks:",percentage)
In [31]: #Q.2(3)
det_A = np.linalg.det(matrix_A)
print("Determinant of matrix A:")
print(det_A)
try:
inv_A = np.linalg.inv(matrix_A)
print("\nInverse of matrix A:")
print(inv_A)
except np.linalg.LinAlgError:
print("\nMatrix A is not invertible (singular matrix).")
det_B = np.linalg.det(matrix_B)
print("\nDeterminant of matrix B:")
print(det_B)
try:
inv_B = np.linalg.inv(matrix_B)
print("\nInverse of matrix B:")
print(inv_B)
except np.linalg.LinAlgError:
print("\nMatrix B is not invertible (singular matrix).")
Determinant of matrix A:
0.9999999999999967
Inverse of matrix A:
[[-24. 20. -5.]
[ 18. -15. 4.]
[ 5. -4. 1.]]
Determinant of matrix B:
13.0
Inverse of matrix B:
[[ 0.30769231 -0.38461538]
[ 0.07692308 0.15384615]]
x = sp.symbols('x')
f = x**5 + 5*x + 1
f_prime = sp.diff(f, x)
initial_guess = -0.5
tolerance = 1e-8
max_iterations = 1000
a = 1
b = 2
n = 8
integral_value = trapezoidal_rule(f, a, b, n)
print(f"The estimated value of the integral using the Trapezoidal Rule with n={n}: {integ
The estimated value of the integral using the Trapezoidal Rule with n=8: 4.05645851191
1803
In [ ]:
In [ ]: #Slip 18
#Q1.1
In [1]: list1=[16,3,5,48,2,4,5,6,78,12,5,6,24]
>>> print ("smallest element is:",list1[0])
In [ ]: #Q1.2
In [5]: side1 = 12
side2 = 5
In [ ]: #Q1.3
In [ ]: #Q2.2
In [7]: n = 1
sum_of_numbers = 0
count = 0
In [ ]: #Q2.3
A = np.array([[3, -2],
[6, -4]])
P = eigenvectors
D = np.diag(eigenvalues)
print("Matrix P (Eigenvectors):")
print(P)
In [ ]: #Q3.a.2
In [ ]: #Q3.b.1
iteration = 0
while iteration < max_iter:
c = (a * func(b) - b * func(a)) / (func(b) - func(a))
iteration += 1
a = 2
b = 3
root = regula_falsi(a, b)
In [ ]:
Untitled26 http://localhost:8888/nbconvert/html/Untitled26.ipyn...
In [ ]: #slip 19
#Q1.1
1 of 6 22/11/23, 14:06
Untitled26 http://localhost:8888/nbconvert/html/Untitled26.ipyn...
Multiplication Table of 2
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
Multiplication Table of 3
3 X 1 = 3
3 X 2 = 6
3 X 3 = 9
3 X 4 = 12
3 X 5 = 15
3 X 6 = 18
3 X 7 = 21
3 X 8 = 24
3 X 9 = 27
3 X 10 = 30
Multiplication Table of 4
4 X 1 = 4
4 X 2 = 8
4 X 3 = 12
4 X 4 = 16
4 X 5 = 20
4 X 6 = 24
4 X 7 = 28
4 X 8 = 32
4 X 9 = 36
4 X 10 = 40
Multiplication Table of 5
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
Multiplication Table of 6
6 X 1 = 6
6 X 2 = 12
6 X 3 = 18
6 X 4 = 24
6 X 5 = 30
6 X 6 = 36
6 X 7 = 42
2 of 6 22/11/23, 14:06
Untitled26 http://localhost:8888/nbconvert/html/Untitled26.ipyn...
6 X 8 = 48
6 X 9 = 54
6 X 10 = 60
Multiplication Table of 7
7 X 1 = 7
7 X 2 = 14
7 X 3 = 21
7 X 4 = 28
7 X 5 = 35
7 X 6 = 42
7 X 7 = 49
7 X 8 = 56
7 X 9 = 63
7 X 10 = 70
Multiplication Table of 8
8 X 1 = 8
8 X 2 = 16
8 X 3 = 24
8 X 4 = 32
8 X 5 = 40
8 X 6 = 48
8 X 7 = 56
8 X 8 = 64
8 X 9 = 72
8 X 10 = 80
Multiplication Table of 9
9 X 1 = 9
9 X 2 = 18
9 X 3 = 27
9 X 4 = 36
9 X 5 = 45
9 X 6 = 54
9 X 7 = 63
9 X 8 = 72
9 X 9 = 81
9 X 10 = 90
Multiplication Table of 10
10 X 1 = 10
10 X 2 = 20
10 X 3 = 30
10 X 4 = 40
10 X 5 = 50
10 X 6 = 60
10 X 7 = 70
10 X 8 = 80
10 X 9 = 90
10 X 10 = 100
In [ ]: #Q1.3
3 of 6 22/11/23, 14:06
Untitled26 http://localhost:8888/nbconvert/html/Untitled26.ipyn...
In [ ]: #Q2.2
rank = np.linalg.matrix_rank(A)
print(f"\nThe rank of matrix A is: {rank}")
Q2.3
4 of 6 22/11/23, 14:06
Untitled26 http://localhost:8888/nbconvert/html/Untitled26.ipyn...
A = np.array([
[2, -1, 2, 7],
[4, 7, 3, 4],
[4, 2, 0, -1]
])
P, L, U = lu(A)
print("Matrix L:")
print(L)
print("\nMatrix U:")
print(U)
Matrix L:
[[1. 0. 0. ]
[1. 1. 0. ]
[0.5 0.9 1. ]]
Matrix U:
[[ 4. 7. 3. 4. ]
[ 0. -5. -3. -5. ]
[ 0. 0. 3.2 9.5]]
In [ ]: #Q3.a.1
return result
angle_to_find = 420
sin_42 = backward_interpolation(angle_to_find, angles, sine_values)
print(f"The estimated value of sin(42) is approximately: {sin_42:.4f}")
In [ ]: #Q3.b.2
5 of 6 22/11/23, 14:06
Untitled26 http://localhost:8888/nbconvert/html/Untitled26.ipyn...
return result
angle_to_find = 42
sin_42 = backward_interpolation(angle_to_find, angles, sine_values)
print(f"The estimated value of sin(42) is approximately: {sin_42:.4f}")
In [ ]:
6 of 6 22/11/23, 14:06
Slip 20 http://localhost:8888/nbconvert/html/Slip%2020.ipyn...
In [ ]: #Slip No 20
#Q1.1
In [ ]: #Q1.2
In [3]: sum_of_squares = 0
for i in range(1,26):
sum_of_squares += i**2
print(sum_of_squares)
5525
In [ ]: #Q1.3
divisors = []
for i in range(1,n+1):
if n % i == 0:
divisors.append(i)
Enter a number: 5
The divisors of 5 are [1, 5]
In [ ]: #Q2.1
print(tuple[1])
am
In [ ]: #Q2.2
1 of 3 22/11/23, 13:42
Slip 20 http://localhost:8888/nbconvert/html/Slip%2020.ipyn...
[[10 10 10 10 10 10]
[10 10 10 10 10 10]
[10 10 10 10 10 10]
[10 10 10 10 10 10]]
diagonalize_matrix = np.diag(eigenvalues)
Matrix:
[[ 3 -2]
[ 6 -4]]
Eigenvalues:
[ 1.77635684e-15 -1.00000000e+00]
Eigenvectors:
[[0.5547002 0.4472136 ]
[0.83205029 0.89442719]]
Diagonalize matrix:
[[ 1.77635684e-15 0.00000000e+00]
[ 0.00000000e+00 -1.00000000e+00]]
In [ ]: #Q3.1
In [ ]: #Q3.2
2 of 3 22/11/23, 13:42
Slip 20 http://localhost:8888/nbconvert/html/Slip%2020.ipyn...
x = np.array([1,2,3,6])
y = np.array([2,6,12,42])
xn = 5
coeffs = np.polyfit(x, y, 3)
f = np.poly1d(coeffs)
f_xn = f(xn)
print("Given data:")
print("x:", x)
print("y:", y)
print("xn:", xn)
print("Interpolated value f(xn):", f_xn)
Given data:
x: [1 2 3 6]
y: [ 2 6 12 42]
xn: 5
Interpolated value f(xn): 29.999999999999993
In [ ]: #Q3.b.1
a = 1
b = 5
n = 5
estimated_integral = trapezoidal_rule(a, b, n)
print(f"The estimated value of the integral using the Trapezoidal Rule (n=
The estimated value of the integral using the Trapezoidal Rule (n=5) is ap
proximately: 131.8400
In [ ]:
3 of 3 22/11/23, 13:42
In [ ]: #Slip24
#Q1.2
In [ ]: #Q1.3
21
42
In [ ]: #Q2.1
1 is a perfect square (1 * 1)
4 is a perfect square (2 * 2)
9 is a perfect square (3 * 3)
16 is a perfect square (4 * 4)
25 is a perfect square (5 * 5)
36 is a perfect square (6 * 6)
49 is a perfect square (7 * 7)
64 is a perfect square (8 * 8)
81 is a perfect square (9 * 9)
100 is a perfect square (10 * 10)
In [ ]: #Q2.2
In [ ]: #Q3.a.2
In [6]: x = [1, 2, 3, 4]
y = [11, 10, 15, 10]
x_value = 1.9
n = len(x)
backward_diff_table = [[0 for _ in range(n)] for _ in range(n)]
for i in range(n):
backward_diff_table[i][0] = y[i]
for i in range(1, n):
for j in range(n - i):
backward_diff_table[j][i] = backward_diff_table[j + 1][i - 1] - backward_diff_tab
def factorial(num):
if num == 0 or num == 1:
return 1
else:
return num * factorial(num - 1)
def backward_difference_interpolation(x, y, x_val):
result = 0
n = len(x)
for i in range(n):
term = backward_diff_table[0][i]
for j in range(i):
term *= (x_val - x[j])
term /= factorial(i)
result += term
return result
result = backward_difference_interpolation(x, y, x_value)
print(f"f({x_value}) ≈ {result}")
f(1.9) ≈ 9.566
In [ ]: #Q3.b.1
In [ ]: