Numerical integration: Difference between revisions

Content added Content deleted
(adding lambdatalk)
Line 2,946: Line 2,946:
Simpson = 18000000.000000
Simpson = 18000000.000000
</pre>
</pre>

=={{header|Lambdatalk}}==
Following Python's presentation

<lang Scheme>
1) FUNCTIONS

{def left_rect {lambda {:f :x :h} {:f :x}}}
-> left_rect

{def mid_rect {lambda {:f :x :h} {:f {+ :x {/ :h 2}}}}}
-> mid_rect

{def right_rect {lambda {:f :x :h} {:f {+ :x :h}}}}
-> right_rect

{def trapezium {lambda {:f :x :h} {/ {+ {:f :x} {:f {+ :x :h}}} 2}}}
-> trapezium

{def simpson
{lambda {:f :x :h}
{/ {+ {:f :x} {* 4 {:f {+ :x {/ :h 2}}}} {:f {+ :x :h}}} 6}}}
-> simpson

{def cube {lambda {:x} {* :x :x :x}}}
-> cube

{def reciprocal {lambda {:x} {/ 1 :x}}}
-> reciprocal

{def identity {lambda {:x} :x}}
-> identity
{def integrate
{lambda {:f :a :b :steps :meth}
{let { {:f :f} {:a :a} {:steps :steps} {:meth :meth}
{:h {/ {- :b :a} :steps}}
} {* :h {+ {S.map {{lambda {:meth :f :a :h :i}
{:meth :f {+ :a {* :i :h}} :h}
} :meth :f :a :h}
{S.serie 1 :steps}} }}}}}
-> integrate

{def methods left_rect mid_rect right_rect trapezium simpson}
-> methods

2) TESTS

We apply the following template

{b ∫*function* from *a* to *b* steps *steps*}
{table
{tr {td exact value:} {td *value*}} // the awaited value
{S.map {lambda {:m}
{tr {td :m}
{td {integrate *function* *a* *b* *steps* :m}} }}
{methods}} }

to the given *functions* from *a* to *b* with *steps*
and we get:

∫x3 from 0 to 100 steps 100 (computed in 13ms)
exact value: 0.25 // 1/4
left_rect 0.25502500000000006
mid_rect 0.26013825000000007
right_rect 0.26532800000000006
trapezium 0.2601765
simpson 0.260151

∫1/x from 1 to 100 steps 1000 (computed in 94ms)
exact value: 4.605170185988092 // log(100)
left_rect 4.55698105751468
mid_rect 4.511421425235764
right_rect 4.467888185754358
trapezium 4.512434621634517
simpson 4.511759157368674

∫x from 0 to 5000 steps 5000000 (computed in ... 560000m)
exact value: 12500000 // 5000*5000/2
left_rect 12500002.5
mid_rect 12500005
right_rect 12500007.5
trapezium 12500005
simpson 12500005

∫x from 0 to 6000 steps 6000 (computed in 420ms) too impatient for 6000000, sorry
exact value: 18000000 // 6000*6000/2
left_rect 18003000
mid_rect 18006000
right_rect 18009000
trapezium 18006000
simpson 18006000
</lang>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==