Message Boards Message Boards

0
|
5197 Views
|
10 Replies
|
8 Total Likes
View groups...
Share
Share this post:

Selecting parts of fitted model

Posted 10 years ago

here is a code to create a list and fit it with two gaussians:

list = Join[
   Table[{RandomReal[{0, 1}], RandomReal[{0, 10}]}, {i, 100}], 
   Table[{RandomReal[{1, 3}], RandomReal[{0, 30}]}, {i, 100}]];
fit = NonlinearModelFit[list, 
  A1*PDF[NormalDistribution[\[Mu]1, \[Sigma]1], t] + 
   A2*PDF[NormalDistribution[\[Mu]2, \[Sigma]2], t], { {A1, 
    15}, {\[Sigma]1, 0.5}, {\[Mu]1, 0.5}, {\[Mu]2, 1.5}, {A2, 
    40}, {\[Sigma]2, 1.5}}, t]
Show[ListPlot[list], Plot[fit[t], {t, -1, 5}, PlotStyle -> Red]]

I can select the first part of the fitted formula by:

Normal[fit][[1]]

But I can't plot or do numerical manipulation:

Plot[Normal[fit][[1]], {t, 0, 3}]

I know I can use /.t-> d construct , but it is a numerically inefficient way to do when dealing with large formulas.

Any ideas?

POSTED BY: Al Guy
10 Replies
Posted 10 years ago

I think that the "properties" of the fit will provide what you want:

fit["BestFit"][[1]]
fit["BestFit"][[2]]
Plot[Evaluate[fit["BestFit"][[1]]], {t, 0, 4}]
POSTED BY: Jim Baldwin
Posted 10 years ago

But this will work:

Plot[Normal[fit][[1]] // Evaluate, {t, 0, 3}]

And you're very welcome!

POSTED BY: David Keith
Posted 10 years ago

You are right, I see what was the problem, I can not do it in one step like Plot[Normal[fit][[1]], {t,0,4}], i need to define additional variable. Seems legit. Thanks, man.

POSTED BY: Al Guy
Posted 10 years ago

term1 plots OK for me. And in the above, In[25] defines a function t1 which is described by the term1 expression. I attach a notebook. (It might be helpful to work with a fixed data set rather than random.) Best, David

Attachments:
POSTED BY: David Keith
Posted 10 years ago

Plot[term1 , {t, 0, 4}] doesn't work. How to make it work in a numerically sound manner?

How to define and compile function f[t_]:= term1?

POSTED BY: Al Guy
Posted 10 years ago

We get different values each time because the fit is generated from random data, but I think you can get the expression as follows, and can use it to define a new function as needed. Yes?

In[23]:= exp = Normal[fit]

Out[23]= -20.981 E^(-50.6429 (-3.00695 + t)^2) + 
 18.0241 E^(-0.257059 (-2.65573 + t)^2)

In[24]:= term1 = exp[[1]]

Out[24]= -20.981 E^(-50.6429 (-3.00695 + t)^2)

In[25]:= t1[t_] = term1

Out[25]= -20.981 E^(-50.6429 (-3.00695 + t)^2)
POSTED BY: David Keith
Posted 10 years ago

I'm not sure I understand. What is the first part of the sum?

POSTED BY: David Keith
Posted 10 years ago

The Normal[fit] consists of two summands: -137.812 E^(-18.4523 (-11.7337 + t)^2) and 15.2716 E^(-0.525651 (-1.92061 + t)^2)

I need the first of them: Normal[fit][[1]], this outputs the first part, but I can't plot it or do numerical computations on it as I posted above.

POSTED BY: Al Guy
Posted 10 years ago

No, I need to select the first part of the sum.

POSTED BY: Al Guy
Posted 10 years ago

This will use the fitted model to define a function, but it is not clear there is an advantage over using fit[t], as you did in your plot.

In[5]:= f[t_] = Normal[fit]

Out[5]= -137.812 E^(-18.4523 (-11.7337 + t)^2) + 
 15.2716 E^(-0.525651 (-1.92061 + t)^2)
POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract