#newton rapsin #EULERS METHOD #bisection
def newton_raphson(x, e, n, d): def f(x, y): def bisection(x1, x2, e):
f = lambda x: x**2 -12 return x + y f = lambda x: x**3 - 4*x-9
f1 = lambda x: 2*x def euler_method(x0, y0, h, xn): f1 = f(x1)
for i in range(1, n+1): n = int((xn - x0) / h) + 1 f2 = f(x2)
fx = f(x) if f1 * f2 > 0:
f1x = f1(x) for i in range(1, n + 1): print("Initial guesses are wrong")
if abs(f1x) < d: y = y0 + h * f(x0, y0) return
print("too small slope") x = x0 + h while True:
return print(f"Step {i}: x = {x0}, y = x = (x1 + x2) / 2
x1 = x - fx/f1x {y0}") if abs((x1 - x2) / x) < e:
if abs((x1 - x)/x1) < e: if x < xn: print(f"root is {x:.6f}")
print(f"root is {x1:.6f}") x0 = x return
return y0 = y fx = f(x)
x = x1 else: if fx * f1 > 0:
print("method does not converge due break x1 = x
to oscillation") x0 = 0 f1 = fx
x0 = 2 y0 = 1 else:
e = 1e-6 h = 0.2 x2 = x
n = 100 xn = 0.6 f2 = fx
d = 1e-12 euler_method(x0, y0, h, xn) x1 = 1
newton_raphson(x0, e, n, d) x2 = 2
e = 1e-6
bisection(x1, x2, e)
#False Position # Fibonacci Series #bubble sort
def f(x): i=1 def bubble_sort(arr):
return x**3 - 4 a=0 N = len(arr)
def false_position_method(x0, x1, e): b=1 for i in range(N - 1):
step = 1 show = 0 for j in range(N - i - 1):
condition = True n = 30 if arr[j] > arr[j + 1]:
while condition: print(f"Fibonacci series up to {n} arr[j], arr[j + 1] = arr[j + 1],
x2 = (x0 * f(x1) - x1 * f(x0)) / terms:") arr[j]
(f(x1) - f(x0)) print(a) return arr
print(f'Step-{step}: x2 = print(b) if __name__ == "__main__":
{x2:.6f}') while i <= n - 2: input_string = input("Enter numbers
if f(x0) * f(x2) < 0: show = a + b separated by spaces: ")
x1 = x2 a=b numbers = list(map(int,
else: b = show input_string.split()))
x0 = x2 i += 1 print("Original array:", numbers)
step = step + 1 print(show) sorted_numbers =
condition = abs(f(x2)) > e bubble_sort(numbers)
print(f'\nRequired root is: {x2:.6f}') print("Sorted array:",
x0 = 1.0 sorted_numbers)
x1 = 2.0
e = 0.00001
false_position_method(x0, x1, e)
#palindrome #matrix
def is_palindrome(num): A = [[2, 2],
reverse = 0 [2, 2]]
temp_num = num B = [[2, 2],
while num != 0: [2, 2,]]
rem = num % 10 result = [[0, 0],
reverse = reverse * 10 + rem [0, 0]]
num = num // 10 for i in range(len(A)):
if reverse == temp_num: for j in range(len(B[0])):
return True for k in range(len(B)):
else: result[i][j] += A[i][k] * B[k][j]
return False for r in result:
if __name__ == "__main__": print(r)
num = int(input("Enter a number
to check if it's a palindrome: "))
if is_palindrome(num):
print(f"{num} is a palindrome.")
else:
print(f"{num} is NOT a
palindrome.")
def input_matrix(rows, cols): #newton rapsin
matrix = [] #False Position def newton_raphson(x, e, n, d):
print(f"Enter the elements of the {rows}x{cols} matrix row-
def f(x): f = lambda x: x**2 -12
wise:")
for i in range(rows): return x**3 - 4 f1 = lambda x: 2*x
row = list(map(int, input().split())) def false_position_method(x0, x1, e): for i in range(1, n+1):
if len(row) != cols: step = 1
raise ValueError(f"Expected {cols} elements, but got
fx = f(x)
{len(row)}")
condition = True f1x = f1(x)
[Link](row) while condition: if abs(f1x) < d:
return matrix x2 = (x0 * f(x1) - x1 * f(x0)) / print("too small slope")
def matrix_multiply(a, b): (f(x1) - f(x0))
m, n = len(a), len(a[0])
return
p, q = len(b), len(b[0])
print(f'Step-{step}: x2 = x1 = x - fx/f1x
if n != p: {x2:.6f}') if abs((x1 - x)/x1) < e:
print("Matrix multiplication is not possible.") if f(x0) * f(x2) < 0: print(f"root is {x1:.6f}")
return None x1 = x2
c = [[0 for _ in range(q)] for _ in range(m)]
return
for i in range(m):
else: x = x1
for j in range(q): x0 = x2 print("method does not converge due to
for k in range(p): step = step + 1 oscillation")
c[i][j] += a[i][k] * b[k][j] condition = abs(f(x2)) > e
return c x0 = 2
def print_matrix(matrix): print(f'\nRequired root is: {x2:.6f}') e = 1e-6
for row in matrix: x0 = 1.0 n = 100
print(" ".join(map(str, row))) x1 = 2.0 d = 1e-12
m, n = map(int, input("Enter the order of the first matrix (m n): e = 0.00001
").split()) newton_raphson(x0, e, n, d)
p, q = map(int, input("Enter the order of the second matrix (p q): false_position_method(x0, x1, e)
").split())
print("\nFirst Matrix:")
matrix1 = input_matrix(m, n)
print("\nSecond Matrix:")
matrix2 = input_matrix(p, q)
result = matrix_multiply(matrix1, matrix2)
if result:
print("\nResultant Matrix:")
print_matrix(result)