# Calculate the area under the curve based on calculating the area of single polygon

**URL:** <https://discourse.julialang.org/t/calculate-the-area-under-the-curve-based-on-calculating-the-area-of-single-polygon/69330>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 6, 2021, 7:43pm UTC](https://discourse.julialang.org/t/calculate-the-area-under-the-curve-based-on-calculating-the-area-of-single-polygon/69330 "2021-10-06T19:43:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![IsraelSI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/israelsi/32/29505_2.png) [@IsraelSI](https://discourse.julialang.org/u/IsraelSI)\
**Post date:** [October 6, 2021, 7:43pm UTC](https://discourse.julialang.org/t/calculate-the-area-under-the-curve-based-on-calculating-the-area-of-single-polygon/69330/1 "2021-10-06T19:43:53Z")

</div>

Hi, sorry I’m new to julia and I’m stuck trying to make based on the function that calculates the area of a polygon to calculate the area under the curve of a strictly non-negative function on a given interval [𝑎, 𝑏]. For this, obtain 𝑛 + 1 equidistant points in that interval ([𝑎, 𝑎 + (𝑏 − 𝑎) / 𝑛,…, 𝑏]) and with them obtain the coordinates of the vertices of the polygon that approximates the area under the curve, [𝑎, 0], [𝑎, 𝑓 (𝑎)], [𝑎 + (𝑏 − 𝑎) / 𝑛, 𝑓 (𝑎 + (𝑏 − 𝑎) / 𝑛)],…, [𝑏, 𝑓 (𝑏)] , [𝑏, 0] and finally calculates its area.  
In this way:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/c/4c226105347dd3b8aa2fd74bd98ffa6b759ba544.png)

Something like in the image, The red points are the subdivision of the inverval and the vertices of the polygon, the polygon is formed by the green lines and the function by the black curve.  
I try with this, but i don´t know where is the error

```julia
area(f(x), 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))

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [October 6, 2021, 8:10pm UTC](https://discourse.julialang.org/t/calculate-the-area-under-the-curve-based-on-calculating-the-area-of-single-polygon/69330/2 "2021-10-06T20:10:30Z")

</div>

Here are some pointers  
[https://docs.julialang.org/en/v1/base/math/#Base.range](https://docs.julialang.org/en/v1/base/math/#Base.range)  
[https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting)  
[https://docs.julialang.org/en/v1/base/collections/#Base.sum](https://docs.julialang.org/en/v1/base/collections/#Base.sum)

---

<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:** [October 6, 2021, 10:03pm UTC](https://discourse.julialang.org/t/calculate-the-area-under-the-curve-based-on-calculating-the-area-of-single-polygon/69330/3 "2021-10-06T22:03:48Z")

</div>

> [@IsraelSI](#):
>
> trying to make based on the function that calculates the area of a polygon to calculate the area under the curve

Is this a homework problem?

(If not, what you are doing is the [trapezoidal rule](https://en.wikipedia.org/wiki/Trapezoidal_rule), and there is plenty of Julia code for this and more sophisticated methods of numerical integration.)

---

<div class="post-metadata">

**Author:** ![IsraelSI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/israelsi/32/29505_2.png) [@IsraelSI](https://discourse.julialang.org/u/IsraelSI)\
**Post date:** [October 6, 2021, 10:19pm UTC](https://discourse.julialang.org/t/calculate-the-area-under-the-curve-based-on-calculating-the-area-of-single-polygon/69330/4 "2021-10-06T22:19:41Z")

</div>

> [@stevengj](#):
>
> Is this a homework problem?
> 
> (If not, what you are doing is the [trapezoidal rule](https://en.wikipedia.org/wiki/Trapezoidal_rule), and there is plenty of Julia code for this and more sophisticated methods of numerical integration.)

Maybe I didn’t have to fiddle with the problem so much, it’s more about how to make my function use f (x), I mean this:

```julia
a, b = 0, 1
f(x) = x^2
n = 50_000
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))

```

This is how my code works, but when I try to do it as a function, I don’t know how to evaluate it, because if I do it like this:

```julia
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

Area( x^2, 0, 1, 50_000)

```

It gives me an error

---

<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:** [October 6, 2021, 10:24pm UTC](https://discourse.julialang.org/t/calculate-the-area-under-the-curve-based-on-calculating-the-area-of-single-polygon/69330/5 "2021-10-06T22:24:42Z")

</div>

[Anonymous function](https://docs.julialang.org/en/v1/manual/functions/#man-anonymous-functions) input required:  
`Area(x -> x^2, 0, 1, 50_000)`

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 6, 2021, 11:01pm UTC](https://discourse.julialang.org/t/calculate-the-area-under-the-curve-based-on-calculating-the-area-of-single-polygon/69330/6 "2021-10-06T23:01:13Z")

</div>

Well, since `f` has already been defined, one can just call

```julia
area(f, 0, 1, 50_000) # function names should be lowercase, btw 

```

---

<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:** [October 6, 2021, 11:12pm UTC](https://discourse.julialang.org/t/calculate-the-area-under-the-curve-based-on-calculating-the-area-of-single-polygon/69330/7 "2021-10-06T23:12:17Z")

</div>

> [@DNF](#):
>
> since `f` has already been defined

In the second example, it was not.
