# Simpsons Rule

**URL:** <https://discourse.julialang.org/t/simpsons-rule/84114>\
**Category:** New to Julia\
**Tags:** question, integral, algorithm\
**Created:** [July 12, 2022, 3:43pm UTC](https://discourse.julialang.org/t/simpsons-rule/84114 "2022-07-12T15:43:42Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)\
**Post date:** [July 13, 2022, 11:42pm UTC](https://discourse.julialang.org/t/simpsons-rule/84114/22 "2022-07-13T23:42:58Z")

</div>

I have not followed the logic, but would it be feasible to extend to a cumulative Simpsons rule for evenly spaced data of any length?

---

<div class="post-metadata">

**Author:** ![simonluzara](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonluzara/32/205260_2.png) [@simonluzara](https://discourse.julialang.org/u/simonluzara)\
**Post date:** [July 14, 2022, 12:41am UTC](https://discourse.julialang.org/t/simpsons-rule/84114/23 "2022-07-14T00:41:05Z")

</div>

Some modifications can be done to the code I suggested, cumulating the sum at a certain point might be done using a loop.  
I will dig into it the next days.

---

<div class="post-metadata">

**Author:** ![abelokoj](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@abelokoj](https://discourse.julialang.org/u/abelokoj)\
**Post date:** [September 3, 2022, 7:03pm UTC](https://discourse.julialang.org/t/simpsons-rule/84114/24 "2022-09-03T19:03:31Z")

</div>

There is a slight problem with your code. The error comes from the area where you make use of Simpson’s 3/8 rule. Kindly check your code

You an test with the following

```julia
f1(x) = x^2;
f2(x) = x^3;
a = 0; b = 1;
# test1
n = 10 ;
simps(f1, a, b, n)
simps(f2, a, b, n)
# result
0.3333333333333333
0.25  

# test2
n = 11 ;
simps(f1, a, b, n)
simps(f2, a, b, n)
# result
0.25569747057350367
0.17828927896546232

```

---

<div class="post-metadata">

**Author:** ![simonluzara](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonluzara/32/205260_2.png) [@simonluzara](https://discourse.julialang.org/u/simonluzara)\
**Post date:** [September 5, 2022, 1:25pm UTC](https://discourse.julialang.org/t/simpsons-rule/84114/25 "2022-09-05T13:25:34Z")

</div>

As said above, if you have a function to calculate, Simpson’s suggest you pick even number of slices.  
If you already have a vector with uneven number of components, then the Simpson’s 3/8th rule could be employed.  
That to be said, there is a room to modify the code with any choice of slice numbers. I will put it through.

[Previous page](https://discourse.julialang.org/t/simpsons-rule/84114.md?page=1)
