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

Lab Task 10

This document contains code for polynomial interpolation using divided differences. It defines functions for calculating divided difference coefficients from a set of x-y data points and for evaluating the interpolating polynomial at given x values. The code takes user input for the data points, calculates the coefficients, and evaluates the polynomial at a user-specified x value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lab Task 10

This document contains code for polynomial interpolation using divided differences. It defines functions for calculating divided difference coefficients from a set of x-y data points and for evaluating the interpolating polynomial at given x values. The code takes user input for the data points, calculates the coefficients, and evaluates the polynomial at a user-specified x value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

HAMZA AHMED WAJEEH (12349) NC LAB (111678) TASK 10

LAB 11 (MANUAL)

Q1 CODE:
def divided_diff(x, y):

n = len(x)

coeff = []

for i in range(n):

coeff.append(y[i])

for j in range(1, n):

for i in range(n-1, j-1, -1):

coeff[i] = (coeff[i] - coeff[i-1]) / (x[i] - x[i-j])

return coeff

def eval_poly(x, coeff, x_vals):

n = len(x_vals)
HAMZA AHMED WAJEEH (12349) NC LAB (111678) TASK 10
LAB 11 (MANUAL)
y_vals = []

for i in range(n):

y = coeff[-1]

for j in range(len(coeff)-2, -1, -1):

y = y * (x_vals[i] - x[j]) + coeff[j]

y_vals.append(y)

return y_vals

n = int(input("Enter the number of data points: "))

x = []

y = []

for i in range(n):

x_val = float(input(f"Enter x[{i}]: "))

x.append(x_val)

y_val = float(input(f"Enter y[{i}]: "))

y.append(y_val)

coeff = divided_diff(x, y)

# Evaluate interpolating polynomial at user-specified point

x_interp = float(input("Enter the x-coordinate at which to evaluate the polynomial: "))

y_interp = eval_poly(x, coeff, [x_interp])[0]

print("Interpolating polynomial coefficients:")

for i in range(len(coeff)):

print(f"c[{i}] = {coeff[i]}")

print(f"\nInterpolated value at x = {x_interp}: y = {y_interp}")


HAMZA AHMED WAJEEH (12349) NC LAB (111678) TASK 10
LAB 11 (MANUAL)
Q1 OUTPUT:

Q2 CODE:
def divided_diff(x, y):

n = len(x)

coeff = []

for i in range(n):

coeff.append(y[i])

for j in range(1, n):

for i in range(n-1, j-1, -1):

coeff[i] = (coeff[i] - coeff[i-1]) / (x[i] - x[i-j])

return coeff

def eval_poly(x, coeff, x_vals):

n = len(x_vals)

y_vals = []3

for i in range(n):

y = coeff[-1]

for j in range(len(coeff)-2, -1, -1):

y = y * (x_vals[i] - x[j]) + coeff[j]

y_vals.append(y)

return y_vals

n = int(input("Enter the number of data points: "))

x = []
HAMZA AHMED WAJEEH (12349) NC LAB (111678) TASK 10
LAB 11 (MANUAL)
y = []

for i in range(n):

x_val = float(input(f"Enter x[{i}]: "))

x.append(x_val)

y_val = input(f"Enter y[{i}]: ")

if y_val == '?':

default_y_val = 0

print(f"Default value of {default_y_val} will be used for y[{i}].")

y.append(default_y_val)

else:

y.append(float(y_val))

coeff = divided_diff(x, y)

x_interp = float(input("Enter the x-coordinate at which to evaluate the polynomial: "))

y_interp = eval_poly(x, coeff, [x_interp])[0]

print("Interpolating polynomial coefficients:")

for i in range(len(coeff)):

print(f"c[{i}] = {coeff[i]}")

print(f"\nInterpolated value at x = {x_interp}: y = {y_interp}")

Q2 OUTPUT:

You might also like