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

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:

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

``````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))
``````
1 Like

Is this a homework problem?

(If not, what you are doing is the trapezoidal rule, and there is plenty of Julia code for this and more sophisticated methods of numerical integration.)

1 Like

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:

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

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

Anonymous function input required:
`Area(x -> x^2, 0, 1, 50_000)`

2 Likes

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

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

In the second example, it was not.

1 Like