mirror of
https://github.com/RustyRaptor/Human-Biology-Notebooks.git
synced 2026-12-03 15:15:16 -07:00
23 KiB
23 KiB
In [1]:
import numpy as np
from sympy import pi, sqrt, cos, symbols, simplify, factorial, sin, Abs, Rational, latex, prod, Sum
def recursive_divided_difference(x_vals, y_vals):
if len(y_vals) == 1:
return y_vals[0]
else:
new_y_vals = []
for i in range(1, len(y_vals)):
# Calculate each divided difference level
new_y = (y_vals[i] - y_vals[i-1]) / (x_vals[i] - x_vals[i-len(y_vals)+1])
new_y_vals.append(new_y)
return recursive_divided_difference(x_vals[1:], new_y_vals)
def newton_interpolation(x_vals, y_vals, x_sym):
coefficients = []
temp_y_vals = y_vals.copy()
for i in range(len(x_vals)):
coeff = recursive_divided_difference(x_vals[:i+1], temp_y_vals[:i+1])
coefficients.append(coeff)
# Prepare the next level of base y-values for divided differences
for j in range(1, len(temp_y_vals)-i):
temp_y_vals[j-1] = temp_y_vals[j] - temp_y_vals[j-1]
temp_y_vals.pop()
# Building the polynomial from the coefficients
polynomial = 0
for i, coeff in enumerate(coefficients):
term = coeff
for j in range(i):
term *= (x_sym - x_vals[j])
polynomial += term
return polynomial
# Example usage
x = symbols('x')
points_example_degree_4 = {
0: 1,
pi/ 6: sqrt(3)/2,
pi/4: 1/sqrt(2),
pi/3: Rational(1,2),
pi/2: 0
}
points_example_degree_3 = {
0: 1,
pi/ 6: sqrt(3)/2,
pi/4: 1/sqrt(2),
pi/3: Rational(1,2)
}
points_example_degree_2 = {
0: 1,
pi/ 6: sqrt(3)/2,
pi/4: 1/sqrt(2)
}
points_example_degree_1 = {
0: 1,
pi/ 6: sqrt(3)/2
}
DD_1 = recursive_divided_difference(list(points_example_degree_1.keys()), list(points_example_degree_1.values()))
DD_2 = recursive_divided_difference(list(points_example_degree_2.keys()), list(points_example_degree_2.values()))
DD_3 = recursive_divided_difference(list(points_example_degree_3.keys()), list(points_example_degree_3.values()))
DD_4 = recursive_divided_difference(list(points_example_degree_4.keys()), list(points_example_degree_4.values()))
DDs = [DD_1, DD_2, DD_3, DD_4]
for i in DDs:
print(latex(i))
print(latex(simplify(i)))
[0;31m---------------------------------------------------------------------------[0m [0;31mModuleNotFoundError[0m Traceback (most recent call last) Cell [0;32mIn[1], line 1[0m [0;32m----> 1[0m [38;5;28;01mimport[39;00m [38;5;21;01mnumpy[39;00m [38;5;28;01mas[39;00m [38;5;21;01mnp[39;00m [1;32m 2[0m [38;5;28;01mfrom[39;00m [38;5;21;01msympy[39;00m [38;5;28;01mimport[39;00m pi, sqrt, cos, symbols, simplify, factorial, sin, Abs, Rational, latex, prod, Sum [1;32m 5[0m [38;5;28;01mdef[39;00m [38;5;21mrecursive_divided_difference[39m(x_vals, y_vals): [0;31mModuleNotFoundError[0m: No module named 'numpy'
In [ ]:
P_4 = newton_interpolation(list(points_example_degree_4.keys()), list(points_example_degree_4.values()), x)
print(latex(P_4))x \left(x - \frac{\pi}{3}\right) \left(x - \frac{\pi}{4}\right) \left(x - \frac{\pi}{6}\right) \left(- \sqrt{3} + \frac{\sqrt{2}}{2} + 1\right) + \frac{6 x \left(x - \frac{\pi}{4}\right) \left(x - \frac{\pi}{6}\right) \left(- \frac{3 \sqrt{2}}{2} - \frac{1}{2} + \frac{3 \sqrt{3}}{2}\right)}{\pi} + \frac{12 x \left(x - \frac{\pi}{6}\right) \left(\frac{4 \left(- \frac{\sqrt{3}}{2} + \frac{\sqrt{2}}{2}\right)}{\pi} + \frac{12 \left(- \frac{3 \sqrt{2}}{2} - \frac{1}{2} + \frac{3 \sqrt{3}}{2}\right)}{\pi}\right)}{\pi} + \frac{6 x \left(- \sqrt{3} + \frac{\sqrt{2}}{2} + 1\right)}{\pi} + 1
In [ ]:
print(latex(simplify(P_4)))\frac{- 576 x \left(6 x - \pi\right) \left(- 8 \sqrt{3} + 3 + 8 \sqrt{2}\right) + 18 \pi x \left(- \left(4 x - \pi\right) \left(6 x - \pi\right) \left(- 3 \sqrt{3} + 1 + 3 \sqrt{2}\right) - 48 \sqrt{3} + 24 \sqrt{2} + 48\right) + \pi^{2} \left(x \left(3 x - \pi\right) \left(4 x - \pi\right) \left(6 x - \pi\right) \left(- 2 \sqrt{3} + \sqrt{2} + 2\right) + 144\right)}{144 \pi^{2}}
In [ ]:
import numpy as np
# Define the function
def f(t):
return np.exp(-t) / t
# Trapezoidal rule parameters
a = 1
b = 2
n = 8
h = (b - a) / n
# Calculate the x points
x_points = np.linspace(a, b, n+1)
# Calculate the function values at each point
f_values = f(x_points)
# Apply the trapezoidal rule
trapezoidal_approximation = (h/2) * (f_values[0] + 2*sum(f_values[1:-1]) + f_values[-1])
trapezoidal_approximation
0.17130740747361625
In [ ]:
import numpy as np
# Redefine the function and variables for Simpson's rule calculation
def f(t):
return np.exp(-t) / t
# Simpson's rule parameters
a = 1
b = 2
n = 8
h = (b - a) / n
# Calculate the x points
x_points = np.linspace(a, b, n+1)
# Calculate the function values at each point
f_values = f(x_points)
# Apply Simpson's rule
simpson_approximation = (h/3) * (f_values[0] + 2*sum(f_values[2:-1:2]) + 4*sum(f_values[1:-1:2]) + f_values[-1])
simpson_approximation
0.170490692028207
In [ ]:
import numpy as np
# Define the function and its derivatives
def f(t):
return np.exp(-t) / t
def f_double_prime(t):
return (np.exp(-t) * (1 + t**2 - 2*t)) / t**3
# Optional: Define a function for the fourth derivative or use numerical differentiation tools
# Evaluate the derivatives over the interval [1, 2]
t_values = np.linspace(1, 2, 100) # More points for better maximum estimation
second_derivative_max = np.max(np.abs(f_double_prime(t_values)))
# Trapezoidal rule error estimate
a, b, h = 1, 2, 1/8
trapezoidal_error_estimate = -(b-a) * h**2 / 12 * second_derivative_max
# Print results
print("Maximum |f''(t)| over [1, 2]:", second_derivative_max)
print("Trapezoidal Rule Error Estimate:", trapezoidal_error_estimate)Maximum |f''(t)| over [1, 2]: 0.018245965194781895 Trapezoidal Rule Error Estimate: -2.375776718070559e-05
In [ ]: