简体   繁体   English

scipy.optimize.least_squares和MATLAB lsqnonlin的区别

[英]scipy.optimize.least_squares and matlab lsqnonlin difference

I would like to find minimum of equations given in these script. 我想找到这些脚本中给出的最小等式。 It is looking very messy(but deep undestanding of equation is not needed- I suppose). 它看起来非常凌乱(但我想不需要对方程的深刻理解)。 At the end of def is the expression to minimize: def的末尾是要最小化的表达式:

 vys1=-Qd-40*sqrt(5)*sqrt((ch+cm)*ep*kb*Na*T)*w1
 vys2=fi0-fib-Q0/cq 
 vys3=fib-fid+Qd/cq1 
 vysf= np.array([vys1,vys2,vys3])
 return vysf

I write this script in matlab using lsqnonlin to compare the results. 我使用lsqnonlin在matlab中编写此脚本以比较结果。 Matlab results seems much accurate. Matlab结果似乎很准确。 Result are (fi0,fib,fid) 结果是(fi0,fib,fid)

Python 
[-0.14833481 -0.04824387 -0.00942132] Sum(value) ~1e-3.  
Matlab 
[-0,13253   -0,03253      -0,02131 ] Sum(value)~1e-15

Note that script has a check for typos in equation(if they are identical in python and matlab) for [fi0,fib,fid]=[-0.120, -0.0750 ,-0.011] the result are the same [vys1,vys2,vys3] - 请注意,脚本对[fi0,fib,fid]=[-0.120, -0.0750 ,-0.011]的等式中的错别字进行了检查(如果它们在python和matlab中相同),结果是相同的[vys1,vys2,vys3] -

python [0.00069376  0.05500097 -0.06179421]
matlab [0.0006937598,0.05500096 -0.06179421]

Are there any options in least_squares to improve results? least_squares是否有任何选项可以改善结果? Thanks for any help(sorry for misunderstanding english ) 谢谢您的帮助(对英语的理解不正确)

Python 蟒蛇

import scipy as sc
import numpy as np
from math import sinh
import matplotlib as plt
from numpy import exp, sqrt
from scipy.optimize import leastsq,least_squares

def q(par,ep,Na,kb,T,e,gamaal,gamasi,gamax,k1,k2,k3,k4,cq,cq1,ch,cm):
    fi0,fib,fid=np.array([par[0],par[1],par[2]])
    AlOH= gamaal*k1*exp(e*fi0/(T*kb))/(ch + k1*exp(e*fi0/(T*kb)))
    AlOH2= ch*gamaal/(ch + k1*exp(e*fi0/(T*kb)))
    SiO= gamasi*k2*exp(e*fi0/(T*kb))/(ch + k2*exp(e*fi0/(T*kb)))
    SiOH= ch*gamasi/(ch + k2*exp(e*fi0/(T*kb)))
    X= gamax*k3*k4*exp(e*fib/(T*kb))/(ch*k4 + cm*k3 + k3*k4*exp(e*fib/  (T*kb)))
    XH= ch*gamax*k4/(ch*k4 + cm*k3 + k3*k4*exp(e*fib/(T*kb)))
    Xm= cm*gamax*k3/(ch*k4 + cm*k3 + k3*k4*exp(e*fib/(T*kb)))
    Q0=e*(0.5*(AlOH2+SiOH-AlOH-SiO)-gamax)
    Qb=e*(XH+Xm)
    Qd=-Q0-Qb
    w1=sc.sinh(0.5*e*fid/kb/T)
    vys1=-Qd-40*sqrt(5)*sqrt((ch+cm)*ep*kb*Na*T)*w1
    vys2=fi0-fib-Q0/cq 
    vys3=fib-fid+Qd/cq1 
    vysf= np.array([vys1,vys2,vys3])
    return vysf

kb=1.38E-23;T=300;e=1.6e-19;Na=6.022e23;gamaal=1e16;gamasi=1e16
gamax=1e18;k1=1e-4;k2=1e5;k3=1e-4;k4=1e-4;cq=1.6;cq1=0.2
cm=1e-3;ep=80*8.8e-12
ch1=np.array([1e-3,1e-5,1e-7,1e-10])

# Check the equations, if they are same
x0=np.array([-0.120,    -0.0750 ,-0.011])
val=q(x0,ep,Na,kb,T,e,gamaal,gamasi,gamax,k1,k2,k3,k4,cq,cq1,ch1[0],cm)
print(val)
w1=least_squares(q,x0, args=(kb,ep,Na,T,e,gamaal,gamasi,gamax,k1,k2,k3,
                             k4,cq,cq1,ch1[0],cm))
print(w1['x'])

matlab Matlab的

function[F1,poten,fval]=test()
kb=1.38E-23;T=300;e=1.6e-19;Na=6.022e23;gamaal=1e16;gamasi=1e16;gamax=1e18;
k1=1e-4;k2=1e5;k3=1e-4;k4=1e-4;cq=1.6;cq1=0.2;ch=[1e-3];cm=1e-3;ep=80*8.8e-   12;
% Test if equation are same 
x0=[-0.120, -0.0750 ,-0.011];
F1=rovnica(x0,ch) ;
[poten,fval]= lsqnonlin(@(c) rovnica(c,ch(1)),x0);
function[F]=rovnica(c,ch) 
fi0=c(1);
fib=c(2);
fid=c(3);
aloh=exp(1).^(e.*fi0.*kb.^(-1).*T.^(-1)).*gamaal.*k1.*(ch+exp(1).^(e.* ...
fi0.*kb.^(-1).*T.^(-1)).*k1).^(-1);
aloh2=ch.*gamaal.*(ch+exp(1).^(e.*fi0.*kb.^(-1).*T.^(-1)).*k1).^(-1);
sioh=ch.*gamasi.*(ch+exp(1).^(e.*fi0.*kb.^(-1).*T.^(-1)).*k2).^(-1);
sio=exp(1).^(e.*fi0.*kb.^(-1).*T.^(-1)).*gamasi.*k2.*(ch+exp(1).^(e.* ...
fi0.*kb.^(-1).*T.^(-1)).*k2).^(-1);
Xm=cm.*gamax.*k3.*(cm.*k3+ch.*k4+exp(1).^(e.*fib.*kb.^(-1).*T.^(-1)) ...
.*k3.*k4).^(-1);
 XH=ch.*gamax.*k4.*(cm.*k3+ch.*k4+exp(1).^(e.*fib.*kb.^(-1).*T.^(-1)) ...
.*k3.*k4).^(-1);
Q0=e*(0.5*(aloh2+sioh-aloh-sio)-gamax);
Qb=e*(XH+Xm);
Qd=-Q0-Qb;
F=[-Qd+(-40).*5.^(1/2).*((ch+cm).*ep.*kb.*Na.*T).^(1/2).*sinh((1/2).*e.* ...
fid.*kb.^(-1).*T.^(-1));...
 fi0-fib-Q0/cq;...
(fib-fid+Qd/cq1)];
 end
end

There is a mistake in this line: 这行有一个错误:

w1=least_squares(q,x0, args=(kb,ep,Na,T,e,gamaal,gamasi,gamax,k1,k2,k3,
                             k4,cq,cq1,ch1[0],cm))

You have the argument kb in the wrong spot. 您在错误的位置输入了kb参数。 The signature of q is: q的签名是:

def q(par,ep,Na,kb,T,e,gamaal,gamasi,gamax,k1,k2,k3,k4,cq,cq1,ch,cm):

The argument kb is between Na and T . 参数kbNaT之间。 If you fix the args argument in the least_squares call: 如果您修复了least_squares调用中的args参数:

w1 = least_squares(q, x0, args=(ep, Na, kb, T, e, gamaal, gamasi, gamax,
                                k1, k2, k3, k4, cq, cq1, ch1[0], cm))

then the output of the Python script is 那么Python脚本的输出是

[ 0.00069376  0.05500097 -0.06179421]
[-0.13253313 -0.03253254 -0.02131043]

which agrees with the Matlab output. 这与Matlab输出一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 非线性最小二乘:使用 Levenberg-Marquardt 使用 Scipy.optimize.least_squares 重现 Matlabs lsqnonlin - Non linear Least Squares: Reproducing Matlabs lsqnonlin with Scipy.optimize.least_squares using Levenberg-Marquardt 结合 Sympy 与 scipy.optimize.least_squares - Combining Sympy with scipy.optimize.least_squares scipy.optimize.least_squares确定性吗? - Is scipy.optimize.least_squares deterministic? 将字典传递给`scipy.optimize.least_squares` - Passing a dictonary to `scipy.optimize.least_squares` scipy.optimize.leastsq 和 scipy.optimize.least_squares 之间的区别? - difference between scipy.optimize.leastsq and scipy.optimize.least_squares? scipy.optimize.least_squares-雅可比评估的限制数量 - scipy.optimize.least_squares - limit number of jacobian evaluations SciPy.optimize.least_squares() 5PL曲线优化问题 - SciPy.optimize.least_squares() 5PL Curve Optimization problems 如何在“scipy.optimize.least_squares”中添加 Tikhonov 正则化? - How to add Tikhonov regularization in “scipy.optimize.least_squares”? 编写错误函数以在python中喂scipy.optimize.least_squares - Writing an error function to feed scipy.optimize.least_squares in python scipy.optimize.least_squares 中雅可比矩阵的协方差数 - Covariance numbers from Jacobian Matrix in scipy.optimize.least_squares
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM