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

**URL:** https://discourse.julialang.org/t/how-to-draw-tangent-line-for-my-second-plot-with-mth229/90159
**Category:** General Usage
**Tags:** plotting
**Created:** [November 12, 2022, 11:30am UTC](https://discourse.julialang.org/t/how-to-draw-tangent-line-for-my-second-plot-with-mth229/90159 "2022-11-12T11:30:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)
#### Post date: [November 12, 2022, 11:30am UTC](https://discourse.julialang.org/t/how-to-draw-tangent-line-for-my-second-plot-with-mth229/90159/1 "2022-11-12T11:30:10Z")

</div>

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:

```julia
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="")

```

---

<div class="post-metadata">

### Author: ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)
#### Post date: [November 12, 2022, 12:41pm UTC](https://discourse.julialang.org/t/how-to-draw-tangent-line-for-my-second-plot-with-mth229/90159/2 "2022-11-12T12:41:01Z")

</div>

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

```julia
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])

```

---

<div class="post-metadata">

### Author: ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)
#### Post date: [November 12, 2022, 2:43pm UTC](https://discourse.julialang.org/t/how-to-draw-tangent-line-for-my-second-plot-with-mth229/90159/3 "2022-11-12T14:43:13Z")

</div>

> [@j\_verzani](#):
>
> ```julia
> 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])
> 
> ```

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!
