Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Currently I'm playing around with dynamics and the NonlinearModelFit function. To be precise, I want to fit a function the data, which range I want to specify dynamically. This is not a problem and works fine. But when it comes to plotting, Mathematica refuses to show me anything.

Here's an example code (the testdata consists of 100 values. The variable min gives the lower bound for the fit to begin. )

min = 50;

testdata = Table[{x*RandomReal[{0.95, 1.05}], 3 x^2 + 4 x}, {x, 0, 10, 0.1}];

fit = Dynamic[NonlinearModelFit[testdata[[min ;;]], a x^2 + b x, {a, b}, x]];

Dynamic[Plot[fit[x], {x, 0, 4}]];

Do you have an idea, how to overcome this problem?

Thanks in advance, Juergen

share|improve this question
Thx for the reply! I think i should have mentioned, what my intention was with the use of Dynamic. In the huge notebook-file I have, i want to evaluate the dynamic value "min" and all plots, functions etc. which depends on "min" and are declared as "Dynamic" should be instantaneous refreshed. –  Bobu Aug 2 '13 at 20:31
add comment

2 Answers 2

Apparently I misinterpreted this question. My old answer is preserved below for reference.

I now believe you want something like this:

min = 50;

testdata = Table[{x*RandomReal[{0.95, 1.05}], 3 x^2 + 4 x}, {x, 0, 10, 0.1}];

fit := NonlinearModelFit[testdata[[min ;;]], a x^2 + b x, {a, b}, x];

 Refresh[Plot[fit[x], {x, 0, 4}, Evaluated -> True], TrackedSymbols -> {min}]

You should get a Dynamic plot that updates when the value of min is changed. Note that the fit definition has been changed to SetDelayed and Plot is using the (undocumented) Evaluated -> True option to effect that evaluation (inside the Dynamic). Refresh was needed to get specific updating, and TrackedSymbols was used to keep the refresh from happening excessively.

You're not using Dynamic correctly. Dynamic is only for display; it should not be given as input to a function. Please see:

Try this instead:

testdata = Table[{x*RandomReal[{0.95, 1.05}], 3 x^2 + 4 x}, {x, 0, 10, 0.1}];

 With[{fit = NonlinearModelFit[testdata[[min ;;]], a x^2 + b x, {a, b}, x]},
  Plot[fit[x], {x, 0, 4}]
 {min, 1, 100, 1}
share|improve this answer
Thanks for your reply and your solution! But this is not exactly, what I'm looking for. As posted above to the other user, I'm trying to dynamically set the value of the variable min somewhere in my notebook-file. And upon setting min to the new value, I want all plots/functions which depend on "min" to be changed at the same time. –  Bobu Aug 2 '13 at 20:36
@Bobu I'm sorry, I didn't see your comment until now. The message notification system here is very unreliable. I have updated my answer now; please let me know if this is doing what you want. –  Mr.Wizard Aug 13 '13 at 17:22
add comment

Here my attempt with minimal changes to your code:

fit := NonlinearModelFit[testdata[[min ;;]], a x^2 + b x, {a, b}, x];

Dynamic[Plot[Evaluate@fit[x], {x, 0, 4}]]

Now the NonlinearModelFit is performed every time min is changed.

Don't forget to include Evaluate in your plot.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.