# I Want to Plot Second Derivative of Inverse Secant Function with SymPy

**URL:** <https://discourse.julialang.org/t/i-want-to-plot-second-derivative-of-inverse-secant-function-with-sympy/99179>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [May 21, 2023, 4:52am UTC](https://discourse.julialang.org/t/i-want-to-plot-second-derivative-of-inverse-secant-function-with-sympy/99179 "2023-05-21T04:52:17Z")\
**Posts on this page:** 7\
**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:** [May 21, 2023, 4:52am UTC](https://discourse.julialang.org/t/i-want-to-plot-second-derivative-of-inverse-secant-function-with-sympy/99179/1 "2023-05-21T04:52:17Z")

</div>

Hi all,

I have the formula for the first derivative for secant inverse:  
 D\_{x} \sec^{-1} \ x = \frac{1}{|x| \sqrt{x^{2}-1}}

with |x| \> 1

I can manually find the second derivative (the second is very complex). Then copy the equation manually, but it can’t be plotted. Thus, I comment the last 2 lines. How to plot the second derivative of \sec^{-1} \ x ?

```julia
using Plots, LaTeXStrings, SymPy, Plots.PlotMeasures
gr()

function pitick(start, stop, denom; mode=:text)
    a = Int(cld(start, π/denom))
    b = Int(fld(stop, π/denom))
    tick = range(a*π/denom, b*π/denom; step=π/denom)
    ticklabel = piticklabel.((a:b) .// denom, Val(mode))
    tick, ticklabel
end

function piticklabel(x::Rational, ::Val{:text})
    iszero(x) && return "0"
    S = x < 0 ? "-" : ""
    n, d = abs(numerator(x)), denominator(x)
    N = n == 1 ? "" : repr(n)
    d == 1 && return S * N * "π"
    S * N * "π/" * repr(d)
end

function piticklabel(x::Rational, ::Val{:latex})
    iszero(x) && return L"0"
    S = x < 0 ? "-" : ""
    n, d = abs(numerator(x)), denominator(x)
    N = n == 1 ? "" : repr(n)
    d == 1 && return L"%$S%$N\pi"
    L"%$S\frac{%$N\pi}{%$d}"
end

a, b = 0, π

f(x) = sec.(x)
xs = range(a, b, length=150)
ys = f.(xs)

plot(ys, xs, color=:red, ytick=pitick(a, b, 2; mode=:latex), 
	xlims=(-3,3), ylims=(0, π), framestyle=:zerolines,
	linestyle=:solid, linecolor=:red2,
	legend=:topleft, label=L"\sec^{-1} \ x", 
	bottom_margin=3mm,
	size=(800, 400), tickfontsize=10)# the inverse function

@syms x::(real,positive)
h(x) = 1/(abs(x)*sqrt(x^2-1))
xs1 = range(1, b, length=150)
ys1 = h.(xs1)

# diff(h(x),x)

plot!(xs1, ys1, label=L"D_{x} \sec^{-1} \ x = \frac{1}{|x| \sqrt{x^{2} - 1}}", framestyle=:zerolines)

#h2(x) = -x/((x^2 - 1)^(3/2)*abs(x)) - (re(x)*Derivative(re(x), x) + im(x)*Derivative(im(x), x))*sign(x)/(x*sqrt(x^2 - 1)*Abs(x)^2)
#plot!(h2, label=L"D^{2}_{x} \sec^{-1} \ x ", framestyle=:zerolines)

```

 ![111](https://global.discourse-cdn.com/julialang/original/3X/e/1/e111cf4af977ba697368462b97c6520cc8e39223.png)

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [May 21, 2023, 8:17am UTC](https://discourse.julialang.org/t/i-want-to-plot-second-derivative-of-inverse-secant-function-with-sympy/99179/2 "2023-05-21T08:17:54Z")

</div>

This doesn’t directly answer your question, ~~but maybe there is still some problem with the formulas. WolframAlpha claims that the first derivative should have x^2 instead of |x| in the denominator, for example.~~

EDIT: My bad, I didn’t realize the two expressions are the same anyway. I’ll leave the link to WA here just in case someone else find it useful.

> **[arcsec'(x) - Wolfram|Alpha](https://www.wolframalpha.com/input?i=arcsec%27%28x%29)**
>
> Wolfram|Alpha brings expert-level knowledge and capabilities to the broadest possible range of people—spanning all professions and education levels.

You can also compare the three different functions by putting them into the prompt with commas `arcsec(x), arcsec'(x), arcsec''(x)`

---

<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:** [May 21, 2023, 11:26am UTC](https://discourse.julialang.org/t/i-want-to-plot-second-derivative-of-inverse-secant-function-with-sympy/99179/3 "2023-05-21T11:26:04Z")

</div>

try using CalculusWithJulia

```julia

using Plots, LaTeXStrings, CalculusWithJulia
gr()

y(x)=asec(x)

Dy(x)=y'(x)

DDy(x)=Dy'(x)

xs1 = range(1, 4, length=150)
ys1 = Dy.(xs1)

plot!(xs1, ys1, label=L"D_{x} \sec^{-1} \ x = \frac{1}{|x| \sqrt{x^{2} - 1}}")

ys2 = DDy.(xs1)

plot(xs1,ys2, label=L"D^{2}_{x} \sec^{-1} \ x ", framestyle=:zerolines)

```

 ![arcsec_x](https://global.discourse-cdn.com/julialang/original/3X/e/a/eac6398a9bbb2d9b1f282b312442fe239eafd8ca.png)  
 ![asec_Dasec](https://global.discourse-cdn.com/julialang/original/3X/a/a/aa7a42b92c5d9f74e85293eb42c1693efacc9b49.png)

 ![DDasec_x](https://global.discourse-cdn.com/julialang/original/3X/2/a/2a2f76dab98eb16c52facfa1731b39a2fd52641f.png)

---

<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:** [May 21, 2023, 11:44am UTC](https://discourse.julialang.org/t/i-want-to-plot-second-derivative-of-inverse-secant-function-with-sympy/99179/4 "2023-05-21T11:44:27Z")

</div>

Does the plot recipe not just work here:

```julia
using SymPy, Plots
@syms x
plot(diff(asec(x), x, 2), 0, pi, ylim = (-10, 10))

```

---

<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:** [May 23, 2023, 4:23am UTC](https://discourse.julialang.org/t/i-want-to-plot-second-derivative-of-inverse-secant-function-with-sympy/99179/5 "2023-05-23T04:23:29Z")

</div>

This is from Calculus book (Purcell):

![Capture d’écran_2023-05-23_11-21-14](https://global.discourse-cdn.com/julialang/original/3X/c/a/cad79fdf4038e78259c148d826c71ce7ef14ab64.png)

Which one is correct for the D\_{x} \sec^{-1} \ x ?

---

<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:** [May 23, 2023, 4:52am UTC](https://discourse.julialang.org/t/i-want-to-plot-second-derivative-of-inverse-secant-function-with-sympy/99179/6 "2023-05-23T04:52:51Z")

</div>

I am trying your method by using `CalculusWithJulia` and `asec`, but there is a lack of \sec^{-1} plot at the x \< -1 and I need to add another plot fro x\<-1 and one for x\>1, this is the old code :

```julia
using Plots, LaTeXStrings, Plots.PlotMeasures
gr()

function pitick(start, stop, denom; mode=:text)
    a = Int(cld(start, π/denom))
    b = Int(fld(stop, π/denom))
    tick = range(a*π/denom, b*π/denom; step=π/denom)
    ticklabel = piticklabel.((a:b) .// denom, Val(mode))
    tick, ticklabel
end

function piticklabel(x::Rational, ::Val{:text})
    iszero(x) && return "0"
    S = x < 0 ? "-" : ""
    n, d = abs(numerator(x)), denominator(x)
    N = n == 1 ? "" : repr(n)
    d == 1 && return S * N * "π"
    S * N * "π/" * repr(d)
end

function piticklabel(x::Rational, ::Val{:latex})
    iszero(x) && return L"0"
    S = x < 0 ? "-" : ""
    n, d = abs(numerator(x)), denominator(x)
    N = n == 1 ? "" : repr(n)
    d == 1 && return L"%$S%$N\pi"
    L"%$S\frac{%$N\pi}{%$d}"
end

a, b = 0, π

f(x) = sec.(x)
xs = range(a, b, length=150)
ys = f.(xs)

plot(ys, xs, color=:red, ytick=pitick(-b, b, 2; mode=:latex), 
	xlims=(-3,3), ylims=(-π/2,π),framestyle=:zerolines,
	linestyle=:solid, linecolor=:red2,
	legend=:topright, label=L"\sec^{-1} \ x", 
	bottom_margin=3mm,
	size=(800, 400), tickfontsize=10)# the inverse function

h(x) = 1/(abs(x)*sqrt(x^2 - 1))
h2(x) = 2x/((1+x^2)^2)
xs1 = range(1, b, length=150)
ys1 = h.(xs1)

# diff(h(x),x)

plot!(ys1, xs1, label=L"D_{x} \sec^{-1} \ x = \frac{1}{|x| \sqrt{x^{2}-1}}", framestyle=:zerolines)
#plot!(h2, label=L"D^{2}_{x} \cot^{-1} \ x = \frac{2x}{(1 + x^{2})^{2}}", framestyle=:zerolines)

```

\sec^{-1} at the x\< -1 is plotted here:

 ![sec1](https://global.discourse-cdn.com/julialang/original/3X/9/f/9f2eef7b20ec1249ba96aac3d6c59d27e9a9da9e.png)

With code modified from yours:

```julia
using Plots, LaTeXStrings, Plots.PlotMeasures, CalculusWithJulia
gr()

function pitick(start, stop, denom; mode=:text)
    a = Int(cld(start, π/denom))
    b = Int(fld(stop, π/denom))
    tick = range(a*π/denom, b*π/denom; step=π/denom)
    ticklabel = piticklabel.((a:b) .// denom, Val(mode))
    tick, ticklabel
end

function piticklabel(x::Rational, ::Val{:text})
    iszero(x) && return "0"
    S = x < 0 ? "-" : ""
    n, d = abs(numerator(x)), denominator(x)
    N = n == 1 ? "" : repr(n)
    d == 1 && return S * N * "π"
    S * N * "π/" * repr(d)
end

function piticklabel(x::Rational, ::Val{:latex})
    iszero(x) && return L"0"
    S = x < 0 ? "-" : ""
    n, d = abs(numerator(x)), denominator(x)
    N = n == 1 ? "" : repr(n)
    d == 1 && return L"%$S%$N\pi"
    L"%$S\frac{%$N\pi}{%$d}"
end

a, b = 1, 2π

y(x) = asec(x)
Dy(x)=y'(x)
DDy(x)=Dy'(x)

xs = range(-10, -1, length=150)
xs1 = range(a, b, length=150)
ys1 = Dy.(xs1)
ys2 = DDy.(xs1)

plot(xs, y, color=:red, ytick=pitick(-b, b, 2; mode=:latex), 
	xlims=(-3,3), ylims=(-π/2,π),framestyle=:zerolines,
	linestyle=:solid, linecolor=:red2,
	legend=:bottomleft, label=L"\sec^{-1} \ x", 
	bottom_margin=3mm,
	size=(800, 460), tickfontsize=10)# the inverse function
plot!(xs1, y, color=:red, label="")

plot!(xs1, ys1, label=L"D_{x} \sec^{-1} \ x = \frac{1}{|x| \sqrt{x^{2}-1}}",
	color=:blue2)
plot!(xs, ys1, label="",color=:blue2)

plot!(xs1, ys2, label=L"D^{2}_{x} \sec^{-1} \ x", color=:green3)
plot!(xs, ys2, label="", color=:green3)

```

The great thing is it can plot D\_{x}^{2} \sec^{-1} :

 ![sec2](https://global.discourse-cdn.com/julialang/original/3X/5/d/5d4b321442f5878b5e3feef7924d4e73d0d7ba5e.png)

it must be imaginary → the horizontal line for the first and second derivative at x\<-1  
thanks!

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [May 23, 2023, 5:47am UTC](https://discourse.julialang.org/t/i-want-to-plot-second-derivative-of-inverse-secant-function-with-sympy/99179/7 "2023-05-23T05:47:56Z")

</div>

Sorry, the formula is correct as you wrote it, I didn’t see that the term in the square root is also different (so both versions are equivalent).

I edited my original answer.
