最小二乘法高斯牛顿法代码
时间: 2023-11-12 10:56:47 浏览: 162
最小二乘法代码
5星 · 资源好评率100%
以下是最小二乘法和高斯牛顿法的 Python 代码示例:
最小二乘法:
```python
import numpy as np
# 构造数据
x = np.array([0.5, 0.6, 0.7, 0.8, 0.9])
y = np.array([1.2, 1.5, 1.7, 2.0, 2.3])
# 定义模型函数
def model_func(x, a, b):
return a * x + b
# 定义误差函数
def error_func(params):
return y - model_func(x, *params)
# 最小二乘法求解
from scipy.optimize import leastsq
params_init = [1, 1]
params_fit, flag = leastsq(error_func, params_init)
print(params_fit)
```
高斯牛顿法:
```python
import numpy as np
# 构造数据
x = np.array([0.5, 0.6, 0.7, 0.8, 0.9])
y = np.array([1.2, 1.5, 1.7, 2.0, 2.3])
# 定义模型函数
def model_func(x, a, b):
return a * x + b
# 定义误差函数的一阶导数和二阶导数
def jacobian(params):
J = np.empty((len(x), len(params)))
J[:, 0] = x
J[:, 1] = 1
return J
def hessian(params):
H = np.zeros((len(params), len(params)))
H[0, 0] = np.sum(x ** 2)
H[0, 1] = np.sum(x)
H[1, 0] = np.sum(x)
H[1, 1] = len(x)
return H
# 高斯牛顿法求解
params_init = [1, 1]
params_fit = params_init
for i in range(10):
r = y - model_func(x, *params_fit)
J = jacobian(params_fit)
H = hessian(params_fit)
delta = np.linalg.solve(H, J.T @ r)
params_fit += delta
print(params_fit)
```
阅读全文