# Manipulate - function of a function

i'm quite new to Mathematica and I'm trying to figure out why this is not working well. Where am I wrong? I guess this is very easy for most of you. Thanks

y = -3*x+2*alpha;
M[alpha_, y_] := (alpha + y);
Manipulate[Plot[M[alpha, x], {x, 0, 10}], {alpha, 0, 10}]

• Due to how you defined M, your Manipulate[] is now Manipulate[Plot[alpha + y, {x, 0, 10}], {alpha, 0, 10}]. Why not define M as alpha - 3 x + 2 alpha? Mar 26 '17 at 11:44
• Because I want to recall y as a very long expression (here easy as an example). Mar 26 '17 at 11:47
• Then define y as a function itself; e.g. y[alpha_, x_] := (* stuff *). Mar 26 '17 at 11:51
• I think this is the reason: y[x_] := -3*x; M[alpha_, y_] := (alpha + y); Manipulate[Plot[M[alpha, y[x]], {x, 0, 10}], {alpha, 0, 10}] I have to state y[x] inside the Manipulate rather than just y! Ps I dont know how to make the code grey as you did! Mar 26 '17 at 12:00
• "I have to state y[x] inside the Manipulate rather than just y!" - of course; why should Mathematica have to guess that y is explicitly dependent on x? You have to tell the software yourself. Mar 26 '17 at 12:02

In Mathematica you can work with formulae as well as with functions. It is probably best to stick to one approach and not mix.

To make sure that we do not mix up our own symbols with those that are defined in Mathematica already, it is good practise to:

• use $symbolname for a global constant (e.g. a fixed formula here) • use \[FormalX], \[FormalY], ... as placeholders in formulae, since that way we can be sure, that they cannot be assigned any values by accident (Formal Symbols) ## Working with formulae This approach will work using formulae: $y = -3 \[FormalX] + 2 \[FormalA]; (* the formula for y *)

$m = \[FormalA] +$y; (* the formula for m uses the formula for y *)

Manipulate[
Plot[
\$m /. { \[FormalA] -> alpha, \[FormalX] -> x },
{x, 0, 10}
],
{alpha, 0, 10}
]


## Working with functions (aka delayed definitions)

This will work using functions:

y[x_, α_] := -3 x + 2 α
m[α_, y_] := α + y

Manipulate[
Plot[
m[ alpha, y[x, alpha] ],
{x, 0, 10}
],
{alpha, 0, 10}
]

• @Andrea In some cases it is good to remember the advice given in the Details section of the documentation for Plot: "In some cases, it may be more efficient to use Evaluate to evaluate f symbolically before specific numerical values are assigned to x."
– gwr
Mar 27 '17 at 10:04
• No need to define y[x,alpha] inside m[a_,y_] rather than simply y? Could you comment this ? Apr 12 '17 at 15:08
• @AndreaG That is really what learning Mathematica's core language is all about: Patterns. In m[ a_, y_] := ...  the yrefers to the locally defined symbol with regard to the definition of m, so it will be whatever is passed as argument to m. Good resource for learning, the Virtual Book. Here Introductionand Core Language are a must.
– gwr
Apr 12 '17 at 15:26
• now everything is clear. Thanks. May I ask you if you can also check my other question called "Series with Abs giving Imaginary unit as output". Is help asking allowed on this forum? Apr 12 '17 at 15:38
• What if you are doing lots of processing, and then store the result to a variable. It's a little cumbersome to have to list every single variable to make a function definition. Any ways to make that easier? Oct 3 at 19:23