1
$\begingroup$

I have system of 10 equations that I am solving to try and model some data collected. The model using the real world values doesn't produce the best result so I wanted to try and fit the model to the data to get insight into which parameters have the largest impact on the agreement. The model randomly samples values from some distributions, then solves the 10 equations for 9 variables, and does this for a range of 'x' values.

At this stage I am omitting details of the model as it is long and not really important to the question, however I can provide snippets if required.

I have tried to use NonlinearModelFit to do this however it seems to be unhappy with using a numerical result to fit the data, instead interpreting the result of my fit function to be contraints

 NonlinearModelFit[experimentalData,   myFunc[ff, \[Gamma]l, \[Gamma]r, \[Sigma]s,    s], {{\[Gamma]l, 0, 2}, {\[Gamma]r, 0, 2}, {\[Sigma]s, 0, 1}, {s, 0,
    1}}, ff]

NonlinearModelFit: Constraints in {0.00200695,0.00248178,0.00280182,0.00315455,0.00333564,0.00345102,0.00383092,0.0039218,0.00411453,0.00429217,<<34>>} are not all equality or inequality constraints. With the exception of integer domain constraints for linear programming, domain constraints or constraints with Unequal (!=) are not supported.

The model myFunc contains essentially boils down to

test2 = NSolve[
    equations /. {\[CapitalDelta]1 -> 
       newDelta1[[p]], \[CapitalDelta]2 -> 
       newDelta2[[
        p]], \[CapitalOmega]2 -> \[CapitalOmega]2List[[p]]}, {gg, ge, 
     gr, eg, ee, er, rg, re, rr}][[All, 9, 2]];

and so the output is a list of numbers with some pre-defined length from solving this multiple times.

I assume that my issue comes from the fact it assumes the model I am trying to fit is symbolic rather than numeric. Is there a way to get this fitting to work?

$\endgroup$
6
  • $\begingroup$ Start with simple models and add additional terms to work out any computational issues. $\endgroup$
    – JimB
    Commented Dec 21, 2022 at 3:12
  • $\begingroup$ Unfortunately the model is fixed as it is a physics simulation, and is correctly computing what I want. The issue I'm having is not with the computation, but with feeding a numerical model into a fitting function that expects a symbolic one. $\endgroup$
    – D. Brown
    Commented Dec 21, 2022 at 3:38
  • $\begingroup$ I suppose I should say I'm looking for something that operates similar to Scipy.curve_fit in python, where it computes a result based on some input parameters and does a least squares fit to the data. What mathematica wants to do is to compute a symbolic model first, then add the values later which doesn't work for me using NSolve. $\endgroup$
    – D. Brown
    Commented Dec 21, 2022 at 3:42
  • $\begingroup$ If you're unable to provide any specifics (definitions of functions, data, etc.) and use what I would claim are "not well-defined terms" (such as "numerical model"), it will be difficult to give you much constructive advice. (Doesn't Scipy.curve_fit require a specified function? $\endgroup$
    – JimB
    Commented Dec 21, 2022 at 5:26
  • $\begingroup$ I've updated the main post with a snippet of the code that uses NSolve . The rest of the function is purely setting up the equations. The solver works fine, I just want to use it within a fitting function. scipy.curve_fit can take any custom function input as long as the output matches the same size array as your data you are fitting. $\endgroup$
    – D. Brown
    Commented Dec 21, 2022 at 6:03

1 Answer 1

2
$\begingroup$

According to the documentation for NonlinearModelFit, it's possible to provide a function defined by a numerical calculation. However, the calculation has to be wrapped by a Module.

The error message about constraints may be due to a syntax error in setting up myFunc. Without the code it's hard to tell.

$\endgroup$
1
  • 1
    $\begingroup$ Consider editing your answer to state that an example of using a numerically defined model is in the "Generalizations and Extensions" section of the documentation. $\endgroup$
    – bbgodfrey
    Commented Dec 22, 2022 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.