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