Question

I want to access the fit-parameters of a NonlinearModelFit. Here is the code

model = a*Cos[b*t + c];
fit = NonlinearModelFit[data, model, {a, b, c}, t, Method -> NMinimize]

When I use the command:

fit["BestFitParameters"]

the values are returned in the following format:

{a -> 1, b -> 2, c -> -3}

Now i want to store the value of a in a variable x

x=fit["BestFitParameters"][[1]]

but this gives

x= a -> 1

No I want to know how I can resolve the "-> - Operator" to obtain

x=1

Thanks in advance!

Was it helpful?

Solution

Well, you could write

x = a/.{a -> 1, b -> 2, c -> -3}

which will assign the value of a to x.

I'm not entirely sure that you should do this, you could simply store the list of rules in a variable and extract the bits from it as and when you want to.

OTHER TIPS

Might I suggest a yet another approach:

C1fitparameters = 
Table[{C1fit["BestFitParameters"][[i, 1]], C1fit["BestFitParameters"][[i, 2]]}, 
{i, 1, Length[C1fit["BestFitParameters"]]}]

Which returns a list:

{{A, 5.11419}, {Beta, 14.2637}, {Omega_0, 174.118}, {Phi, -0.117246}}

So to access either the variable name:

C1fitparameters[[1, 1]]

returns A. And:

C1fitparameters[[1, 2]]

returns the value of A which is 5.11419. Voila.

You want

x = fit["BestFitParameters"][[1]][[2]]

This retrieves the right-hand-side of a->1. using x = fit["BestFitParameters"][[1]][[1]] would retrieve the left-hand-side ("a", in this case)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top