Message Boards Message Boards

0
|
5569 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

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

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
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

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

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

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

Be respectful. Review our Community Guidelines to understand your role and responsibilities. Community Terms of Use