MATH lab final code
MATH lab final code
n = len(x_values)
result = 0
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])
result += term
return result
# Given interval
a = -2
b = -1.5
# Find root
root = bisection_method(a, b)
if root is not None:
print(f"Approximate root in the interval [{a}, {b}]: {root:.6f}")
for _ in range(max_iter):
# Compute the root using false position formula
c = (a * f(b) - b * f(a)) / (f(b) - f(a))
return table
return result
# Example usage
x = [4, 5, 7, 10, 11, 13]
y = [48, 100, 294, 900, 1210, 2028]
value = 15
1. def find_relations(A):
"""Finds relations R1 and R2 on set A."""
R1 = [] # Relation where a divides b
R2 = [] # Relation where a <= b
for a in A:
for b in A:
if b % a == 0: # Check if a divides b
R1.append((a, b))
if a <= b:
R2.append((a, b))
return R1, R2
A = {1, 2, 3, 4}
R1, R2 = find_relations(A)
for a in A:
for b in B:
if a > b:
R.append((a, b))
# Matrix Representation
matrix = [[0 for _ in B] for _ in A] # Initialize matrix with 0s
for a, b in R:
matrix[A.index(a)][B.index(b)] = 1 # Set matrix element to 1
if (a, b) is in R
return R, matrix
A = [1, 2, 3]
B = [1, 2]
R, matrix = find_relation_and_matrix(A, B)
for i in range(rows):
for j in range(cols):
union_matrix[i][j] = MR1[i][j] or MR2[i][j]
return union_matrix
for i in range(rows):
for j in range(cols):
intersection_matrix[i][j] = MR1[i][j] and MR2[i][j]
return intersection_matrix
print("MR1 ∪ MR2:")
for row in union_result:
print(row)
print("\nMR1 ∩ MR2:")
for row in composition_result:
print(row)
3. class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = [[0] * vertices for _ in range(vertices)]
def welsh_powell_coloring(self):
# Sort vertices by degree (number of connections)
degree_list = sorted(range(self.V), key=lambda v:
sum(self.graph[v]), reverse=True)
colors = [-1] * self.V # -1 means uncolored
color = 0
color += 1
return colors
# Example usage
g = Graph(5)
g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(1, 3)
g.add_edge(1, 4)
g.add_edge(2, 3)
g.add_edge(3, 4)
coloring = g.welsh_powell_coloring()
print("Vertex Colors:", coloring)
4. INF = float('inf')
def floyd_warshall(graph):
V = len(graph)
dist = [row[:] for row in graph] # Copy of the graph
for k in range(V):
for i in range(V):
for j in range(V):
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
return dist
shortest_paths = floyd_warshall(graph)
for row in shortest_paths:
print(row)
6. import numpy as np
def forward_difference(y_values):
n = len(y_values)
table = np.zeros((n, n))
table[:, 0] = y_values
return table
table = forward_difference(y_values)
result, u_term, fact = y_values[0], 1, 1
for i in range(1, len(x_values)):
u_term *= (u - (i - 1))
fact *= i
result += (u_term / fact) * table[0][i]
return result
# Given data
years = np.array([1911, 1921, 1931, 1941, 1951, 1961])
population = np.array([12, 15, 20, 27, 39, 52])
year_to_predict = 1946
print(f"Estimated population in {year_to_predict}:
{newton_gregory(years, population, year_to_predict):.2f}")
7. import numpy as np
def backward_difference(y_values):
n = len(y_values)
table = np.zeros((n, n))
table[:, 0] = y_values
return table
table = backward_difference(y_values)
result, u_term, fact = y_values[-1], 1, 1
return result
# Given data
x_vals = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y_vals = np.array([1, 8, 27, 64, 125, 216, 343, 512])
x_to_predict = 7.5
print(f"Estimated value at f({x_to_predict}):
{newton_gregory_backward(x_vals, y_vals, x_to_predict):.2f}")