Skip to main content

plotting - Finding the tangent to a piecewise function


I'm new to Mathematica and I am trying to calculate the expression of the tangent to the curve


f[X_] = 
Piecewise[{{-X^2 + 4 X - 3, X < 3}, {3 - X (Abs[X - 4]), X >= 3}}]


at x = 2.


I have to draw the function and the tangent line. I would really appreciate some help.



Answer



Well, easy. Just calulate the slope at $x=2$ and then, plot the tangent with that slope and through the point $(2, f(2))$.


The slope is calulated as the first derivative of $f(x)$ in $x=2$:


D[f[v], v] /. v -> 2

and the tangent line passing through $(2, f(2))$ with $f(2)=1$ is:


X*(D[f[v], v] /. v -> 2) + f[2]


Plotting both functions:


Plot[{Piecewise[{{-3 + 4 X - X^2, X < 3}, {3 - X Abs[-4 + X], X >= 3}}, 0],
X*(D[f[v], v] /. v -> 2) + f[2]}, {X, 0, 5},
PlotRange -> All, Epilog -> {Red, PointSize[0.02], Point[{2, f[2]}]}]

enter image description here


Comments