Numerical integration: Difference between revisions
Content added Content deleted
(→{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details) |
(+Stata) |
||
Line 4,415: | Line 4,415: | ||
val t = integrate (square, 0.0, 1.0, 10, trapezium ) |
val t = integrate (square, 0.0, 1.0, 10, trapezium ) |
||
val s = integrate (square, 0.0, 1.0, 10, simpson )</lang> |
val s = integrate (square, 0.0, 1.0, 10, simpson )</lang> |
||
=={{header|Stata}}== |
|||
<lang>mata |
|||
function integrate(f,a,b,n,u,v) { |
|||
s = 0 |
|||
h = (b-a)/n |
|||
m = length(u) |
|||
for (i=0; i<n; i++) { |
|||
x = a+i*h |
|||
for (j=1; j<=m; j++) s = s+v[j]*(*f)(x+h*u[j]) |
|||
} |
|||
return(s*h) |
|||
} |
|||
function log_(x) { |
|||
return(log(x)) |
|||
} |
|||
function id(x) { |
|||
return(x) |
|||
} |
|||
function cube(x) { |
|||
return(x*x*x) |
|||
} |
|||
function inv(x) { |
|||
return(1/x) |
|||
} |
|||
function test(f,a,b,n) { |
|||
return(integrate(f,a,b,n,(0,1),(1,0)), |
|||
integrate(f,a,b,n,(0,1),(0,1)), |
|||
integrate(f,a,b,n,(0.5),(1)), |
|||
integrate(f,a,b,n,(0,1),(0.5,0.5)), |
|||
integrate(f,a,b,n,(0,1/2,1),(1/6,4/6,1/6))) |
|||
} |
|||
test(&cube(),0,1,100) |
|||
test(&inv(),1,100,1000) |
|||
test(&id(),0,5000,5000000) |
|||
test(&id(),0,6000,6000000) |
|||
end</lang> |
|||
'''Output''' |
|||
<pre> 1 2 3 4 5 |
|||
+--------------------------------------------------------+ |
|||
1 | .245025 .255025 .2499875 .250025 .25 | |
|||
+--------------------------------------------------------+ |
|||
1 2 3 4 5 |
|||
+-----------------------------------------------------------------------+ |
|||
1 | 4.654991058 4.556981058 4.604762549 4.605986058 4.605170385 | |
|||
+-----------------------------------------------------------------------+ |
|||
1 2 3 4 5 |
|||
+------------------------------------------------------------------+ |
|||
1 | 12499997.5 12500002.5 12500000 12500000 12500000 | |
|||
+------------------------------------------------------------------+ |
|||
1 2 3 4 5 |
|||
+--------------------------------------------------------+ |
|||
1 | 17999997 18000003 18000000 18000000 18000000 | |
|||
+--------------------------------------------------------+</pre> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Line 4,483: | Line 4,548: | ||
trapezium 1.9835235375094546 (-0.8%) |
trapezium 1.9835235375094546 (-0.8%) |
||
simpson 2.0000067844418012 (0.0%)</pre> |
simpson 2.0000067844418012 (0.0%)</pre> |
||
=={{header|TI-89 BASIC}}== |
=={{header|TI-89 BASIC}}== |