2
$\begingroup$

Let's say, that we have a list of parameters, that we want to use in manipulate, like so:

parameters={{a,0,5},{b,-1,5}};

Of corse you could manually copy and paste that into Manipulate[], but in the problem I'm actually solving, you can't do that.

Below is a very simple example and 2 versions of the code, that don't work:

VERSION 1

plot1=ListPlot@Table[{x,2x+3}+RandomReal[.4,2],{x,-5,5}];
parameters={{a,0,5},{b,-1,5}};

Manipulate@@Join[
    {Show[plot1,Plot[a x + b, {x,-5,5}]]},
    parameters
]

VERSION 2

plot1=ListPlot@Table[{x,2x+3}+RandomReal[.4,2],{x,-5,5}];
parameters={{a,0,5},{b,-1,5}};

Manipulate[
    Show[plot1,Plot[a x + b, {x,-5,5}]],
    Splice@parameters
]

And I tied a few more things, but none of them work. How to do it?

$\endgroup$
5
  • $\begingroup$ Rather than eyeball a fit, you should use one of the fitting functions, e.g., Fit, LinearModelFit, or NonlinearModelFit. $\endgroup$
    – Bob Hanlon
    Mar 18 at 18:10
  • $\begingroup$ This was just an example. For fiting I mostly use NonlinearModelFit. But also for fiting NonlinearModelFit can be very unstble (finding the wrong local RMS minimums). In some cases, like even if you set the initial values quite close, the fit can be completely wrong. In that case doing it manually is probably the best you can (easyly) do. $\endgroup$ Mar 18 at 18:28
  • $\begingroup$ Okay, probably another easy way would be to Table the NonlinearModelFit for many different initial values of parameters in some ranges, and chose the best one, but it might still be not that stable, and with a lot of parameters, you could only take a few values for each parameter $\endgroup$ Mar 18 at 18:52
  • 1
    $\begingroup$ If you are having a problem with NonlinearModelFit you should post a question showing what you have tried and others might be able to suggest how to overcome the problem. $\endgroup$
    – Bob Hanlon
    Mar 18 at 19:03
  • $\begingroup$ I'll ask about it the next time $\endgroup$ Mar 18 at 19:49

1 Answer 1

4
$\begingroup$

Manipulate has a HoldAll attribute, so you have to manually Evaluate the list with parameters. Furthermore, use Sequence to expand the list into a sequence of arguments.

Manipulate[Show[plot1, Plot[a x + b, {x, -5, 5}]], 
 Evaluate[Sequence @@ parameters]]
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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