# Calculate Area of Function with Circumscribed Polygon with CalculusWithJulia

**URL:** <https://discourse.julialang.org/t/calculate-area-of-function-with-circumscribed-polygon-with-calculuswithjulia/90895>\
**Category:** General Usage\
**Tags:** package\
**Created:** [November 27, 2022, 9:13am UTC](https://discourse.julialang.org/t/calculate-area-of-function-with-circumscribed-polygon-with-calculuswithjulia/90895 "2022-11-27T09:13:02Z")\
**Posts on this page:** 3\
**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:** [November 27, 2022, 9:13am UTC](https://discourse.julialang.org/t/calculate-area-of-function-with-circumscribed-polygon-with-calculuswithjulia/90895/1 "2022-11-27T09:13:02Z")

</div>

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](https://global.discourse-cdn.com/julialang/original/3X/8/4/84004426b036ec1a8dfdb5733f66744228fa274c.png)

This is the code I use:

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

---

<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:** [November 27, 2022, 12:48pm UTC](https://discourse.julialang.org/t/calculate-area-of-function-with-circumscribed-polygon-with-calculuswithjulia/90895/2 "2022-11-27T12:48:27Z")

</div>

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:

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

```

---

<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:** [November 27, 2022, 4:08pm UTC](https://discourse.julialang.org/t/calculate-area-of-function-with-circumscribed-polygon-with-calculuswithjulia/90895/3 "2022-11-27T16:08:36Z")

</div>

Thanks @j_verzani , you are really a Mathematics expert!

The CalculusWithJulia is amazing!
