3
$\begingroup$

Suppose I have a function of a variable f, depending on some other parameters a and b, for example:

fun[f_,a_,b_] := Sin[a*f] + Cos[b*f]

I can plot and manipulate this function for some independent values of the parameters a and b:

Manipulate[Plot[fun[f,a,b], {f, 0, 10}], {a,0,5},{b,0,5}]

enter image description here

Now, suppose I would like to give discrete values to these parameters, and change both at the same time. For example,I define two points:

A = Sequence[1,1];

and

B = Sequence[4,2];

I can do this plots:

Plot[fun[f,A], {f, 0, 10}] or Plot[fun[f,B], {f, 0, 10}]

but when I try to combine them, in order to include the possibility to manually choose between 'A' or 'B',

Manipulate[Plot[fun[f, X], {f, 0, 10}], {X, A, B}]

I obtain an empty plot... Instead, I would like to get a plot with two button, 'A' and 'B', corresponding to two different pairs of values for the parameters a and b.

I tried to use List[ ] instead of Sequence[ ], but it failed to work too! I also tried Row[ ], adding some braces but nothing happened... :(

What should I do? Where is the problem with my code?

Thank you all in advance!

$\endgroup$
1

2 Answers 2

3
$\begingroup$

Is something like this what you seek?

fun[f_, a_, b_] := Sin[a f] + Cos[b f];
Manipulate[
 Plot[fun[f, params[[1]], params[[2]]], {f, 0, 10}],
   {{params, {1, 1}}, {{1, 1} -> "A", {4, 2} -> "B"}}]

There are probably more elegant ways to do this, but it sounds like you want a finite set of parameter pairs. You could have a single variable represent each parameter pair in the manipulate, and then you could select which param to pass to your function using Part (the [[...]] operator).

Manipulate with discrete parameters

$\endgroup$
1
  • $\begingroup$ Thank you, Christopher! As you pointed out, [[...]] was the key to make it work! As regards the 'elegance', well... it is ok to me! ;) $\endgroup$ Mar 27, 2014 at 8:26
4
$\begingroup$

Better to use Lists, for your A and B e.g. something like:

a = {1, 1}; b = {4, 2};

Then use Part, [[i]], to extract the elements.

Manipulate[
 Plot[fun[f, x[[1]], x[[2]]], {f, 0, 10}], {{x, a}, {a -> "A", b -> "B"}}]

Sequence should not be used to group items. Most functions automatically splice in Sequence objects, e.g. Head[A] gives an error message rather than returning Sequence, and this can be confusing. Indeed the definition of the control x in your Manipulate ends up being {x, 1, 1, 4, 2} which results in an InputField control rather than the more usual SetterBar or Slider. (It's also not a good idea to use uppercase variables as these can be reserved by the system, e.g. N.)

If you were to have a variable number of arguments to fun then you may want to use a construct like fun[f, Sequence@@list] to splice in the arguments but most times you would simply pass in list and use that in the function.

$\endgroup$
4
  • 1
    $\begingroup$ Could you explain why Generally Sequence should be avoided in Mathematica code. further? $\endgroup$ Mar 26, 2014 at 18:22
  • $\begingroup$ Thank you, Mike, for your answer and your precious suggestions! $\endgroup$ Mar 27, 2014 at 8:27
  • $\begingroup$ @belisarius - is that better? $\endgroup$ Mar 27, 2014 at 17:49
  • $\begingroup$ @MikeLimaOscar Perhaps Sequence[] should be avoided as a mean of grouping objects for later treatment, but not "in general" $\endgroup$ Mar 27, 2014 at 17:55

Your Answer

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

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