Region Bounded Between $\sin (x)$ and $\cos (x)$

Hi all,

I want to ask whether I am right that the triangle region between 0 and \pi/2 should not be included for region bounded by \sin (x) and \cos (x) between x = - \pi / 4 and x = 3 \pi / 4 ?

But, In the plot below it becomes dark blue since it is filled twice. How to make it all correctly filled so none is darker than the other?

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 = -2π, 2π

y(x) = sin(x)
y2(x) = cos(x)
xs1 = range(a, b, length=150)


plot(xs1, y, color=:red, xtick=pitick(a, b, 4; mode=:latex), 
	xlims=(-3,3), ylims=(-π/2,π/2),framestyle=:zerolines,
	linestyle=:solid,  linecolor=:red2,
	legend=:topleft, label=L"y = \sin \ x", 
	bottom_margin=3mm,
	size=(800, 460), tickfontsize=10)

plot!(xs1,y2, label=L"y = \cos \ x")

plot!(y,-π/4,3π/4, label="", fill=(0, 0.15, :blue))
plot!(y2,-π/4,3π/4, label="", fill=(0, 0.15, :blue))


I tried PolygonArea and use plot!(y2 \ y, color=:green) → not working

1 Like

try implicit functions.
I don’t know much. I got there by trial and error.

julia> using Plots, LaTeXStrings, Plots.PlotMeasures, CalculusWithJulia

julia> gr()
Plots.GRBackend()

julia> using ImplicitEquations


julia> f(x,y) = y-sin(x)
f (generic function with 1 method)

julia> g(x,y) = y-cos(x)
g (generic function with 1 method)

julia> 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
pitick (generic function with 1 method)


julia> a, b = -2π, 2π
(-6.283185307179586, 6.283185307179586)

julia> plot((f ≫ 0) & (g ≦ 0)  , fc=:gray,xtick=pitick(a, b, 4; mode=:latex),  xlims=[-π/4,π/4],lims=[-1,1], aspect_ratio=:equal)

julia> plot!((g ≫ 0) & (f ≦ 0)  , fc=:gray,xtick=pitick(a, b, 4; mode=:latex),  xlims=[-π/4,3π/4],lims=[-1,1], aspect_ratio=:equal)

PS
you could try with two colors that “added” give white

1 Like

Try the following:

plot(xs1, y, color=:red, xtick=pitick(a, b, 4; mode=:latex), 
     xlims=(-3,3), ylims=(-π/2,π/2),framestyle=:zerolines,
     linestyle=:solid,  linecolor=:red2,
     legend=:topleft, label=L"y = \sin \ x", 
     bottom_margin=3mm,
     size=(800, 460), tickfontsize=10)

plot!(xs1,y2, label=L"y = \cos \ x")

plot!(y2,-π/4,3π/4, label="", fillrange=y,fillalpha=0.3, fillcolor=:blue)

The trick was to use fillrange attribute. It’s in the documentation, but perhaps a little obscure.

P.S. question is phrased in reader friendly way, with a working example. Thanks.

1 Like

Thanks for this. Really appreciate it.

I would prefer Dan’s soluzioni.

1 Like

This is a nice solution too. Thanks a lot.

Yes, but you give another way of thinking by using Implicit Equation instead of just Plots. So I learn more here.