Curve fitting with constraints outside the range of fitting

34 views
Skip to first unread message

Jamal Assaf

unread,
Jul 13, 2022, 10:19:56 PM7/13/22
to lmfit-py
I'm trying to fit a function f(x)=a+(b+x^c)/(d+b.x)= y where x and y are arrays.
I would like to perform the fitting to get a, b, c, and d over a specific range of x only, lets say 0.1<x<1. However, I would also like to specify a condition that at x = 4 (outside the fitting range) f(4) = 2. I'm not sure if it is possible to perform this in lmfit.
I would appreciate if anyone has some thoughts or suggestions.
Thank you. 

Jonathan Gutow

unread,
Jul 14, 2022, 9:51:55 AM7/14/22
to lmfit-py
I think you are talking about fitting to multiple ranges even if one of the ranges is only a specific point. You need to fit to multiple ranges with the f(4) = 2 point set to 2 and an error of zero (weighting of infinity). If you are not clear on how to fit multiple discontinuous ranges of a data set, you can try my fitting GUI to help you generate some boiler plate code that you can modify for your specific needs. See Jupyter Pandas GUI.

Jonathan

Laurence Lurio

unread,
Jul 14, 2022, 11:04:32 AM7/14/22
to lmfi...@googlegroups.com
You should be able to eliminate one of your parameters by solving for the condition at x=4.  Then you can just fit the remaining parameters 

Larry 

On Jul 14, 2022, at 08:51, Jonathan Gutow <gu...@uwosh.edu> wrote:


--
You received this message because you are subscribed to the Google Groups "lmfit-py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/27719715-3450-4905-b37d-08af44b6ade0n%40googlegroups.com.

Jonathan Gutow

unread,
Jul 14, 2022, 12:22:29 PM7/14/22
to lmfit-py
Larry's suggestion is definitely the best option if your fit must go through the point f(4) = 2 and you are not really trying to fit a function to non-contiguous ranges of your data set.

Jonathan

Jamal Assaf

unread,
Jul 18, 2022, 11:46:10 PM7/18/22
to lmfit-py
Thank you Jonathan and Larry for your suggestions. I was able to get a close fit by placing a much higher weight to the constraint point. However to make sure the curve passes through the point, I'm not sure how to implement Larry's suggestion to eliminate one parameter by solving for f(4) = 2. I would appreciate any suggestions on that.
Thank you.
Jamal 

Laurence Lurio

unread,
Jul 19, 2022, 9:47:35 AM7/19/22
to lmfi...@googlegroups.com
You could try using something like the following as your fitting function:

from scipy.optimize import fsolve
def c4fun(x,a,b,c):
    d = fsolve(lambda z: a+(b+4**c)/(z+4*b)-2,0)[0]
    return a+(b+x**c)/(d+b*x)

Larry

Laurence Lurio

unread,
Jul 19, 2022, 10:01:22 AM7/19/22
to lmfi...@googlegroups.com
Jamal,

Sorry, I was making this too hard.

 
In your case, the equation can be solved for d directly,

e.g. 

d = (a+(b+4**c)-8b)/2

so no need to use fsolve, but the fsolve option would work for any condition regardless of whether or not you could solve for your variable.

Larry

On Mon, Jul 18, 2022 at 10:46 PM Jamal Assaf <jimmy...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages