# How to Reflect Tangent Line toward $y=x$ axis with CalculusWithJulia?

**URL:** <https://discourse.julialang.org/t/how-to-reflect-tangent-line-toward-y-x-axis-with-calculuswithjulia/95712>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [March 8, 2023, 5:34am UTC](https://discourse.julialang.org/t/how-to-reflect-tangent-line-toward-y-x-axis-with-calculuswithjulia/95712 "2023-03-08T05:34:54Z")\
**Posts on this page:** 6\
**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:** [March 8, 2023, 5:34am UTC](https://discourse.julialang.org/t/how-to-reflect-tangent-line-toward-y-x-axis-with-calculuswithjulia/95712/1 "2023-03-08T05:34:54Z")

</div>

Hi all,

I am able to plot reflection of a curve toward y=x axis. But how to reflect a tangent line that went through a certain point to that curve as well?

I want a tangent line plotted at (d,c).

```julia
using Plots, LaTeXStrings, Plots.PlotMeasures, CalculusWithJulia # ignore any warnings 
gr()

# Define the x-range explicitly
xrange = 0.0:0.1:23.0
# Define the transform (or anything else like `(x,y) -> (x, -y)` etc.
xyidentity = (x,y) -> (x,y)
xytransform = (x,y) -> (y, x)

function make_fg_plot()
    f(x) = log(0.2x) + sin(0.01x^2)
    a, b = 0, 2
    c = 1
    
    plt = plot(; xtick=:true, xlims = (-5,23), ylims = (-5, 23), 
	 legend = :topleft, bottom_margin=5mm)
    plt_tangent = plot(tangent(f,c); linecolor = :red, 
	 legend = :topleft, bottom_margin=5mm)

    for (T, attrs) in [(xyidentity, (;label = "", linecolor = :green)), 
                       (xytransform, (;label = "", linecolor = :red))]
    # Apply the transformation to all tuples (x, f(x))
    transformedPoints = T.(xrange, f.(xrange))
    # Make vectors out of the resulting tuples for plotting
    newx = first.(transformedPoints)
    newy = last.(transformedPoints)

    plot!(plt, newx, newy; label="")
    #plot!(plt_tangent, newx, newy; label="")
    plot!(tangent(f,c), a, b, linecolor = :red, label="")
    g(x)=x
    plot!(g,linecolor=:green, line=(:dash), label="")

    scatter!([1], [f(1)], color = "red1", label="", markersize = 3)
    scatter!([f(1)], [1], color = "red1", label="", markersize = 3)
    plot!([1,f(1)],[f(1),1], label="", linecolor=:green, line=(:dash))
    annotate!([(-2.1,1.5, (L"(d,c)", 7, :black))])
    annotate!([(2.1,-2, (L"(c,d)", 7, :black))])
    annotate!([(0.5,-3.7, (L"l_{1}", 7, :black))])
    annotate!([(-3.4,0.8, (L"l_{2}", 7, :black))])

    end
    return plt
end

make_fg_plot()

```

![Capture d’écran_2023-03-08_12-28-31](https://global.discourse-cdn.com/julialang/original/3X/c/0/c0d9664928f55c5f205c658798f3d80a340300fb.png)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 8, 2023, 8:57am UTC](https://discourse.julialang.org/t/how-to-reflect-tangent-line-toward-y-x-axis-with-calculuswithjulia/95712/2 "2023-03-08T08:57:10Z")

</div>

Is my old answer here of help?

> <https://stackoverflow.com/questions/66236968/plotting-a-line-tangent-to-a-function-at-a-point/66238889#66238889>

---

<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:** [March 8, 2023, 12:37pm UTC](https://discourse.julialang.org/t/how-to-reflect-tangent-line-toward-y-x-axis-with-calculuswithjulia/95712/3 "2023-03-08T12:37:13Z")

</div>

There is an example here [Calculus with Julia - 10&nbsp; The Inverse of a Function](https://jverzani.github.io/CalculusWithJuliaNotes.jl/precalc/inversefunctions.html#lines)

---

<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:** [March 9, 2023, 4:09am UTC](https://discourse.julialang.org/t/how-to-reflect-tangent-line-toward-y-x-axis-with-calculuswithjulia/95712/4 "2023-03-09T04:09:58Z")

</div>

It helped in the previous chapter, but this time not. I already read your old answer.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [March 10, 2023, 9:53am UTC](https://discourse.julialang.org/t/how-to-reflect-tangent-line-toward-y-x-axis-with-calculuswithjulia/95712/5 "2023-03-10T09:53:09Z")

</div>

you could try to define yourself your “inverse tangent” something like the following

```julia
using Plots, CalculusWithJulia
gr()

tangentinv(f,c)=x->c + inv(f'(c))*(x-f(c))

function make_fg_plot(f,c, xr)

    plt = plot(; xtick=:true, xlims = (-5,23), ylims = (-5, 23), aspect_ratio=:equal)

    plot!(f;linecolor = :red, label="")
    a, b = 0, 2
    plot!(tangent(f,c), a, b, linecolor = :black, label="")

    plot!(x->x,linecolor=:green, line=(:dash), label="")

    plot!(plt, f.(xr), xr; linecolor = :red, label="")
    A, B = tangent(f,c)(a), tangent(f,c)(b) # inverse of f
    plot!(tangentinv(f,c), A, B, linecolor = :black, label="")

    return plt
end

xrange = 0.0:0.1:23.0
f(x) = log(0.2x) + sin(0.01x^2)
c = 1

make_fg_plot(f,c, xrange)

```

![invertedplot](https://global.discourse-cdn.com/julialang/original/3X/1/6/167cc2bed4c086b5d7fa58cdc65608b6d9859790.png)

---

<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:** [April 6, 2023, 3:49am UTC](https://discourse.julialang.org/t/how-to-reflect-tangent-line-toward-y-x-axis-with-calculuswithjulia/95712/6 "2023-04-06T03:49:26Z")

</div>

Thanks a lot for this @rocco_sprmnt21
