I have a little problem using the curve_fit function included in Scipy. Here is the function I would like to fit :
def funclog(x, a, b, c, d):
return a * np.log(b * x + c) + d
The problem I have is that I would like the fit function to have a specific value on some points (y(min)=0 and y(max)=1). How can I force these points with curve_fit ?
Thank you
The requirement of the fit having specific values at x=0
, x=1
, implies that the parameters a
, b
, c
, d
are constrained according to the set of two equations:
funclog(0, a, b, c, d) = 0
, funclog(1, a, b, c, d) = 1
For the form of funclog
you are considering, you can solve this system of equations with respect to a
and d
resulting in the (unique) solution
a = 1/(-log(c) + log(b + c))
and d=log(c)/(log(c) - log(b + c))
(assuming that b
and c
are such that the denominators are not equal to zero).
Replacing these expressions for a
and d
in funclog
results in a new fitting function, namely,
(log(c) - log(b*x + c))/(log(c) - log(b + c))
,
which by default satisfies the constraints. The values of b
and c
can be found by curve_fit
.
You may try use bounds:
bounds = ([amin, bmin, cmin, dmin], [amax, bmax, cmax, dmax])
(or np.inf -np.inf if limes of param is in infininty)
next
popt1, pcov1 = curve_fit(funclog, x, y, bounds=bounds)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.