# Assistance in setting up constraints

31 views

### sam mahdi

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()
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.

### Matt Newville

Mar 6, 2024, 2:19:43 PMMar 6
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,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.

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

### sam mahdi

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

### Matthew Newville

Mar 6, 2024, 4:52:10 PMMar 6
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