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

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 ?

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)  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. You can also compare the three different functions by putting them into the prompt with commas arcsec(x), arcsec'(x), arcsec''(x) try using CalculusWithJulia  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)  1 Like Does the plot recipe not just work here: using SymPy, Plots @syms x plot(diff(asec(x), x, 2), 0, pi, ylim = (-10, 10))  This is from Calculus book (Purcell): Which one is correct for the D_{x} \sec^{-1} \ x ? 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 : 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:

With code modified from yours:

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} :

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

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).