# Plot The Squeeze Theorem Correctly

**URL:** <https://discourse.julialang.org/t/plot-the-squeeze-theorem-correctly/86585>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [August 31, 2022, 7:07am UTC](https://discourse.julialang.org/t/plot-the-squeeze-theorem-correctly/86585 "2022-08-31T07:07:27Z")\
**Posts on this page:** 6\
**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:** [August 31, 2022, 7:07am UTC](https://discourse.julialang.org/t/plot-the-squeeze-theorem-correctly/86585/1 "2022-08-31T07:07:27Z")

</div>

Hi all,

I am trying to plot something to follow the Squeeze Theorem. It turns out to become funny.

```julia
using Plots, LaTeXStrings
pyplot(fmt=:svg)

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π
#plot(sin, a, b; xtick=pitick(a, b, 4), label="y = sin t", size=(720, 250))
#plot(cos, a, b; xtick=pitick(a, b, 4), label="y = cos t", size=(720, 250))

f(x) = 1 + sin(x+π)
g(x) = -1 + sin(x+π) 
h(x) = sin(4x+π)

plot(f, a, b; xtick=pitick(a, b, 4; mode=:latex), label="f", size=(720, 360), tickfontsize=10)
plot!(g, a, b; xtick=pitick(a, b, 4; mode=:latex), label="g", size=(720, 360), tickfontsize=10)
plot!(h, a, b; xtick=pitick(a, b, 4; mode=:latex), label="h", size=(720, 360), tickfontsize=10)

```

The result that I want is like this:

 ![what-is-the-squeeze-theorem](https://global.discourse-cdn.com/julialang/original/3X/f/a/fafa224febe6862d6a77d941b6ab31afc047be1e.png)

what should I do to make it works?

The `f` and `h` functions are the `sin` that is translated toward y-axis, but how to draw the g that follow the movement of the outside functions?

Like putting Bollinger Bands indicator toward the securities chart.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 31, 2022, 4:56pm UTC](https://discourse.julialang.org/t/plot-the-squeeze-theorem-correctly/86585/2 "2022-08-31T16:56:58Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> but how to draw the `g` that follow the movement of the outside functions?

The simplest is to define: `g(x) = (f(x) + h(x))/2` or any other weighting, e.g., `g(x) = 0.3*f(x) + 0.7*h(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:** [September 1, 2022, 10:08am UTC](https://discourse.julialang.org/t/plot-the-squeeze-theorem-correctly/86585/3 "2022-09-01T10:08:45Z")

</div>

It is great but I actually want it to look like this:

![Capture d’écran_2022-09-01_17-06-56](https://global.discourse-cdn.com/julialang/original/3X/6/a/6af031cf96fd43e652c8f0610d83308e4b0fcf06.png)

I manage to create a simple one:

```julia
using Plots, LaTeXStrings
pyplot(fmt=:svg)

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π

h(x) = x*cos(1/x)
i(x) = abs(x)
j(x) = -abs(x)

plot(i, a, b; xtick=pitick(a, b, 4; mode=:latex), label="f", size=(720, 360), tickfontsize=10)
plot!(j, a, b; xtick=pitick(a, b, 4; mode=:latex), label="g", size=(720, 360), tickfontsize=10)
plot!(h, a, b; xtick=pitick(a, b, 4; mode=:latex), label="h", size=(720, 360), tickfontsize=10)

```

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [September 1, 2022, 11:27am UTC](https://discourse.julialang.org/t/plot-the-squeeze-theorem-correctly/86585/4 "2022-09-01T11:27:22Z")

</div>

Just for the mathematical fun, you could generalise refael’s answer as  
`g(x) = f(x) * k(x) + h(x) * (1-k(x))` and then pick any kind of function for k which is in [0,1]. For example `k(x) = 0.5 + 0.5*cos(x)` (which is exactly your example) or something crazy like `k(x) = 0.5 + 0.5*cos(x + sin(4*x))` 😃

Maybe take `f` and `h` as interpolated functions, then you don’t have to find a crazy function by hand.

---

<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:** [September 1, 2022, 12:14pm UTC](https://discourse.julialang.org/t/plot-the-squeeze-theorem-correctly/86585/5 "2022-09-01T12:14:39Z")

</div>

That’s what I love about Julia Discourse and Mathematics. Sharing thoughts and solving problems together.

---

<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:** [September 1, 2022, 1:20pm UTC](https://discourse.julialang.org/t/plot-the-squeeze-theorem-correctly/86585/6 "2022-09-01T13:20:47Z")

</div>

> [@SteffenPL](#):
>
> k(x) = 0.5 + 0.5_cos(x + sin(4_x))

```julia
k(x) = 0.5 + 0.5*cos(x + sin(4*x))

```

this works great
