curve_fit多元函数拟合
时间: 2024-06-07 22:12:06 浏览: 120
Curve fitting in Python using the `curve_fit` function from the `scipy.optimize` module allows you to fit a multi-variable function to your data. The `curve_fit` function uses non-linear least squares to minimize the residuals between the predicted and actual data points.
Here is an example of how to perform multi-variable function fitting using `curve_fit`:
```python
import numpy as np
from scipy.optimize import curve_fit
# Define the function you want to fit
def my_func(x, a, b, c):
return a * x[0] + b * x[1] + c
# Generate some sample data
x_data = np.random.rand(100, 2) # Two-dimensional input data
y_data = my_func(x_data.T, 2, 3, 4) # Corresponding output data
# Perform curve fitting
popt, pcov = curve_fit(my_func, x_data.T, y_data)
# Print the optimized parameters
print(popt)
```
In this example, the `my_func` function is defined with three parameters (`a`, `b`, `c`) that you want to optimize. The `x_data` represents the input data, and `y_data` represents the corresponding output data. The `curve_fit` function returns the optimized parameters (`popt`) and the estimated covariance of the parameters (`pcov`).
You can modify the `my_func` function and provide your own data to fit a multi-variable function to your specific problem.
阅读全文