0

The function is f[x] := xsin(ax),the first derivative is f'[x]=axcos(ax) + sin(ax)and third derivative is f'''[x]=-(a^3)xcos(ax) - (3a^2)sin(ax).I tried to plot them all in the same graph but I just get a blank graph with the axes. How do I graph them?

So far,I tried using the Show command to get them in a single plot but mathematica says it can't plot them together. For example, to plot them together I put f(x), Derv1,Derv3 inside Show(). I also tried using Manipulate option but when I use it for f(x),f'(x) and f'''(x) altogether I get errors. What else can I do that works?

enter image description here

Kurapika
  • 23
  • 5
  • 1
    Did you provide a value for a ? – flinty Jul 15 '20 at 22:23
  • 1
    You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is useful for learning how to format your questions and answers. You may also find this meta Q&A helpful – Michael E2 Jul 15 '20 at 22:27
  • I used the plot option from below after getting the first/third derivative. That's how I learnt about Manipulate option. For f'(x) I had these values for {a, -8, 8} and for f'''(x) I had these {a, -518.394, 1847.46}. So did I do it all wrong from the get go? – Kurapika Jul 15 '20 at 22:30
  • 3
    Clear["Global\*"]; f[a_][x_] := x Sin[a x]; With[{a = 2}, Plot[Evaluate@{f[a][x], f[a]'[x], f[a]''[x]}, {x, 0, 2 Pi}, PlotLegends -> "Expressions"]]` – Bob Hanlon Jul 15 '20 at 22:37
  • @BobHanlon I am a novice at this so this looks a bit convoluted to me. Could you give a breakdown of what you did or if possible suggest a simpler alternative? – Kurapika Jul 15 '20 at 22:44
  • Clear["Global\*"]; f[a_, x_] := x Sin[a x]; With[{a = 2}, Plot[Evaluate@{f[a, x], D[f[a, x], x], D[f[a, x], {x, 2}]}, {x, 0, 2 Pi}, PlotLegends -> "Expressions"]]` – Bob Hanlon Jul 15 '20 at 22:50

1 Answers1

2

Here's a short version of what I think you're struggling with:

Clear[f, a];
f[x_] := Sin[a x];
Manipulate[
 Plot[f[x], {x, 0, 2, π}],
 {a, 1, 2}
]

The reason this plot is empty, is because Manipulate localizes the variable a, so the a in your definition of f is a different a than the one in the Manipulate. This happens because the line Plot[f[x], {x, 0, 2, π}] has no explicit reference to a in its unevaluated form. The best way to deal with this, is to propagate a as an argument so that the dependence on a becomes explicit:

Clear[f, a];
f[x_, a_] := Sin[a x];
Manipulate[
 Plot[f[x, a], {x, 0, 2 π}],
 {a, 1, 2}
]

Alternatively, if you absolutely must use a global variable:

Clear[f, a];
f[x_] := Sin[a x];
Manipulate[
  Plot[f[x], {x, 0, 2 π}],
  {a, 1, 2},
  LocalizeVariables -> False,
  TrackedSymbols :> {a}
]

As you can see, you need to tell the Manipulate to track a, otherwise it won't realize that the expression Plot[f[x], {x, 0, 2 π}] depends on a.

Sjoerd Smit
  • 23,370
  • 46
  • 75