# Horizontal Slicing in Integral

**URL:** <https://discourse.julialang.org/t/horizontal-slicing-in-integral/92043>\
**Category:** Offtopic\
**Tags:** question\
**Created:** [December 23, 2022, 10:29am UTC](https://discourse.julialang.org/t/horizontal-slicing-in-integral/92043 "2022-12-23T10:29:46Z")\
**Posts on this page:** 11\
**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:** [December 23, 2022, 10:29am UTC](https://discourse.julialang.org/t/horizontal-slicing-in-integral/92043/1 "2022-12-23T10:29:46Z")

</div>

Hi all,

it is quite hard for me to understand for part (c)

this is the question:

![Capture d’écran_2022-12-23_17-16-16](https://global.discourse-cdn.com/julialang/original/3X/3/6/361af6eb4e7fddc3f87fe524a38e98b65d46c5fc.png)

I have worked on part (b):

Basically just make integral from the interval [1,c] and find half the area of part (a)

\int\_{1}^{c} \frac{1}{x^{2}} = \frac{5}{12}

I get the c=12/7

```julia
using Plots, LaTeXStrings, SymPy
gr()

x = collect(range(0, 4, length= 100))
f(x) = 1/(x^2)

function Area(f, a, b, n)
xs = a:(b-a)/n:b
deltas = diff(xs)
cs = xs[1:end-1]
sum(f(cs[i]) * deltas[i] for i in 1:length(deltas))
end

r1 = round(Area(x -> 1/(x^2), 1, 12/7, 50000), digits=5)
r2 = round(Area(x -> 1/(x^2), 12/7, 6, 50000), digits=5)

plot(f,0,7, xtick=1:5:6, xlims=(0,7), ylims=(0,1.2), 
	framestyle=:zerolines,
	label=L"f(x) = \frac{1}{x^{2}}", legend=:topright)

# Fill the area
plot!(f,1,12/7, label="", fill=(0, 0.25, :blue))
plot!(f,12/7,6, label="", fill=(0, 0.45, :blue))

# The line that bisects the area vertically
plot!([12/7], seriestype="vline", color=:green, label="")

# Annotate the x line that bisects the area
annotate!([(12/7,-0.05, (L"x = \frac{12}{7}", 7, :blue))])

# Annotate the area
annotate!([(1.33,0.25, ("Left area =", 7, :black))])
annotate!([(1.3,0.15, (r1, 7, :black))])
annotate!([(2.23,0.05, ("Right area = ", 7, :black))])
annotate!([(2.97,0.05, (r2, 7, :black))])

```

then I try to work on part (c), the horizontal slicing, but the result is questionable, I still don’t understand

If I do this x = \frac{1}{\sqrt{y}}, and the interval will be [1/36,1], then the integral should be:

\int\_{d}^{1} \frac{1}{\sqrt{y}} = \frac{5}{12}

I will get d=0.627, but when I calculate the integral for the area of this function in y terms:

![Capture d’écran_2022-12-23_17-25-16](https://global.discourse-cdn.com/julialang/original/3X/0/c/0cf2e1b8ea34ddd69f0735f05fd7545c0d2d3506.png)

it get the area of 20/12 \approx 1.66667

But the integral for area of this function in x terms get the total area of half the area in y terms:

![Capture d’écran_2022-12-23_17-27-25](https://global.discourse-cdn.com/julialang/original/3X/1/8/182eebfb3b250e96ca2ff7403ec07aad9a6f742b.png)

it get the area of 10/12 \approx 0.8333

Which part is it that I am missing or lost?

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 23, 2022, 10:56am UTC](https://discourse.julialang.org/t/horizontal-slicing-in-integral/92043/2 "2022-12-23T10:56:41Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> Which part is it that I am missing or lost?

I think you have an extra part: The strip `0 < x < 1 && 1/36 < y < 1` is not part of the area when calculating using `y = 1/x^2`, while it is when integrating `x = 1/sqrt(y)`.

So you might want to integrate `x = 1/sqrt(y) - 1`.

In the other direction, when calculating the area, the strip `1 < x < 6 && 0 < y < 1/36` is included and you need to decide if this is part of the area or not. If it is, and is not in the other direction, then you need to adjust what ‘half the area’ means in each direction (specifically, 5/12 isn’t the same in the other direction).

Hope this clears up the source of the inconsistency.

---

<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:** [December 23, 2022, 11:05am UTC](https://discourse.julialang.org/t/horizontal-slicing-in-integral/92043/3 "2022-12-23T11:05:24Z")

</div>

Well, I read the manual / solution, and still confused since I do not get half the area after integrating

![Capture d’écran_2022-12-23_18-03-33](https://global.discourse-cdn.com/julialang/original/3X/e/7/e7cc8c868ae825c63c943e3a57759fb399dedddf.png)

\int\_{d}^{1} \frac{1}{\sqrt(y)}

it becomes \frac{5}{12} \approx 0.4163 with d=0.627

and then

\int\_{1/36}^{d} \frac{1}{\sqrt(y)}

becomes 1.25033329825300

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 23, 2022, 11:10am UTC](https://discourse.julialang.org/t/horizontal-slicing-in-integral/92043/4 "2022-12-23T11:10:36Z")

</div>

The solution might have a mistake!  
They add `(1/36)*5` at the top of the answer, corresponding to the horizontal strip below `1/36`, but they forget to subtract `(1 * (35/36))` which is the extra vertical strip calculated by the integral.

---

<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:** [December 23, 2022, 11:16am UTC](https://discourse.julialang.org/t/horizontal-slicing-in-integral/92043/5 "2022-12-23T11:16:47Z")

</div>

I think so too, the solution is a mistake and add more confusion for me.

I plot part (c) like this:

![Capture d’écran_2022-12-23_18-15-16](https://global.discourse-cdn.com/julialang/original/3X/5/c/5c0e858aaa61f58beca405c36a73cc699e94ad07.png)

With part (b) everything seems so smooth and easy to understand:

 ![ch1-calculus-6applicationsoftheintegral-10](https://global.discourse-cdn.com/julialang/original/3X/1/c/1cad276e2687fc24945e1f52fdd3ceb29557f95d.png)

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 23, 2022, 11:22am UTC](https://discourse.julialang.org/t/horizontal-slicing-in-integral/92043/6 "2022-12-23T11:22:06Z")

</div>

Trying to fix the answer, we have

```julia
integral(1/sqrt(y) from d to 1) = 5/12 + 1*(1-d)
# last term the added area

```

so

```julia
2*sqrt(y) from d to 1 = 17/12 - d 
# this becomes a quadratic
d - 2*sqrt(d) + 7/12 = 0

```

The solution of which is:

```julia
# ignoring 2nd solution outside range
1-sqrt(5/12) ~ 0.3545
# but 0.3545 is sqrt(d) so
d ~ 0.3545^2 ~ 0.1257

```

So try using the line `y = 0.1257` to bisect.

Also, in the flipped chart, it would be better to color the region only between `x = 1` and `x = 6` to get the area right. The `x = 6` doesn’t need any change, but the `x = 1` requires to color from a little above the axis. Finally, you might want to color the rectangle `1 < x < 6 && 0 < y < 1/36` in the flipped chart.

---

<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:** [December 23, 2022, 12:09pm UTC](https://discourse.julialang.org/t/horizontal-slicing-in-integral/92043/7 "2022-12-23T12:09:34Z")

</div>

I try to solve the quadratic using Sympy:

```julia
d = symbols("d")

p = d-2*sqrt(d) + 7/12	

solve(p,d) 

```

the answer is

**2-element Vector{Sym}:**  
\*\* 0.125672217930861\*\*  
\*\* 2.70766111540247\*\*

Thus when I plot at the original function:

 ![Capture d’écran_2022-12-23_19-08-12](https://global.discourse-cdn.com/julialang/original/3X/1/e/1ec37314232457b642cacc2dbb4f8fd57ec8db12.png)

It looks convincing.

But I want to know why do you add ` 1*(1-d)` ? in

```julia
integral(1/sqrt(y) from d to 1) = 5/12 + 1*(1-d)

```

the reasonable why you add that term? Because I want to logically understand this solution.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 23, 2022, 12:12pm UTC](https://discourse.julialang.org/t/horizontal-slicing-in-integral/92043/8 "2022-12-23T12:12:14Z")

</div>

(numerical error in prev answer fixed)

As for `1*(1-d)` this is the area of the strip `0 < x < 1` and `d < y < 1` which gets added in the integral (because it calculates area from axis and not from 1 and the integral length is `1-d`).

In the chart, you can now add the `Top area` and `Bottom area` annotations.

---

<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:** [December 23, 2022, 1:18pm UTC](https://discourse.julialang.org/t/horizontal-slicing-in-integral/92043/9 "2022-12-23T13:18:44Z")

</div>

Alright, after a long thinking. This is what I thought would be right:

for the flipped chart \frac{1}{\sqrt{y}}

 ![Capture d’écran_2022-12-23_20-18-13](https://global.discourse-cdn.com/julialang/original/3X/7/8/7846a604a6303fa7da0ae958d9e5c176da07d615.png)

my justification is:

\int\_{0.340472}^{0.627} \frac{1}{\sqrt{y}} \ dy= \int\_{0.627}^{1} \frac{1}{\sqrt{y}} \ dy \approx 0.41633

thus it will make the sum as half the area of part (a).

The code for the flipped chart:

```julia
using Plots, LaTeXStrings, Plots.PlotMeasures, SymPy
gr()

f(x) = 1/(sqrt(x))

function Area(f, a, b, n)
xs = a:(b-a)/n:b
deltas = diff(xs)
cs = xs[1:end-1]
sum(f(cs[i]) * deltas[i] for i in 1:length(deltas))
end

r1 = round(Area(x -> 1/(sqrt(x)), 0.340472, 0.627, 50000), digits=5)
r2 = round(Area(x -> 1/(sqrt(x)), 0.627, 1, 50000), digits=5)

plot(f,0,2, xtick=false, xlims=(0,1.2), ylims=(0,6.2), 
	framestyle=:zerolines, bottom_margin=5mm,
	label=L"x = \frac{1}{\sqrt{y}}", legend=:topright)

# Fill the area
plot!(f,0.340472,0.627, label="", fill=(0, 0.25, :blue))
plot!(f,0.627,1, label="", fill=(0, 0.45, :blue))

# The line that bisects the area vertically
plot!([0.627], seriestype="vline", color=:green, label="")

# Annotate the interval tick line 
annotate!([(0.340472,-0.01, (L"|", 6, :blue))])
annotate!([(0.340472,-0.27, (L"y = 0.34047", 6, :blue))])
annotate!([(0.63,-0.01, (L"|", 6, :blue))])
annotate!([(0.627,-0.27, (L"y = 0.627", 6, :blue))])
annotate!([(1,-0.01, (L"|", 6, :blue))])
annotate!([(1,-0.27, (L"y = 1", 6, :blue))])

# Annotate the area
annotate!([(0.42,1.15, ("Left area =", 6, :black))])
annotate!([(0.55,1.15, (r1, 6, :black))])
annotate!([(0.73,0.55, ("Right area = ", 6, :black))])
annotate!([(0.83,0.55, (r2, 6, :black))])

```

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [December 23, 2022, 8:23pm UTC](https://discourse.julialang.org/t/horizontal-slicing-in-integral/92043/10 "2022-12-23T20:23:34Z")

</div>

You can use SymPy a bit more easily here by integrating from `d` to `1/1^2` (the top half), so there is no need to adjust for the bottom rectangle, as the median is above 1/6^2:

```julia
julia> a, b = 1, 6
A =(1, 6)

julia> @syms x
(x,)

julia> A = integrate(1/x^2, (x, a, b))
5/6

julia> @syms c
(c,)

julia> solve(integrate(1/x^2, (x, a, c)) ~ A/2, c)
1-element Vector{Sym}:
 12/7

julia> @syms d
(d,)

julia> # y = 1/x^2 -> x = 1/sqrt(y)

julia> @syms y
(y,)

julia> solve(integrate(1/sqrt(y), (y, d, 1)) ~ A/2)
1-element Vector{Sym}:
 361/576

```

---

<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:** [December 24, 2022, 8:02am UTC](https://discourse.julialang.org/t/horizontal-slicing-in-integral/92043/11 "2022-12-24T08:02:39Z")

</div>

Thanks a lot @j_verzani

how a newbie will know that `d` is above 1/36 (1/6^2) ? is it intuition?
