# Extracting parameters from a fitted function

Let's say I have written a function which performs convolution and returns value calculated for the time, t.

CalculateConvolution[length_, aifMatrix_, Ktrans_, Kep_, dt_, t_] :=
aifMatrix[[(Round[t/dt] + 1)]].CreateKernel[Ktrans, Kep, dt, length];


aifMatrix - is a matrix;

CreateKernel[Ktrans, Kep, dt, length] - is a function that returns a vector;

CreateKernel[Ktrans_, Kep_, dt_, length_] := Module[{kernel = ConstantArray[0, length]},
Do[kernel[[i]] = Ktrans*Exp[-Kep*dt*i], {i, 1, length, 1}]; kernel];


After that I use the next function to perform nonlinear least squares fitting:

NonlinearModelFit[TheoreticalTissueData, CalculateConvolutionOptimized[
Length[fiitedAIF], fittedConvMatrix, Ktrans, Ktrans/(Kep*60), 0.5, t], {Ktrans, Kep}, t]


where the TheoreticalTissueData is the data which I need to fit.

So, when I run this code I get the fitted data in the next form: {value1, value2, ..., valuen} which corresponds to the fitted curve, however I can't extract fitted parameters: Ktrans,Kep.

I tried to use ["ParameterTable"], however I didn't receive any results (because it worked for several minutes without any results, so I aborted calculations).

What is the easiest way to get the fit parameters for this case?

• Hi ! Please, visit the help centre and read more about proper code formatting. Thanks ! – Sektor Jan 8 '15 at 21:21
• I'm a bit confused by your description. When running NonlinearModelFit, the result should be a FittedModel object, not something in the form of {value1, value2, ...}. Can you clarify what you get? Once you do have a FittedModel objects (let's say stored in the variable fm), evaluating fm["ParameterTable"] should probably be quick. If it's not, can you give a short but complete example that reproduces the problem? If you need to retrieve the parameter values for programmatic use, try "BestFitParameters" instead of "ParameterTable" (which is for visual inspection). – Szabolcs Aug 4 '15 at 11:36
• Old question again, sorry ... – Szabolcs Aug 4 '15 at 11:37

Make a name for your fitting, like nlm here:

In[1]:= data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}, {6, 4}, {7, 5}};
In[2]:= nlm = NonlinearModelFit[data, Log[a + b x^2], {a, b}, x];
In[3]:= nlm["BestFitParameters"]
Out[3]= {a -> 1.50632, b -> 1.42633}


Now to get a or b you can simply do this:

In[4]:= myA = a /. nlm["BestFitParameters"];
In[5]:= myB = b /. nlm["BestFitParameters"];


I hope you'll find it useful :)