Assignment No.
3
Question No.1:
Solution
# Known angle of attack values (in degrees)
x_values = [0, 5, 10, 15]
# Corresponding lift coefficient values
y_values = [0.0, 0.6, 1.1, 1.3]
# The angle at which we want to interpolate
x_target = 7
def lagrange_interpolation(x_vals, y_vals, x):
total = 0
n = len(x_vals)
for i in range(n):
term = y_vals[i]
for j in range(n):
if i != j:
term *= (x - x_vals[j]) / (x_vals[i] - x_vals[j])
total += term
return total
cl_at_7 = lagrange_interpolation(x_values, y_values, x_target)
print(f"Estimated lift coefficient at {x_target}° is {cl_at_7:.4f}")
Output
Estimated lift coefficient at 7° is 0.8232
Question No.2:
Solution
x = [5, 9, 12, 20] # Time in minutes
y = [45, 50, 52, 60] # Temperature in °C
x_target = 15
def divided_differences(x, y):
n = len(x)
table = [[0 for _ in range(n)] for _ in range(n)]
for i in range(n):
table[i][0] = y[i]
for j in range(1, n):
for i in range(n - j):
table[i][j] = (table[i+1][j-1] - table[i][j-1]) / (x[i+j] -
x[i])
return table
def newton_interpolation(x, table, x_vals, x_point):
n = len(x_vals)
result = table[0][0]
product = 1
for i in range(1, n):
product *= (x_point - x_vals[i-1])
result += table[0][i] * product
return result
# Compute divided difference table
dd_table = divided_differences(x, y)
# Estimate temperature at 15 minutes
temp_at_15 = newton_interpolation(x, dd_table, x, x_target)
print(f"Estimated temperature at {x_target} minutes: {temp_at_15:.4f}
°C")
# Estimate first derivative (using finite difference from Newton's
polynomial)
# f'(x) ≈ f[x1,x2] + f[x1,x2,x3]*(2x - x1 - x2) + ...
f1 = dd_table[0][1] # first divided difference
f2 = dd_table[0][2] # second divided difference
f3 = dd_table[0][3] # third divided difference
d1 = f1 + f2 * (2 * x_target - x[0] - x[1]) + f3 * (3 * x_target**2 - 2 *
x_target * (x[0] + x[1] + x[2]) + x[0]*x[1] + x[0]*x[2] + x[1]*x[2])
print(f"Estimated first derivative (rate of change) at {x_target}
minutes: {d1:.4f} °C/min")
# Estimate second derivative
# f''(x) ≈ 2f[x1,x2,x3] + 6f[x1,x2,x3,x4]*(x - avg)
avg = (x[0] + x[1] + x[2]) / 3
d2 = 2 * f2 + 6 * f3 * (x_target - avg)
print(f"Estimated second derivative (acceleration) at {x_target} minutes:
{d2:.4f} °C/min²")
Output
Estimated temperature at 15 minutes: 53.8636 °C
Estimated first derivative (rate of change) at 15 minutes: 0.7348 °C/min
Estimated second derivative (acceleration) at 15 minutes: 0.1212 °C/min²