3
$\begingroup$

How can I plot a function defined as a derivative of another function?

For example,

y[x_] := Sin[x]

y1[x_] := D[y[x], x] 

Plot[y1[x], {x, 0, 2 Pi}]

doesn't work. What is wrong with this? (y[x_] may of course be a much more complicated function!)

$\endgroup$
2
  • 3
    $\begingroup$ Like this: Plot[y'[x], {x, 0, 2 π}]. $\endgroup$ – J. M.'s torpor Jun 22 '16 at 6:00
  • $\begingroup$ Welcome! I suggest the following: 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. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – user9660 Jun 22 '16 at 7:52
4
$\begingroup$

You should pay attention to the differece of "=" and ":="

y[x_] := Sin[x] 
y1[x_] = D[y[x], x]
Plot[y1[m], {m, 0, 2 Pi}]

then you can get the result

enter image description here

If you write in the form of

 y1[x_] := D[y[x], x]

the mathematica will calculate y[x] firstly to get a number, then the derivative will make no sense.

$\endgroup$
2
  • $\begingroup$ Using := one should evaluate the right-hand side y1[x_] := Evaluate[D[y[x], x]]. $\endgroup$ – BoLe Jun 22 '16 at 9:23
  • 1
    $\begingroup$ Or just Plot[y'[x], {x, 0, 2 \[Pi]}]. $\endgroup$ – murray Jun 22 '16 at 19:49
1
$\begingroup$

You can also think about storing a pure function. Instead of:

y[x_] := Sin[x]/x

Define a function as:

z = Function[x, Sin[x]/x]

Then plot:

Plot[{z[x], z'[x]}, {x, 0, 6 Pi},
 PlotRange -> All]
$\endgroup$

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.