How do you make an integral (∫) block in Snap!

So I am trying to make a factorial block using the gamma function which uses an integral, and that is where I am right now.

I tried mapping a really large list of numbers from A to B to the given function of #1, but it’s not very efficient nor is it accurate enough.

Images:


1: try compiling your map blocks. (right click->compile) this makes them faster
2: read this: I got the wrong answer to a computation involving decimals!
3: use
untitled script pic(1)
for factorial, VERY fast, and if the block keeps reporting Infinity for large numbers, use this block before you use the factorial block:
untitled script pic

drag these blocks into snap to use them. (although if snap says you need to download the blocks because of a cross origin proxy, you may have to download them and then drag the file in)
4: you don't need to use input names in your block as the function, just leave the slot you would use the input names in empty, and make sure to click on the left arrowhead to get rid of the input names in the ring.

Hope this helps!

If the function is “well-behaved” (though I’m not sure about the exact conditions) Simpson’s rule - there’s actually several versions, the simplest “1/3” rule is usually good enough - will approximate a definite integral pretty well, very fast.

See this post.

If (like in the OP’s example) the function is quadratic, the rule will even generate a perfect solution, with any even number of calculation intervals (my code will round to the next higher even number, if necessary):

Simpsons rule script pic 2

BTW with a higher number of intervals. minor rounding errors may occur (in this particular case; whereas in general the approximation tends to improve with the number of intervals). However, when integration is used for real-world (physical) purposes, small errors don’t matter much. If you require an exact result, better apply mathematical analysis on the original function’s formula.

Additional remark
If you(@scratchyg6) wouldn’t want to use Simpson’s rule, but rather something simpler, either the weight of both the first and last term would need to be halved (and the divider lowered by 1 - trapezoidal rule; it’s supported by the SciSnap! library), OR each interval would need to be represented by its middle value (basic summation). This actually will remove most of your calculation error; applying Simpson’s rule will make your calculation even more accurate and/or faster.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.