Hi fellow internet user,
I am a computer engineering student in my last year (not quite true but its a long story!). I have recently learned Haskell which I quite fancy for the moment. I was wondering how I can find the motivation to dwell into this particulate programming language more than just passing my course assignments! Fortunate or not I have not passed my Numerical Analysis course. So while waiting for the re-exam in late August I will rewrite some of the numerical methods presented to me in the course in Haskell instead of as Matlab scripts.
And I start with an easy one, a bracketing method called The Bisection Method. Without further ado here is my code:
type F = Float
bisec :: (F -> F) -> (F,F) -> F -> F
bisec f (a, b) tol
| (b-a)/2 < tol = (a+b)/2
| f(c) == 0 = c
| f(a)*f(c) < 0 = bisec f (a,c) tol
| otherwise = bisec f (c,b) tol
where c = (a+b)/2
func :: F -> F
func x = x^3+x-1
main = do
let answer = bisec (func) (0, 2) 0.0000005
print answer
No comments:
Post a Comment