How to draw tangent line for my second plot with MTH229?

Hi all,

I want to plot tangent lines to my second plot below (the g(x) function), but I do not know how. I am using tangent function from MTH229 package.

Here is my code:

using Plots, LaTeXStrings, MTH229
gr()

f(x) = 1+x^2
g(x) = 2 - x^2

# tangent in MTH229 package
tangent(f, c) = x -> f(c) + f'(c)*(x-c)
tangent(g, c) = x -> g(c) + g'(c)*(x-c)

c = -2.20
d = -0.5
h = 1.1

              
p1 = plot(f, -3, 3)
p2 = plot(g, -3, 3)

s1 = "f' increasing: Concave up";
s2 = "f' decreasing: Concave down";

plot(p1, p2, layout = (2, 1), legend=:outerright,
     xaxis = "x", yaxis = "y(x)", label=[s1 s2])
plot!(tangent(f, c), -2.8,-1.6 , label="")
plot!(tangent(f, d), -1.3,0.1, label="")
plot!(tangent(f, h), 0.5,1.7, label="")

plot!(tangent(g, c), -2.8,-1.6 , label="")
plot!(tangent(g, d), -1.3,0.1, label="")
plot!(tangent(g, h), 0.5,1.7, label="")

It looks like you want to specify which plot object you are drawing to with plot!:

using Plots, LaTeXStrings, MTH229
gr()

f(x) = 1+x^2
g(x) = 2 - x^2

# tangent in MTH229 package
tangent(f, c) = x -> f(c) + f'(c)*(x-c)
tangent(g, c) = x -> g(c) + g'(c)*(x-c)

c = -2.20
d = -0.5
h = 1.1


p1 = plot(f, -3, 3)
p2 = plot(g, -3, 3)

s1 = "f' increasing: Concave up";
s2 = "f' decreasing: Concave down";

plot!(p1,tangent(f, c), -2.8,-1.6 , label="")
plot!(p1,tangent(f, d), -1.3,0.1, label="")
plot!(p1, tangent(f, h), 0.5,1.7, label="")

plot!(p2, tangent(g, c), -2.8,-1.6 , label="")
plot!(p2, tangent(g, d), -1.3,0.1, label="")
plot!(p2, tangent(g, h), 0.5,1.7, label="")

plot(p1, p2, layout = (2, 1), legend=:outerright,
     xaxis = "x", yaxis = "y(x)", label=[s1 s2])

1 Like

Thanks this is what I want @j_verzani , your package is really great it helps me a lot and I am learning from it everyday!