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.

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!

share|improve this question
    
Please take a look at guide/ControlObjects and choose what fits your needs the best. Manipulate/Scope/Controls will show you how to use choosen object. And if you want to learn how to create dependent controls here's the link. - just in case. –  Kuba Mar 26 at 18:20

2 Answers 2

up vote 1 down vote accepted

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

share|improve this answer
    
Thank you, Christopher! As you pointed out, [[...]] was the key to make it work! As regards the 'elegance', well... it is ok to me! ;) –  AstoundingJB Mar 27 at 8:26

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.

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

Your Answer

 
discard

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.