我在SciPy中使用fsolve
函数来求解某些非线性方程组,并且我注意到将结果与MATLAB的fsolve
进行比较以得到准确的输入和初始条件,结果却有所不同。因此,我想知道如何使用SciPy的fsolve
与MATLAB中的结果相同。
目前,我已经在Matlab中配置了fsolve
options = optimoptions('fsolve','Display','iter-detailed','PlotFcn',@optimplotfirstorderopt);
options.StepTolerance = 1e-14;
%options.OptimalityTolerance = 1e-14
options.FunctionTolerance = 1e-14;
options.MaxIterations = 100000;
options.MaxFunctionEvaluations = 400;
% options.Diagnostics = 'on'
options.Algorithm = 'levenberg-marquardt';%'trust-region'%
fun= @solveTransmissionLineForGAndC;
我已经在SciPy中像这样配置fsolve
fsolve(self.CandGTransmissionLineDistributedE,[g0,c0],Yin,xtol=1e-14,maxfev= 100000,epsfcn=1e-14)
因此,例如,对于相同的四个输入,我从MATLAB和SciPy两者获得的输出是
SciPy:[0.0263397261951065,0.0255996001756125,0.0255996001756125,0.0248335970729323]
MATLAB:
[0.036897213114754,0.036897213114754,0.036897213114754,0.036897213114754,0.036897213114754]
如何在SciPy的fsolve
中选择求解器算法(或方法)?
编辑:添加MWE
def WandG(GW,Zin):
G,W = GW # define real variables to be used for the function
R = (G**2)*(Zin.real - 0.1164) +(( 70e-12 * ( W/100 ) )**2)*( 2.9036155068866304e+16*(Zin.real - 0.1164) ) - G
I = (G**2)*( Zin.imag - 18.743998408378143 * (1-W/200) ) + (((W/100)*70e-12)**2)*( 2.9036155068866304e+16*(Zin.imag - 18.743998408378143 * (1-W/200)) ) + 170399985.53071037*(70e-12*(W/100) )
return R,I
sol = optimize.root(WandG, [0.136879496, 47.04],(12.652884410928804+14.632724423763523j), jac=False, method='lm',tol=1e-14)
使用Scipy的解决方案将是
sol.x[0] = 0.0795283512113496 # G
sol.x[1] = 36.53727146377749 # W
对于具有相同初始条件的MATLAB,求解器和Zin而言,它会返回,我更信任它,因为它与W
的最终结果(W=50
G = 0.0794
W = 44.5234