-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr3.py
53 lines (43 loc) · 1.71 KB
/
r3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import numpy as np
import matplotlib.pyplot as plt
def initialize_points(interval, num_points):
"""Step 1: Initialize points across the interval."""
return np.linspace(interval[0], interval[1], num_points)
def polynomial_fit(f, x_points):
"""Step 2: Fit a polynomial to the function at the given points."""
# This is a placeholder for a more complex fitting process
y_points = f(x_points)
degree = len(x_points) - 1
coeffs = np.polyfit(x_points, y_points, degree)
return coeffs
def error_analysis(f, coeffs, interval):
"""Step 3: Analyze error across the interval."""
x_fine = np.linspace(interval[0], interval[1], 1000)
y_fine = f(x_fine)
y_poly = np.polyval(coeffs, x_fine)
error = y_fine - y_poly
return x_fine, error
# Example usage
def example_function(x):
return np.tanh(x)
interval = [0, np.pi] # Define the interval of interest
num_initial_points = 5 # Starting with 5 points
# Step 1: Initialize points
x_points = initialize_points(interval, num_initial_points)
# Step 2: Fit polynomial
coeffs = polynomial_fit(example_function, x_points)
# Step 3: Error analysis
x_fine, error = error_analysis(example_function, coeffs, interval)
# Plotting for visualization
plt.figure(figsize=(10, 5))
plt.plot(x_fine, example_function(x_fine), label='Original Function')
plt.plot(x_fine, np.polyval(coeffs, x_fine), '--', label='Polynomial Fit')
plt.scatter(x_points, example_function(x_points), color='red', label='Initial Points')
plt.title('Initial Polynomial Fit and Error Analysis')
plt.legend()
plt.show()
plt.figure(figsize=(10, 5))
plt.plot(x_fine, error, label='Error')
plt.title('Error Analysis')
plt.legend()
plt.show()