# Manipulating the derivative of (-1)^(x/a)+(-1)^(x/(a+5)) with respect to the parameter a [duplicate]

I'd just like to know if it is possible to use Manipulate for to show how the derivative of an expression such as

(-1)^(x/a)+(-1)^(x/(a+5))


varies with respect to the parameter a.

In Mathematica I can't just write derivative(equation) and get a result as I can do on-line with Wolfram|Alpha. In Mathematica I write

f[x_] := (-1)^(x/a) + (-1)^(x/(a + 7))


on one line and write

Manipulate[Plot[{Re{f'[x]]}, {x, 0, 10}, {a, 1, 10}]


on another. You guys probably know I get a plot, but I can't vary a, because I didn't write the whole thing within Manipulate.

How can I write this correctly?

• f[x_, a_] := (-1)^(x/a) + (-1)^(x/(a + 7)); Manipulate[Plot[Evaluate@Re@D[f[x, a], x], {x, 0, 10}], {a, 1, 10}] – corey979 Nov 19 '16 at 16:06
• Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – user9660 Nov 19 '16 at 16:17

Your problem is a matter of scoping. You need to get all the references to a in the same scope, but your naive approach, which makes a a free variable, puts the references to a that occur outside the Manipulate expression in a different scope than the references made inside.

There are many ways that a correct scoping may be achieved. corey979 comment gives one. Here is a slightly improved version of it.

f[x_, a_] = (-1)^(x/a) + (-1)^(x/(a + 7));
df[x_, a_] = Re[D[f[x, a], x]];
Manipulate[
Plot[df[x, a], {x, 0, 10}],
{a, 1, 10}]


The improvement is to compute the symbolic derivative only once.

I prefer keeping everything in the Manipulate expression, so I would probably write

Manipulate[
Plot[Evaluate @ Re[df[x, a]], {x, 0, 10}],
{a, 1, 10},
Initialization :> (
df[x_, a_] = D[(-1)^(x/a) + (-1)^(x/(a + 7)), x])]


Note that I didn't bother to define a function f. However, the Manipulate can be built to be based on f and implicit evaluation of the derivative. In this case, Plot has to wrapped in Dynamic because the expression Re[f'[x]] does not explicitly refer to a.

Manipulate[
Dynamic @ Plot[Re[f'[x]], {x, 0, 10}],
{a, 1, 10},
Initialization :> (
f[x_] := (-1)^(x/a) + (-1)^(x/(a + 7)))]