September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,70 +1,89 @@
|
|||
approx f xs ws = sum [w * f x | (x,w) <- zip xs ws]
|
||||
approx
|
||||
:: Fractional a
|
||||
=> (a1 -> a) -> [a1] -> [a] -> a
|
||||
approx f xs ws =
|
||||
sum
|
||||
[ w * f x
|
||||
| (x, w) <- zip xs ws ]
|
||||
|
||||
integrateOpen :: Fractional a => a -> [a] -> (a -> a) -> a -> a -> Int -> a
|
||||
integrateOpen v vs f a b n = approx f xs ws * h / v where
|
||||
m = fromIntegral (length vs) * n
|
||||
h = (b-a) / fromIntegral m
|
||||
ws = concat $ replicate n vs
|
||||
c = a + h/2
|
||||
xs = [c + h * fromIntegral i | i <- [0..m-1]]
|
||||
integrateOpen
|
||||
:: Fractional a
|
||||
=> a -> [a] -> (a -> a) -> a -> a -> Int -> a
|
||||
integrateOpen v vs f a b n = approx f xs ws * h / v
|
||||
where
|
||||
m = fromIntegral (length vs) * n
|
||||
h = (b - a) / fromIntegral m
|
||||
ws = concat $ replicate n vs
|
||||
c = a + h / 2
|
||||
xs =
|
||||
[ c + h * fromIntegral i
|
||||
| i <- [0 .. m - 1] ]
|
||||
|
||||
integrateClosed :: Fractional a => a -> [a] -> (a -> a) -> a -> a -> Int -> a
|
||||
integrateClosed v vs f a b n = approx f xs ws * h / v where
|
||||
m = fromIntegral (length vs - 1) * n
|
||||
h = (b-a) / fromIntegral m
|
||||
ws = overlap n vs
|
||||
xs = [a + h * fromIntegral i | i <- [0..m]]
|
||||
integrateClosed
|
||||
:: Fractional a
|
||||
=> a -> [a] -> (a -> a) -> a -> a -> Int -> a
|
||||
integrateClosed v vs f a b n = approx f xs ws * h / v
|
||||
where
|
||||
m = fromIntegral (length vs - 1) * n
|
||||
h = (b - a) / fromIntegral m
|
||||
ws = overlap n vs
|
||||
xs =
|
||||
[ a + h * fromIntegral i
|
||||
| i <- [0 .. m] ]
|
||||
|
||||
overlap :: Num a => Int -> [a] -> [a]
|
||||
overlap n [] = []
|
||||
overlap n (x:xs) = x : inter n xs where
|
||||
inter 1 ys = ys
|
||||
inter n [] = x : inter (n-1) xs
|
||||
inter n [y] = (x+y) : inter (n-1) xs
|
||||
inter n (y:ys) = y : inter n ys
|
||||
|
||||
intLeftRect = integrateClosed 1 [1,0]
|
||||
intMidRect = integrateOpen 1 [1]
|
||||
intRightRect = integrateClosed 1 [0,1]
|
||||
intTrapezium = integrateClosed 2 [1,1]
|
||||
intSimpson = integrateClosed 3 [1,4,1]
|
||||
overlap
|
||||
:: Num a
|
||||
=> Int -> [a] -> [a]
|
||||
overlap n [] = []
|
||||
overlap n (x:xs) = x : inter n xs
|
||||
where
|
||||
inter 1 ys = ys
|
||||
inter n [] = x : inter (n - 1) xs
|
||||
inter n [y] = (x + y) : inter (n - 1) xs
|
||||
inter n (y:ys) = y : inter n ys
|
||||
|
||||
uncurry4 :: (t1 -> t2 -> t3 -> t4 -> t) -> (t1, t2, t3, t4) -> t
|
||||
uncurry4 f ~(a, b, c, d) = f a b c d
|
||||
|
||||
main = do
|
||||
let m1 = "rectangular left: "
|
||||
let m2 = "rectangular middle: "
|
||||
let m3 = "rectangular right: "
|
||||
let m4 = "trapezium: "
|
||||
let m5 = "simpson: "
|
||||
-- TEST ----------------------------------------------------------------------
|
||||
ms
|
||||
:: Fractional a
|
||||
=> [(String, (a -> a) -> a -> a -> Int -> a)]
|
||||
ms =
|
||||
[ ("rectangular left", integrateClosed 1 [1, 0])
|
||||
, ("rectangular middle", integrateOpen 1 [1])
|
||||
, ("rectangular right", integrateClosed 1 [0, 1])
|
||||
, ("trapezium", integrateClosed 2 [1, 1])
|
||||
, ("simpson", integrateClosed 3 [1, 4, 1])
|
||||
]
|
||||
|
||||
let arg1 = ((\x -> x ^ 3), 0, 1, 100)
|
||||
putStrLn $ m1 ++ (show $ uncurry4 intLeftRect arg1)
|
||||
putStrLn $ m2 ++ (show $ uncurry4 intMidRect arg1)
|
||||
putStrLn $ m3 ++ (show $ uncurry4 intRightRect arg1)
|
||||
putStrLn $ m4 ++ (show $ uncurry4 intTrapezium arg1)
|
||||
putStrLn $ m5 ++ (show $ uncurry4 intSimpson arg1)
|
||||
putStrLn ""
|
||||
integrations
|
||||
:: (Fractional a, Num t, Num t1, Num t2)
|
||||
=> [(String, (a -> a, t, t1, t2))]
|
||||
integrations =
|
||||
[ ("x^3", ((^ 3), 0, 1, 100))
|
||||
, ("1/x", ((1 /), 1, 100, 1000))
|
||||
, ("x", (id, 0, 5000, 500000))
|
||||
, ("x", (id, 0, 6000, 600000))
|
||||
]
|
||||
|
||||
let arg2 = ((\x -> 1 / x), 1, 100, 1000)
|
||||
putStrLn $ m1 ++ (show $ uncurry4 intLeftRect arg2)
|
||||
putStrLn $ m2 ++ (show $ uncurry4 intMidRect arg2)
|
||||
putStrLn $ m3 ++ (show $ uncurry4 intRightRect arg2)
|
||||
putStrLn $ m4 ++ (show $ uncurry4 intTrapezium arg2)
|
||||
putStrLn $ m5 ++ (show $ uncurry4 intSimpson arg2)
|
||||
putStrLn ""
|
||||
|
||||
let arg3 = ((\x -> x), 0, 5000, 5000000)
|
||||
putStrLn $ m1 ++ (show $ uncurry4 intLeftRect arg3)
|
||||
putStrLn $ m2 ++ (show $ uncurry4 intMidRect arg3)
|
||||
putStrLn $ m3 ++ (show $ uncurry4 intRightRect arg3)
|
||||
putStrLn $ m4 ++ (show $ uncurry4 intTrapezium arg3)
|
||||
putStrLn $ m5 ++ (show $ uncurry4 intSimpson arg3)
|
||||
putStrLn ""
|
||||
|
||||
let arg4 = ((\x -> x), 0, 6000, 6000000)
|
||||
putStrLn $ m1 ++ (show $ uncurry4 intLeftRect arg4)
|
||||
putStrLn $ m2 ++ (show $ uncurry4 intMidRect arg4)
|
||||
putStrLn $ m3 ++ (show $ uncurry4 intRightRect arg4)
|
||||
putStrLn $ m4 ++ (show $ uncurry4 intTrapezium arg4)
|
||||
putStrLn $ m5 ++ (show $ uncurry4 intSimpson arg4)
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_
|
||||
(\(s, e@(_, a, b, n)) -> do
|
||||
putStrLn
|
||||
(concat
|
||||
[ indent 20 ("f(x) = " ++ s)
|
||||
, show [a, b]
|
||||
, " ("
|
||||
, show n
|
||||
, " approximations)"
|
||||
])
|
||||
mapM_
|
||||
(\(s, integration) ->
|
||||
putStrLn (indent 20 (s ++ ":") ++ show (uncurry4 integration e)))
|
||||
ms
|
||||
putStrLn [])
|
||||
integrations
|
||||
where
|
||||
indent n = take n . (++ replicate n ' ')
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
// version 1.1.2
|
||||
|
||||
typealias Func = (Double) -> Double
|
||||
|
||||
fun integrate(a: Double, b: Double, n: Int, f: Func) {
|
||||
val h = (b - a) / n
|
||||
val sum = DoubleArray(5)
|
||||
for (i in 0 until n) {
|
||||
val x = a + i * h
|
||||
sum[0] += f(x)
|
||||
sum[1] += f(x + h / 2.0)
|
||||
sum[2] += f(x + h)
|
||||
sum[3] += (f(x) + f(x + h)) / 2.0
|
||||
sum[4] += (f(x) + 4.0 * f(x + h / 2.0) + f(x + h)) / 6.0
|
||||
}
|
||||
val methods = listOf("LeftRect ", "MidRect ", "RightRect", "Trapezium", "Simpson ")
|
||||
for (i in 0..4) println("${methods[i]} = ${"%f".format(sum[i] * h)}")
|
||||
println()
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
integrate(0.0, 1.0, 100) { it * it * it }
|
||||
integrate(1.0, 100.0, 1_000) { 1.0 / it }
|
||||
integrate(0.0, 5000.0, 5_000_000) { it }
|
||||
integrate(0.0, 6000.0, 6_000_000) { it }
|
||||
}
|
||||
24
Task/Numerical-integration/Zkl/numerical-integration.zkl
Normal file
24
Task/Numerical-integration/Zkl/numerical-integration.zkl
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
fcn integrate(F,f,a,b,steps){
|
||||
h:=(b - a) / steps;
|
||||
h*(0).reduce(steps,'wrap(s,i){ F(f, h*i + a, h) + s },0.0);
|
||||
}
|
||||
|
||||
fcn rectangularLeft(f,x) { f(x) }
|
||||
fcn rectangularMiddle(f,x,h){ f(x+h/2) }
|
||||
fcn rectangularRight(f,x,h) { f(x+h) }
|
||||
fcn trapezium(f,x,h) { (f(x) + f(x+h))/2 }
|
||||
fcn simpson(f,x,h) { (f(x) + 4.0*f(x+h/2) + f(x+h))/6 }
|
||||
|
||||
args:=T( T(fcn(x){ x.pow(3) }, 0.0, 1.0, 10),
|
||||
T(fcn(x){ 1.0 / x }, 1.0, 100.0, 1000),
|
||||
T(fcn(x){ x }, 0.0, 5000.0, 0d5_000_000),
|
||||
T(fcn(x){ x }, 0.0, 6000.0, 0d6_000_000) );
|
||||
fs:=T(rectangularLeft,rectangularMiddle,rectangularRight,
|
||||
trapezium,simpson);
|
||||
names:=fs.pump(List,"name",'+(":"),"%-18s".fmt);
|
||||
|
||||
foreach a in (args){
|
||||
names.zipWith('wrap(nm,f){
|
||||
"%s %f".fmt(nm,integrate(f,a.xplode())).println() }, fs);
|
||||
println();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue