Skip to content

Instantly share code, notes, and snippets.

@lovely-error
Last active February 21, 2024 07:21
Show Gist options
  • Save lovely-error/dead5ee4881035ede3c4636729aaa77e to your computer and use it in GitHub Desktop.
Save lovely-error/dead5ee4881035ede3c4636729aaa77e to your computer and use it in GitHub Desktop.
Computing definite integral over sinus in range [0;pi] (with terrible precision :3 ~2.0097% deviation from analytic solution)
-- basically this in wolfram alpha terms
-- 2*(Integrate[sin\(40)x\(41),{x,0,Divide[pi,4]}]+Integrate[sin\(40)x\(41),{x,Divide[pi,4],Divide[pi,2]}])
-- https://www.desmos.com/calculator/1fokxfyphz
-- https://www.wolframalpha.com/input?i2d=true&i=Integrate%5Bsin%5C%2840%29x%5C%2841%29%2C%7Bx%2C0%2Cpi%7D%5D
-- https://www.wolframalpha.com/input?i2d=true&i=2*%5C%2840%29Integrate%5Bsin%5C%2840%29x%5C%2841%29%2C%7Bx%2C0%2CDivide%5Bpi%2C4%5D%7D%5D%2BIntegrate%5Bsin%5C%2840%29x%5C%2841%29%2C%7Bx%2CDivide%5Bpi%2C4%5D%2CDivide%5Bpi%2C2%5D%7D%5D%5C%2841%29
import Text.Printf (printf)
main = putStrLn $ res
integRes =
-- left semi could be better honestly , but idgaf cus rate of change is minimal there
let leftSemiSegment = ((pi/4) * (sin $ pi/4)) / 2
rightSemiSegment = foldr (+) 0 $ take 16 (areaSubdivs :: [Double])
in 2 * (leftSemiSegment + rightSemiSegment)
res =
let value = integRes
integResFmtStr = ((printf "%.16f" value) :: String)
errorRate = (100/2) * (2-value)
errorRateFmt = ((printf "%.4f" errorRate) :: String)
in "Expected value is 2, algo has computed: " <> integResFmtStr <> "\n"
<> "Error rate is " <> errorRateFmt <> "%"
terms = (( map (printf "%.16f") $ take 16 (areaSubdivs :: [Double])) :: [String])
areaSubdivs = asc (pi/4) 4
where
asc c d =
let ch = sin c
nt = d*2
n = c + pi/nt
nh = sin n
hdel = nh - ch
ldel = n - c
in ch*ldel+(hdel*ldel/2) : asc n nt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment