3
$\begingroup$

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}]
$\endgroup$
5
  • $\begingroup$ 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? $\endgroup$
    – J. M.'s torpor
    Mar 26 '17 at 11:44
  • $\begingroup$ Because I want to recall y as a very long expression (here easy as an example). $\endgroup$
    – Andrea G
    Mar 26 '17 at 11:47
  • 1
    $\begingroup$ Then define y as a function itself; e.g. y[alpha_, x_] := (* stuff *). $\endgroup$
    – J. M.'s torpor
    Mar 26 '17 at 11:51
  • $\begingroup$ 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! $\endgroup$
    – Andrea G
    Mar 26 '17 at 12:00
  • $\begingroup$ "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. $\endgroup$
    – J. M.'s torpor
    Mar 26 '17 at 12:02
4
$\begingroup$

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}
]
$\endgroup$
6
  • $\begingroup$ @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." $\endgroup$
    – gwr
    Mar 27 '17 at 10:04
  • $\begingroup$ No need to define y[x,alpha] inside m[a_,y_] rather than simply y? Could you comment this ? $\endgroup$
    – Andrea G
    Apr 12 '17 at 15:08
  • $\begingroup$ @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. $\endgroup$
    – gwr
    Apr 12 '17 at 15:26
  • $\begingroup$ 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? $\endgroup$
    – Andrea G
    Apr 12 '17 at 15:38
  • $\begingroup$ 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? $\endgroup$
    – Ion Sme
    Oct 3 at 19:23

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.