# Fitting data with results generated from a numerical model

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?

– JimB
Commented Dec 21, 2022 at 3:12
• 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. Commented Dec 21, 2022 at 3:38
• 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. Commented Dec 21, 2022 at 3:42
• 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?
– JimB
Commented Dec 21, 2022 at 5:26
• 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. Commented Dec 21, 2022 at 6:03