I would like to draw curves that are smooth and have linear segments. These curves are to be used in the graph of one-dimensional functions, x(t). [To be used as position vs time graphs in writing homework problems for introductory physics.] Example:
I would like to draw these plots by specifying that the curve go through particular points with particular tangents. This question very nicely implements exactly what I've described so far Implementing a syntax: Smooth curves with specified points and tangents.
However, my goal is to use these curves in plots of one-dimensional functions x(t) and would really, really like to be able to generate from them curves of the derivative x'(t). Is this feasible within tikz
?
(I made the example graph above with pgfplots
. The function is a piecewise function I constructed in Mathematica. This approach would certainly work for me, but the process is a bit of a pain. I plan on making sufficiently many problems using graphs similar to this that I would really like something more efficient. The code is below.)
Code
\pgfmathdeclarefunction{MyF}{1}{%
\pgfmathparse{%
(and (1 , #1<=5)*(3.-0.5*#1-2.24667*#1^2+2.93766*#1^3-1.55322*#1^4+0.413019*#1^5-0.0534444*#1^6+0.00265741*#1^7)) +%
(and (5<#1 , #1<7)*(4)) +%
(and (7<=#1 , #1<12)*(131.4-156.613*#1+54.0096*#1^2-7.99267*#1^3+0.538*#1^4-0.0135556*#1^5)) +%
(and (12<=#1 , 1)*(1)) %
}%
}
\begin{tikzpicture}
\begin{axis}[axis lines = middle,minor tick num = 1, grid = both, xlabel = {$t$\,(\si{s})}, ylabel = {$x$\,(\si{m})}, no markers, smooth,xmin=0, xmax=14, ymin=-6, ymax=6, samples = 100, thick, unit vector ratio = 1]
\addplot +[very thick, domain=0:14] {MyF(x)};
\end{axis}
\end{tikzpicture}
Update
Both of the answers below draw the derivative by constructing an explicit mathematical function for original curve, then taking its derivative numerically.
I would really like to avoid constructing an explicit mathematical expression for the original curve because, for graphs like the one above, I find the expressions unwieldy and unnatural to generate. I would much rather generate the original curve via the method outlined in Implementing a syntax: Smooth curves with specified points and tangents or something similar.
My question is: without an explicit mathematical expression for the curve, is it feasible within LaTeX (probably via tikz
) to draw the derivative.
For example, given a path
\draw (0,0) .. controls (1,1) and (1,1) .. (2,0);
could you draw its derivative? (Restricting our attention to paths y(x) that are single-valued functions of x).
Best Answer
The easy way out is to fake the derivative;
A decoration for TikZ paths. It's relatively accurate but of course, it is depending on sane inputs for the function with not so steep bends.