# Plot Sin (x) for - π or negative x-axis

**URL:** <https://discourse.julialang.org/t/plot-sin-x-for-or-negative-x-axis/90224>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [November 14, 2022, 8:09am UTC](https://discourse.julialang.org/t/plot-sin-x-for-or-negative-x-axis/90224 "2022-11-14T08:09:30Z")\
**Posts on this page:** 15\
**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 14, 2022, 8:09am UTC](https://discourse.julialang.org/t/plot-sin-x-for-or-negative-x-axis/90224/1 "2022-11-14T08:09:30Z")

</div>

Hi all,

I wonder why the sin(x) function can’t be plot for the domain of -π or the negative x-axis.

This is my code:

```julia
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 = -π, 2π/3

f(x) = (sin.(x))^(2/3)

plot(f, a, b; xtick=pitick(a, b, 4; mode=:latex), framestyle=:zerolines,
	legend=:outerright, label=L"f(x) = (\sin \ x)^{2/3}", 
	size=(720, 360), tickfontsize=10)

```

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [November 14, 2022, 8:32am UTC](https://discourse.julialang.org/t/plot-sin-x-for-or-negative-x-axis/90224/2 "2022-11-14T08:32:48Z")

</div>

You can’t raise a negative real number to a non-integer power:

```julia
julia> sin(-1.)
-0.8414709848078965

julia> ans^(2/3)
ERROR: DomainError with -0.8414709848078965:
Exponentiation yielding a complex result requires a complex argument.
Replace x^y with (x+0im)^y, Complex(x)^y, or similar.

```

---

<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 14, 2022, 4:50pm UTC](https://discourse.julialang.org/t/plot-sin-x-for-or-negative-x-axis/90224/3 "2022-11-14T16:50:05Z")

</div>

Like this:

```julia
f(x) = Complex(sin.(x))^(2/3)

```

or

```julia
f(x) =(sin.(x+0im))^(2/3)

```

they are still not working

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [November 14, 2022, 4:57pm UTC](https://discourse.julialang.org/t/plot-sin-x-for-or-negative-x-axis/90224/4 "2022-11-14T16:57:00Z")

</div>

I think you need something like `f(x) = real((complex(sin.(x)))^(2/3))` which makes a plot. I’m not sure if it’s the plot you want, but it plots over the negative part of the x axis.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 14, 2022, 5:13pm UTC](https://discourse.julialang.org/t/plot-sin-x-for-or-negative-x-axis/90224/5 "2022-11-14T17:13:22Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> `f(x) = (sin.(x))^(2/3)`

Just use `f(x) = cbrt(sin(x))^2` to compute the 2/3 power in a way that works for negative numbers (and is faster to boot). (Not sure why you have a dot here. If you want to apply `f` elementwise to a vector just do `f.(x)`.)

That’s what the `cbrt` function is for, whereas converting to complex chooses a different branch cut that you may not want here:

```julia
julia> cbrt(-1)
-1.0

julia> complex(-1)^(1//3)
0.5 + 0.8660254037844386im

julia> cbrt(-1)^2
1.0

julia> complex(-1)^(2//3)
-0.4999999999999999 + 0.8660254037844387im

```

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [November 14, 2022, 5:14pm UTC](https://discourse.julialang.org/t/plot-sin-x-for-or-negative-x-axis/90224/6 "2022-11-14T17:14:00Z")

</div>

I think you also need an extra dot for consistency

```julia
f(x) = (sin.(x)) .^ (2/3)

```

Or no dots at all and apply it element-wise by broadcasting, as Steven says.

(And then the other change)  
It would be best to put the full error message and a minimal example!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 14, 2022, 5:15pm UTC](https://discourse.julialang.org/t/plot-sin-x-for-or-negative-x-axis/90224/7 "2022-11-14T17:15:24Z")

</div>

> [@aramirezreyes](#):
>
> I think you also need a dot

Simpler to just define `f` with _no_ dots and then apply it elementwise as `f.(x)` as needed. Define scalar functions as scalar operations.

And `plot(f, a, b)` takes a scalar function, so the dots are irrelevant.

---

<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 14, 2022, 6:14pm UTC](https://discourse.julialang.org/t/plot-sin-x-for-or-negative-x-axis/90224/8 "2022-11-14T18:14:02Z")

</div>

It is a great solution,

but the question is, how about if the function is

```julia
f(x) = sin(x)^(2/5)

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 14, 2022, 6:24pm UTC](https://discourse.julialang.org/t/plot-sin-x-for-or-negative-x-axis/90224/9 "2022-11-14T18:24:22Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> how about if the function is
> 
> ```julia
> f(x) = sin(x)^(2/5)
> 
> ```

You could use

```julia
# return the real x^p (if it exists)
realpow(x::Real, p::Rational) =
    isodd(p.den) || x ≥ 0 ? copysign(abs(x)^(1//p.den), x)^p.num : throw(DomainError())

```

and then call `realpow(sin(x), 2//5)`.

(In general, most math libraries in any language tend not to provide this kind of function, as far as I can tell, because it is a nonstandard branch cut for the `x^(1/n)` power.)

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 14, 2022, 6:32pm UTC](https://discourse.julialang.org/t/plot-sin-x-for-or-negative-x-axis/90224/10 "2022-11-14T18:32:29Z")

</div>

I do think that we should provide an `nthroot` function that works for negative `x` and odd `n`. IIRC IEEE 2018 recommends it.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 14, 2022, 6:33pm UTC](https://discourse.julialang.org/t/plot-sin-x-for-or-negative-x-axis/90224/11 "2022-11-14T18:33:48Z")

</div>

Ah, I forgot about the `nthroot` function. (`realpow` seems more general, and is only possible because we have a `Rational` type.)

Filed [nthroot function · Issue #47565 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/47565)

---

<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 19, 2022, 11:20am UTC](https://discourse.julialang.org/t/plot-sin-x-for-or-negative-x-axis/90224/12 "2022-11-19T11:20:24Z")

</div>

So if one want to plot \sin(x^{7/5}) we can use `nthroot`?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 19, 2022, 1:54pm UTC](https://discourse.julialang.org/t/plot-sin-x-for-or-negative-x-axis/90224/13 "2022-11-19T13:54:38Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> So if one want to plot \sin(x^{7/5}) we can use `nthroot`?

Yes, if that’s what you mean by x^{7/5} (for negative x, the real 5th root, computed by `nthroot(x, 5)` is different from the principal nth root, computed by `complex(x)^(1//5)`).

---

<div class="post-metadata">

**Author:** ![Shubham\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shubham_kumar/32/21642_2.png) [@Shubham\_kumar](https://discourse.julialang.org/u/Shubham_kumar)\
**Post date:** [December 2, 2022, 4:03am UTC](https://discourse.julialang.org/t/plot-sin-x-for-or-negative-x-axis/90224/14 "2022-12-02T04:03:02Z")

</div>

can you please specify which paper you are talking about. I cant find it

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 2, 2022, 4:12am UTC](https://discourse.julialang.org/t/plot-sin-x-for-or-negative-x-axis/90224/15 "2022-12-02T04:12:08Z")

</div>

See [IEEE-754 recommended functions · Issue #6148 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/6148) It’s section 9.2 of the IEEE-754 standard (2008/2019 revision). Unfortunately it’s not freely available.
