Check if my Plot is Right for sin(|x|)

Hi all,

I want to know whether my plot for \sin (|x|) is correct or not, this is the code:

using Plots, LaTeXStrings
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 = -4π, 4π

f(x) = sin(x)
g(x) = sin(abs(x))

plot(f,a,b, xtick=pitick(a, b, 2; mode=:latex), xlims=(-7,7), ylims=(-3,3), 
    bottom_margin = 10mm, label=L"f(x) = \sin(x)", framestyle = :zerolines, 
    legend=:outerright)
plot!(g,a,b, xtick=pitick(a, b, 2; mode=:latex), xlims=(-7,7), ylims=(-3,3), 
    bottom_margin = 10mm, label=L"g(x) = \sin (|x|)", framestyle = :zerolines, 
    legend=:outerright)

is it true to plot with code of sin(abs(x)) ?

I’m not sure what is the question, does it look like this? (your code does not run)

julia> using UnicodePlots

julia> x = -2π:1e-3:2π;

julia> lineplot(x, sin.(abs.(x)))
      ┌────────────────────────────────────────┐ 
    1 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠏⢳⠀⠀⡇⠀⡞⠹⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡼⠀⠈⡇⠀⡇⢸⠁⠀⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⢳⠀⡇⡞⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠁⠀⠀⢸⡀⣇⡇⠀⠀⠈⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡼⠀⠀⠀⠀⡇⣿⠀⠀⠀⠀⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⢧⡿⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⠃⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠘⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠤⠤⠤⠤⠤⠤⠤⢤⠤⠤⠤⠤⠤⢼⠤⠤⠤⠤⠤⠼⡧⠤⠤⠤⠤⠤⡧⠤⠤⠤⠤⠤⡤⠤⠤⠤⠤⠤⠤⠤│ 
      │⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⡞⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⢳⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠸⡄⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⢠⠇⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⢸⠁⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠈⡇⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⢳⠀⠀⠀⣸⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⣇⠀⠀⠀⡞⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⢸⡀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⢸⠀⠀⢀⡇⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⣇⠀⢸⠁⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠈⡇⠀⣸⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
   -1 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⣄⡏⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⢹⣠⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      └────────────────────────────────────────┘ 
      ⠀-10⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀10⠀ 


3 Likes

Glad someone is using UnicodePlots

I get it that I need to use sin.(abs.(x)) instead of sin(abs(x)) but the plot stays the same for me, I am using Jupyter Notebook // IJulia.

The plot:

My question is, is it the right plot for sin(|x|) ?

No. That is a plot of two functions. Only one of those functions is \sin(|x|). (The red line).

The Julia function abs(x) gives the absolute value of x, which is often written as |x|. Was that your question?

yes that is my question, I plot on purpose sin(x) too as comparison. I think my plot is right…

Yes, your plot is fine.