Calculate Area of Function with Circumscribed Polygon with CalculusWithJulia

Hi all,

I try to calculate this function

$3x^{2} + x + 1$

But, I get the result different from the solution:

Capture d’écran_2022-11-27_16-11-35

This is the code I use:

using CalculusWithJulia

function riemann(f, a, b, n; method="right")
  xs = a:(b-a)/n:b
  deltas = diff(xs)      # forms x2-x1, x3-x2, ..., xn-xn-1
  if method == "left"
    cs = xs[1:end-1]
  elseif method == "right"
    cs = xs[2:end]
  else
    cs = [(xs[i] + xs[i+1])/2 for i in 1:length(deltas)]
  end
  sum(f(cs[i]) * deltas[i] for i in 1:length(deltas))
end

# intervals
a, b = -1,1
# the function
f(x) = 3x^2 + x + 1
#number of partitions
n = 10

# print the result
println("The area for $f(x):")
riemann(f, -1, 1, 10)   # Same result

the result from the code is: 4.24

different from the solution 4.656. Maybe the solution is wrong?

The solution is different. The left rectangle is a left sum, the right one a right sum (heights taken from f(-1) and f(1)). Try this to match:

riemann(f, -1, 0, 5; method="left") + riemann(f, 0, 1, 5; method="right")
1 Like

Thanks @j_verzani , you are really a Mathematics expert!

The CalculusWithJulia is amazing!