用于Python的Matlab拟合NonlinearLeastSquares

问题描述 投票:0回答:1

我重写了用Matlab编写的Python代码,但我无法正确解析Python中的fit函数。Matlab中的代码:

y = *Array 361x1*;
x = *Array 361x1*;
y1 = *Single value 1x1*;
x1 = *Single value 1x1*;

fo = fitoptions('Method','NonlinearLeastSquares','Lower',[-Inf,-Inf],'Upper',[Inf, Inf],'Startpoint',[0.0,0.0]);

ft = fittype( @(A,B, x) A.*x.^2 + B.*x + y1 - A.*x1.^2 - B.*x1, 'independent','x','dependent','y','options',fo);

[fitobject,gof,out] = fit(x,y,ft);

A = fitobject.A;
B = fitobject.B;

我基于this article通过Scipy最小二乘尝试了Python中的解决方案。我写了以下代码:

ft = least_squares(lambda coeffs: coeffs[0]*x**2 + coeffs[1]*x + y1 - coeffs[0]*x1**2 - coeffs[1]*x1, [0, 0], bounds=([-np.inf, -np.inf], [np.inf, np.inf]))
print(ft('x'))

显然,这是不正确的(Python代码中未考虑数组y,并且我获得了系数A和B的不同值。我已经尝试过不同的函数,例如curve%fit等,但是没有结果...] >

我重写了用Matlab编写的Python代码,但我无法正确解析Python中的fit函数。 Matlab中的代码:y = * Array 361x1 *; x = *数组361x1 *; y1 = *单一值1x1 *; x1 = *单个值...

python matlab scipy least-squares
1个回答
0
投票

您可以使用scipy.optimize的curve_fit。假设xy是包含数据的numpy.ndarrays:

© www.soinside.com 2019 - 2024. All rights reserved.