Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Fit multiple datasets with multiple expressions that share parameters
ResourceFunction["MultiNonlinearModelFit"][{dat1,dat2,…},{form1,form2,…},{β1,…},{x1,…}] is a generalized form of NonlinearModelFit where each dataset dati is fitted with expression formi. The expressions are allowed to share parameter values βi with each other. | |
ResourceFunction["MultiNonlinearModelFit"][{dat1,dat2, …}, <|"Expressions"→{form1,form2,…}, "Constraints"→cons |>, {β1,…},{x1,…}] fits the model subject to parameter constraints cons. |
Fit some random data to simple linear models with a shared slope parameter:
| In[1]:= | ![]() |
| Out[4]= |
| In[5]:= | ![]() |
| Out[5]= | ![]() |
Fit the same model with parameter constraints to allow for a small difference between the slopes of the lines (this works best by using a quadratic form for the difference between the slope parameters, since (a1-a2)2is differentiable whereas Abs[a1–a2] is not):
| In[6]:= | ![]() |
| Out[6]= |
| In[7]:= | ![]() |
| Out[7]= | ![]() |
It is possible to define a fitting function that evaluates to a list of values equal to the number of datasets when provided with numerical parameters:

The fit function does not evaluate symbolically, only numerically:
| In[8]:= |
| Out[8]= |
| In[9]:= |
| Out[9]= |
Use this function to fit with:
| In[10]:= | ![]() |
| Out[10]= |
| In[11]:= | ![]() |
| Out[11]= | ![]() |
Fit two Gaussian peaks with a shared location parameter:
| In[12]:= | ![]() |
| Out[13]= | ![]() |
Fit with the models that were used to generate the data:
| In[14]:= | ![]() |
| Out[14]= | ![]() |
| In[15]:= |
| Out[15]= |
Extract the fits as a list of expressions:
| In[16]:= |
| Out[16]= |
Compare the fits to the data:
| In[17]:= | ![]() |
| Out[17]= | ![]() |
The Weights option can be specified in a number of ways. First generate datasets with an unequal number of points and offset them slightly:
| In[18]:= | ![]() |
| Out[19]= | ![]() |
Fit the data normally. In this case, each individual data point has equal weights in the fit, so the first dataset gets more weight overall since it has more points:
| In[20]:= | ![]() |
| Out[21]= |
| In[22]:= |
| Out[22]= | ![]() |
Assign more weight to the second dataset:
| In[23]:= | ![]() |
| Out[24]= |
| In[25]:= |
| Out[25]= | ![]() |
Assign weights inversely proportional to the number of points in the dataset. This asserts that each dataset is equally important:
| In[26]:= | ![]() |
| Out[27]= |
| In[28]:= |
| Out[28]= | ![]() |
To assign weights for each individual data point, you can pass a list of vectors matching the input data:
| In[29]:= | ![]() |
| Out[30]= |
| In[31]:= |
| Out[31]= | ![]() |
Use a different symbol to index the datasets:
| In[32]:= | ![]() |
| Out[33]= |
| In[34]:= |
| Out[34]= |
This work is licensed under a Creative Commons Attribution 4.0 International License