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))
(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.)
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)