Untitled
unknown
plain_text
5 months ago
959 B
7
Indexable
import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit # Example data # Replace x_data and y_data with your actual dataset x_data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) y_data = np.array([2.3, 3.1, 4.5, 6.2, 8.0, 10.1, 12.5, 15.3, 18.2, 22.1]) # Define the model function: y = a * x^b def power_law(x, a, b): return a * np.power(x, b) # Use curve_fit to fit the model to the data params, covariance = curve_fit(power_law, x_data, y_data) # Get the fitted parameters a and b a, b = params print(f"Fitted parameters: a = {a}, b = {b}") # Generate values using the fitted model for plotting x_fit = np.linspace(min(x_data), max(x_data), 100) y_fit = power_law(x_fit, a, b) # Plot the original data and the fitted curve plt.scatter(x_data, y_data, label="Data", color="red") plt.plot(x_fit, y_fit, label=f"Fitted Curve: y = {a:.2f} * x^{b:.2f}", color="blue") plt.xlabel('x') plt.ylabel('y') plt.legend() plt.show()
Editor is loading...
Leave a Comment