1
$\begingroup$

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?

$\endgroup$
  • 2
    $\begingroup$ 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}] $\endgroup$ – corey979 Nov 19 '16 at 16:06
  • $\begingroup$ 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! $\endgroup$ – user9660 Nov 19 '16 at 16:17
0
$\begingroup$

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)))]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you your answer is most helpful $\endgroup$ – user2379694 Dec 20 '16 at 1:42
  • 1
    $\begingroup$ @user2379694. If you found the answer helpful, you should accept it by clicking on the large checkmark that appears on lefthand side of answer. $\endgroup$ – m_goldberg Dec 20 '16 at 2:19
  • $\begingroup$ @user2379694 Accepting the answer will reward the post's author with some (unvaluable) points, will remove this question of the unanswered list, and will avoid people trying to answer your question another time. You can also upvote if you think the answer is useful. $\endgroup$ – anderstood Dec 21 '16 at 2:09
  • $\begingroup$ @anderstood. Thanks for the support. getting the question off the unanswered list is the main reason I asked for acceptance. I would point out that the OP doesn't have sufficient reputation to up-vote. $\endgroup$ – m_goldberg Dec 21 '16 at 5:12

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