2
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – Bobu
    Aug 2, 2013 at 20:31

2 Answers 2

1
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

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

Dynamic[
 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}];

Manipulate[
 With[{fit = NonlinearModelFit[testdata[[min ;;]], a x^2 + b x, {a, b}, x]},
  Plot[fit[x], {x, 0, 4}]
 ],
 {min, 1, 100, 1}
]
$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Bobu
    Aug 2, 2013 at 20:36
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Aug 13, 2013 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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