Numerical integration: Difference between revisions
m
Using lang tags now.
No edit summary |
m (Using lang tags now.) |
||
Line 18:
This solution creates a generic package into which the function F(X) is passed during generic instantiation. The first part is the package specification. The second part is the package body.
with function F(X : Long_Float) return Long_Float;
package Integrate is
Line 113:
end Simpson;
end Integrate;</lang>
=={{header|ALGOL 68}}==
###############
Line 199:
h / 6 * (f(a) + f(b) + 4 * sum1 + 2 * sum2)
END # simpson #;
SKIP</lang>
=={{header|BASIC}}==
Line 205:
{{trans|Java}}
h = (b - a) / n
sum = 0
Line 255:
simpson = h / 6 * (f(a) + f(b) + 4 * sum1 + 2 * sum2)
END FUNCTION</lang>
=={{header|C}}==
Line 391:
Due to their similarity, it makes sense to make the integration method a policy.
template<typename Method, typename F, typename Float>
double integrate(F f, Float a, Float b, int steps, Method m)
Line 453:
double rr = integrate(f, 0.0, 1.0, 10, rectangular(rectangular::right));
double t = integrate(f, 0.0, 1.0, 10, trapezium());
double s = integrate(f, 0.0, 1.0, 10, simpson());</lang>
=={{header|D}}==
The function f(x) is Func1/Func2 in this program.
<
import std.stdio ;
import std.math ;
Line 574:
writefln("%12.9f (%3dstep).",
Sigma(&Func2, 512, -1.0L, 2.0L, Sigma.Method.SIMP),512) ;
}</
Parts of the output:
<pre>Function(x) = 2 / (1 + 4x^2)
Line 586:
=={{header|Forth}}==
defer method ( fn F: x -- fn[x] )
Line 626:
test mid fn2 \ 2.496091
test trap fn2 \ 2.351014
test simpson fn2 \ 2.447732</lang>
=={{header|Fortran}}==
In ISO Fortran 95 and later if function f() is not already defined to be "elemental", define an elemental wrapper function around it to allow for array-based initialization:
<lang fortran> elemental function elemf(x)
real :: elemf, x
elemf = f(x)
end function elemf</lang>
Use Array Initializers, Pointers, Array invocation of Elemental functions, Elemental array-array and array-scalar arithmetic, and the SUM intrinsic function
<lang fortran> program integrate
integer, parameter :: n = 20 ! or whatever
real, parameter :: a = 0.0, b = 15.0 ! or whatever
Line 664:
! Simpson's rule integral
simpson = h / 6 * sum(fleft + fright + 4*fmid)
end program integrate</lang>
=={{header|Haskell}}==
Line 670:
Different approach from most of the other examples: First, the function ''f'' might be expensive to calculate, and so it should not be evaluated several times. So, ideally, we want to have positions ''x'' and weights ''w'' for each method and then just calculate the approximation of the integral by
Second, let's to generalize all integration methods into one scheme. The methods can all be characterized by the coefficients ''vs'' they use in a particular interval. These will be fractions, and for terseness, we extract the denominator as an extra argument ''v''.
Line 676:
Now there are the closed formulas (which include the endpoints) and the open formulas (which exclude them). Let's do the open formulas first, because then the coefficients don't overlap:
integrateOpen v vs f a b n = approx f xs ws * h / v where
m = fromIntegral (length vs) * n
Line 682:
ws = concat $ replicate n vs
c = a + h/2
xs = [c + h * fromIntegral i | i <- [0..m-1]]</lang>
Similarly for the closed formulas, but we need an additional function ''overlap'' which sums the coefficients overlapping at the interior interval boundaries:
integrateClosed v vs f a b n = approx f xs ws * h / v where
m = fromIntegral (length vs - 1) * n
Line 699:
inter n [] = x : inter (n-1) xs
inter n [y] = (x+y) : inter (n-1) xs
inter n (y:ys) = y : inter n ys</lang>>
And now we can just define
intRightRect = integrateClosed 1 [0,1]
intMidRect = integrateOpen 1 [1]
intTrapezium = integrateClosed 2 [1,1]
intSimpson = integrateClosed 3 [1,4,1] </lang>
or, as easily, some additional schemes:
intOpen1 = integrateOpen 2 [3,3]
intOpen2 = integrateOpen 3 [8,-4,8]</lang>
Some examples:
Line 730:
=={{header|Java}}==
The function in this example is assumed to be <tt>f(double x)</tt>.
public static double leftRect(double a, double b, double n){
double h = (b - a) / n;
Line 777:
}
//assume f(double x) elsewhere in the class
}>/lang>
=={{header|Logo}}==
output invoke :fn :x
end
Line 815:
print integrate "i.mid "fn2 4 -1 2 ; 2.496091
print integrate "i.trapezium "fn2 4 -1 2 ; 2.351014
print integrate "i.simpsons "fn2 4 -1 2 ; 2.447732</lang>
=={{header|OCaml}}==
let h = (b -. a) /. float_of_int steps in
let rec helper i s =
Line 848:
let rr = integrate square 0. 1. 10 right_rect
let t = integrate square 0. 1. 10 trapezium
let s = integrate square 0. 1. 10 simpson</lang>
=={{header|Pascal}}==
Line 933:
=={{header|Scheme}}==
(define h (/ (- b a) steps))
(* h
Line 953:
(define rr (integrate square 0 1 10 right-rect))
(define t (integrate square 0 1 10 trapezium))
(define s (integrate square 0 1 10 simpson))</lang>
|