如何使用scipy最小化多变量函数

时间:2015-03-13 21:58:22

标签: python

所以我有功能

    f(x) = I_0(exp(Q*x/nKT)

其中Q,K和T是常数,为了清楚起见,我将添加值

Q = 1.6x10^(-19)  
K = 1.38x10^(-23)  
T = 77.6

和n和I_0是我试图最小化的两个约束。

我的xdata是50个数据点的列表,就像我的ydata一样。到目前为止,这是我的代码:

from __future__ import division
import scipy.optimize as optimize
import numpy


xdata = numpy.array([1.07,1.07994,1.08752,1.09355,
1.09929,1.10536,1.10819,1.11321,
1.11692,1.12099,1.12435,1.12814,
1.13181,1.13594,1.1382,1.14147,
1.14443,1.14752,1.15023,1.15231,
1.15514,1.15763,1.15985,1.16291,1.16482])

ydata = [0.00205,
0.004136,0.006252,0.008252,0.010401,
0.012907,0.014162,0.016498,0.018328,
0.020426,0.022234,0.024363,0.026509,
0.029024,0.030457,0.032593,0.034576,
0.036725,0.038703,0.040223,0.042352,
0.044289,0.046043,0.048549,0.050146]

#data and ydata is experimental data, xdata is voltage and ydata is current

def f(x,I0,N):
# I0 = 7.85E-07
# N = 3.185413895
    Q = 1.66E-19
    K = 1.38065E-23
    T = 77.3692
    return I0*(numpy.e**((Q*x)/(N*K*T))-1)

result = optimize.curve_fit(f, xdata,ydata) #trying to minize I0 and N

但答案并没有给出适当优化的约束

任何帮助都会非常感激我意识到可能有一些明显的东西我不知道,我只是看不出它是什么!

2 个答案:

答案 0 :(得分:0)

我试过这个,但出于某种原因,如果你抛弃这些常量,那么函数就变成了

def f(x,I0,N):
   return I0*(numpy.exp(x/N)-1)
你得到一些合理的东西。

1.86901114e-13,   4.41838309e-02

答案 1 :(得分:0)

这是真的,当我们摆脱常数时它更好。将功能定义为:

def f(x,A,B):
    return A*(np.e**(B*x)-1)

并通过curve_fit拟合,你将能够获得明确为I0(A = I0)和B的A(你可以简单地通过N = Q /(B K T)获得N ))。我设法变得非常合适。

我认为如果有太多的常量,算法会在某种程度上混淆。