Numerical integration: Difference between revisions

m
(Ouch. Most examples were wrong. Testing specs are probably needed. If someone could covert those to mathML, I'd be appreciative.)
m (→‎{{header|PureBasic}}: Update & fix)
Line 1,411:
<pre>105.333</pre>
=={{header|PureBasic}}==
{{incorrect|PureBasic|rightRect stops one h too soon; midRect does not sample midpoints but reimplements trapezium differently}}
 
<lang PureBasic>ProcedurePrototype.d leftRectTestFunction(a.d, b.d, nArg.d)
{{trans|Ada}}
 
<lang PureBasic>Procedure.d leftRect(a.d, b.d, n.d)
Procedure.d LeftIntegral(Start, Stop, Steps, *func.TestFunction)
Protected.d hn=(Stop-Start)/Steps, sum, x=Start
hWhile x <= (b Stop- a) / n
sum + hn * (f*func(x))
x = a
While x <= b -+ hn
Wend
sum + h * (f(x))
x + h
Wend
ProcedureReturn sum
EndProcedure
 
Procedure.d rightRectRightItegral(a.dStart, b.dStop, Steps, n*func.dTestFunction)
Protected.d hn=(Stop-Start)/Steps, sum, x=Start
While x+n <= b - hStop
h = (bx - a) /+ n
x = asum + hn * *func(x)
While x <= b - h
sum + h * (f(x))
x + h
Wend
ProcedureReturn sum
EndProcedure
 
Procedure.d midRectTrapezium(a.dStart, b.dStop, Steps, n*func.dTestFunction)
Protected.d hn=(Stop-Start)/Steps, sum, x=Start
While x <= b - hStop
h = (bsum -+ an * (*func(x) /+ *func(x+n))/2
x = ax+n
While x <= b - h
sum + (h / 2) * (f(x) + f(x + h))
x + h
Wend
ProcedureReturn sum
EndProcedure
 
Procedure.d trapeziumSimpson(a.dStart, b.dStop, Steps, n*func.dTestFunction)
Protected.d hn=(Stop-Start)/Steps, sumsum1, sum2, x=Start
Protected i
For i = 0 To n steps- 1
sum1 + f*func(a Start+ h n* i + h n/ 2)
h = (b - a) / n
sum = f(a) + f(b)
For i = 1 To n - 1
sum + 2 * f((a + i * h))
Next
For i = 1 To n Steps- 1
sum2 + f*func(a Start+ h n* i)
ProcedureReturn h / 2 * sum
Next
ProcedureReturn h / 6n * (f*func(aStart) + f*func(bStop) + 4 * sum1 + 2 * sum2) / 6
EndProcedure
Procedure.d simpson(a.d, b.d, n.d)
Protected.d h, sum1, sum2
Protected i
h = (b - a) / n
 
Procedure.d simpsonTest1(a.d, b.d, n.d)
For i = 0 To n - 1
ProcedureReturn n*n*n
sum1 + f(a + h * i + h / 2)
EndProcedure</lang>
Next
 
Procedure.d test2(n.d)
For i = 1 To n - 1
ProcedureReturn h 1/n 2 * sum
sum2 + f(a + h * i)
EndProcedure
Next
 
; = 0.25
Debug LeftIntegral(0,1,100,@Test1())
Debug RightItegral(0,1,100,@Test1())
Debug Trapezium (0,1,100,@Test1())
Debug Simpson (0,1,100,@Test1())
 
; = ~4.60517...
Debug LeftIntegral(1,100,1000,@Test2())
Debug RightItegral(1,100,1000,@Test2())
Debug Trapezium (1,100,1000,@Test2())
Debug Simpson (1,100,1000,@Test2())</lang>
<pre>0.2353220100000005
0.24502500000000052
0.25002500000000044
0.25000000000000006
 
4.6540000764434195
ProcedureReturn h / 6 * (f(a) + f(b) + 4 * sum1 + 2 * sum2)
4.5559910575146834
EndProcedure</lang>
4.6059860575146745
4.6051703849571446</pre>
 
=={{header|Python}}==
Anonymous user