31 views

Skip to first unread message

Mar 6, 2024, 1:48:18 PMMar 6

to lmfit-py

I'm having difficulty understanding how to setup constraints for my system.

I have 6 variables, we can call these A,B,C,D,E,F

I have 2 models, the coefficients are provided

model_1=aA+bB+cC

model_2=dD+eE+fF

Finally, I have a constraint whereby

(C-B)^2/100+(F-E)^2 < (A-B)^2/100+(E-D)^2

In my script I call these test_1 and test_2

Simplifying

(F-E)^2 < (A-B)^2-(C-B)^2/100+(E-D)^2

I cannot take the square root here to solve for E or F since that will result in Nan during minimization. Therefore within my function I solve for F.

So combining everything

```

from lmfit import minimize,Parameters

from lmfit.printfuncs import report_fit

import numpy as np

def model(new_paramters,coefficients,data_1,data_2):

f=(new_paramters['delta'].value)**0.5+new_paramters['E'].value

parameters_to_fit_1=np.array([new_paramters['A'],new_paramters['B'],new_paramters['C']])

parameters_to_fit_2=np.array([f,new_paramters['D'],new_paramters['E']])

fits_1=(coefficients@parameters_to_fit_1)-data_1

fits_2=(coefficients@parameters_to_fit_2)-data_2

return np.concatenate((fits_1,fits_2),axis=None)

data_1=np.array([[124.16,123.231,124.089,124.1]])

data_2=np.array([[9.178,9.167,9.16,9.176]])

coefficients=np.array([[7.67e-4,3.38e-3,9.95e-1],[1.68e-2,7.12e-2,9.11e-1],[4.54e-2,1.76e-1,7.77e-1],[3.75e-1,5.24e-1,1e-1]])

new_paramters=Parameters()

new_paramters.add('A',value=110,min=0)

new_paramters.add('B',value=110,min=0)

new_paramters.add('C',value=110,min=0)

new_paramters.add('D',value=7,min=0)

new_paramters.add('E',value=7,min=0)

new_paramters.add('delta',value=1,min=1e-10)

new_paramters.add('F',expr='((((A-B)**2-(C-B)**2)/100)+(D-E)**2)+delta',min=1e-10)

solution=minimize(model,new_paramters,args=(coefficients,data_1,data_2))

f=solution.params['delta'].value**0.5+solution.params['E'].value

test_1=(solution.params['B'].value-solution.params['C'].value)**2/100+(f-solution.params['E'].value)**2

test_2=(solution.params['A'].value-solution.params['B'].value)**2/100+(solution.params['D'].value-solution.params['E'].value)**2

report_fit(solution)

from lmfit.printfuncs import report_fit

import numpy as np

def model(new_paramters,coefficients,data_1,data_2):

f=(new_paramters['delta'].value)**0.5+new_paramters['E'].value

parameters_to_fit_1=np.array([new_paramters['A'],new_paramters['B'],new_paramters['C']])

parameters_to_fit_2=np.array([f,new_paramters['D'],new_paramters['E']])

fits_1=(coefficients@parameters_to_fit_1)-data_1

fits_2=(coefficients@parameters_to_fit_2)-data_2

return np.concatenate((fits_1,fits_2),axis=None)

data_1=np.array([[124.16,123.231,124.089,124.1]])

data_2=np.array([[9.178,9.167,9.16,9.176]])

coefficients=np.array([[7.67e-4,3.38e-3,9.95e-1],[1.68e-2,7.12e-2,9.11e-1],[4.54e-2,1.76e-1,7.77e-1],[3.75e-1,5.24e-1,1e-1]])

new_paramters=Parameters()

new_paramters.add('A',value=110,min=0)

new_paramters.add('B',value=110,min=0)

new_paramters.add('C',value=110,min=0)

new_paramters.add('D',value=7,min=0)

new_paramters.add('E',value=7,min=0)

new_paramters.add('delta',value=1,min=1e-10)

new_paramters.add('F',expr='((((A-B)**2-(C-B)**2)/100)+(D-E)**2)+delta',min=1e-10)

solution=minimize(model,new_paramters,args=(coefficients,data_1,data_2))

f=solution.params['delta'].value**0.5+solution.params['E'].value

test_1=(solution.params['B'].value-solution.params['C'].value)**2/100+(f-solution.params['E'].value)**2

test_2=(solution.params['A'].value-solution.params['B'].value)**2/100+(solution.params['D'].value-solution.params['E'].value)**2

report_fit(solution)

```

The minimization provides a reasonable solution, however. You will note my condition here broken.

test_1 should be less than test_2, but as can be observed in my scenario above test_1 > test_2 (0.026 vs. 0.023). So the constraint I have tried to setup is not working, and I'm not entirely sure why.

Mar 6, 2024, 2:19:43 PMMar 6

to lmfi...@googlegroups.com

On Wed, Mar 6, 2024 at 12:48 PM sam mahdi <sammah...@gmail.com> wrote:

I'm having difficulty understanding how to setup constraints for my system.I have 6 variables, we can call these A,B,C,D,E,FI have 2 models, the coefficients are providedmodel_1=aA+bB+cCmodel_2=dD+eE+fFFinally, I have a constraint whereby(C-B)^2/100+(F-E)^2 < (A-B)^2/100+(E-D)^2In my script I call these test_1 and test_2Simplifying(F-E)^2 < (A-B)^2-(C-B)^2/100+(E-D)^2I cannot take the square root here to solve for E or F since that will result in Nan during minimization. Therefore within my function I solve for F.

My inclination would be to do this:

Set expressions for

FminusE = 'sqrt ( (A-B)^2 - (C-B)^2/100 + (E-D)^2) - delta'

F = FminusE + E

vary delta, with a minimum value of 0. That does sort of assert that F>E. I think it might be harder if you are not sure of that sign.

--Matt

Mar 6, 2024, 2:23:21 PMMar 6

to lmfit-py

Doing that could result in nan for FminusE, that's why within my function I take the square root. If you have it in the expression the minimizer usually breaks.

Also I just want to ensure my setup here is correct. The constraint is not working but I don't know if that's because I set it up incorrectly

Mar 6, 2024, 4:52:10 PMMar 6

to lmfi...@googlegroups.com

I think your inequality constraint can give impossible values, say if C-B > 10*(A-B). That is, you don't do anything to ensure that the right-hand side of your inequality is >0.

The nan is a symptom, not the cause of the problem ;).

--Matt

--

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/94def0e9-579c-4247-b846-472e910cb3aan%40googlegroups.com.

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/94def0e9-579c-4247-b846-472e910cb3aan%40googlegroups.com.

Reply all

Reply to author

Forward

0 new messages

Search

Clear search

Close search

Google apps

Main menu