## DEV Community

Maria Ines Montenegro

Posted on • Updated on

# Trapezoidal Rule in F#

Numerical is approximate; analytical is exact.

A numerical method is a tool used to find a numerical solution to a problem which can be difficult to solve analytically.

Mostly done by computers, this approach is a complete set of instructions that are executed in order to find a solution. With this being said,
the Trapezoidal Rule is a numerical method used to find definite integrals. The most common application for this method is to find the
area under a function in a given set of intervals. Finding this area is typically done using Riemann sums, but this method proves to be more exact because it uses trapezoids instead of rectangles. • Reimann sum • Trapezoidal Rule

In this case, I implemented this numerical method using F# which is a functional programming language. First, I chose the function to integrate; in this case sin(x):

``````let f x = System.Math.Sin(x)
``````

The algorithm has to receive the intervals (a,b) through which it will find the area and a number (N) of trapezoids to form.
With these parameters, it can now calculate the length of each trapezoid:

``````let h = (b - a)/N
`````` I created two functions in order to calculate the formula above:

``````let rec sum_recursive (xi:float) (h:float) f (N:float) (iter:float) =
if iter = N then
xi
else
let xi_1 = xi + h*f(h*iter)
sum_recursive xi_1 h f N (iter+1.0)

let trapezoidal (a:float) (b:float) f (N:float) =
let h = (b - a)/N
let left_side = h/2.0*(f(a)+f(b))
left_side + sum_recursive 0.0 h f N 0.0

``````

This method can be used for things like: finding how much fluid is flowing through a specific area, calculating the work needed to move an object at a certain speed, or even finding the center of mass of an object.

Overall, learning to program a numerical method has enabled me to relate mathematical concepts, that are normally just seen in class; with programming, which is something I actually use in my day to day life. Furthermore, this was also my first time working with a functional programming language such as F#; which I ended up enjoying.

Here's my complete code
Hope you find it useful!

### References Great job but I'm not sure why using functional-first language to use it OO-style. To me using mutable in F# is a flag that something is done in a non-functional fashion.
How about considering the code below?

``````let trapezoidal2 a b N =
let h = (b - a)/N
let initialAccValue = h/2.0*(f(a)+f(b))
[|1 .. System.Convert.ToInt32(N)|]
|> Array.mapi(fun i x -> i+1,x)
|> Array.fold (fun acc (i,_) -> acc + h*f(float(i)*h+a)) initialAccValue
``````

The key tricks here are `Array.mapi` which allows us to work with tuple (element,index) instead of just an element; and substitution of iterative increment of xi by h to multiplication `index*h+a` Maria Ines Montenegro • Edited

Since this was my first time with a functional programming language I think I hadn't fully grasped the concepts. I ended up using recursion in order to remove the mutable variables; which I updated in my post. Thank you very much for your feedback!
{"ft_lang_label":"__label__en","ft_lang_prob":0.9044615,"math_prob":0.89651316,"size":3112,"snap":"2023-40-2023-50","text_gpt3_token_len":749,"char_repetition_ratio":0.10875161,"word_repetition_ratio":0.011235955,"special_character_ratio":0.22397172,"punctuation_ratio":0.104166664,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9989095,"pos_list":[0,1,2,3,4,5,6,7,8,9,10],"im_url_duplicate_count":[null,2,null,2,null,2,null,4,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-07T14:30:55Z\",\"WARC-Record-ID\":\"<urn:uuid:64b1e970-db67-4fac-a647-92c8be1a122c>\",\"Content-Length\":\"127893\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3e0c3358-44d3-4a67-abe3-0f68dbad7897>\",\"WARC-Concurrent-To\":\"<urn:uuid:441244e5-65fe-4e7f-9e17-f97dbdcb6f8d>\",\"WARC-IP-Address\":\"151.101.130.217\",\"WARC-Target-URI\":\"https://dev.to/marinesm/trapezoidal-rule-in-f-253l\",\"WARC-Payload-Digest\":\"sha1:XDYYV7YCLVZPOWWS7EV6JL5SHFDHA42P\",\"WARC-Block-Digest\":\"sha1:MTII6E6IBZ2IAIDVC3MTYFQI4E3P3OCE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100674.56_warc_CC-MAIN-20231207121942-20231207151942-00352.warc.gz\"}"}