haskell newton’s method

typically the first thing i do when learning a new programming language is to write newton’s method for root-finding (locating the y value where a function evaluates to 0) in it. here’s my (probably silly) haskell version. i’ll go through it piece by piece so i understand it better.

converge (old:new:xs) t = if reldiff old new <= t
                          then new
                          else converge (new:xs) t
    where reldiff old new = abs (old-new) / old

here i define a function ‘converge’ which, because there are two chunks before the equals sign, takes two arguments. function arguments (both defining and calling) in haskell are separated by spaces, much like in lisp. the second argument is just a threshold value; once the relative difference between successive newton iterates becomes less than t, the recursion stops and the most recent iterate is returned.

the first argument is more complicated. the parenthesized thing is a pattern matching expression; it takes a list with at least two values, binds the first two to ‘old’ and ‘new’, and the rest of the list (possibly empty) to xs. haskell lists are linked lists just like in lisp. ‘:’ is the list constructor, which you can see both in the pattern matching and in the else (recursive) clause. since we split the original first parameter apart we can’t just call ‘tail’ (lisp ‘cdr’) on it, we have to put it back together. so what is this mysterious list? it’s an infinite list of newton iterates, of course. i’ll come back to this.

finally, the where clause at the bottom lets me specify bound variables to be used in the definition of converge. functions are just a type of variable, so i can define the local function reldiff here. it just computes the relative (percentage) difference between its two parameters, old and new.

newton f f' x0 = x1:xs
    where x1 = x0 - f x0 / f' x0
          xs = newton f f' x1

yes, all the work here is done in the where clause. newton takes three parameters: in order, a function f, f’s derivative f’, and an initial guess x0. x1 is simple; it’s the next iterate, computed via the iteration function in the wikipedia article i linked to at the top. the definition for xs would be doing something really scary in just about every other language: non-terminating recursion. also since newton generates a list, we end up with an infinite list too. happily, haskell does lazy evaluation, which means that new newton iterates are only computed when they are asked for by converge.

main = putStrLn (show (converge (newton f f' 3) 1e-3))
    where f  x = sin x
          f' x = cos x

nothing much to see here, just some i/o. show converts things to strings so they can be printed. this applies newton’s method to sin(x) with an initial guess of 3 and a threshold of one thousandth. when it is run it prints out a value for pi accurate to ten significant digits, since sin(pi) = 0 and 3 is pretty close to that zero crossing.

comedy gold

omg this is hilarious (from indexed):

birthday

i am 24 years old today. yay, i guess.

you’re wearing… A SPARKLY!

we saw the secret of nimh yesterday on the silver screen. it was good and filled with sparklies all over. then we hung out with our cool friend katie who works in the box office whom we haven’t seen in months, and it was really good that we were awake enough to go out yesterday because it was her last day working there. all told we were away ten hours for work, then three for movie and talking to katie. very good. we’re pretty tired again today though. :/ i want to go to cool events and make friends and that’s hard when we’re always tired.

(aside from james: they had no problem with depicting an actual government agency in that light in a kids film? interesting.)

how i spent my stimulus

howispentmystimulus.com. with pictures.

i put mine in the bank. this is more patriotic than spending it because it provides badly-needed improvement in bank balance sheets and, because we use a fractional-reserve banking system, allows banks to lend more than the original $600 for other people to spend. either that or i’m just a cheapskate.

creepy posters

many of these (mostly) polish movie posters are creepy, but i think this one would have scared me the most as a small child. yes, i was a weird kid.

favorite teas

…for the moment, anyway.

mighty leaf orange dulce and green tea tropical. they have large loosely woven teabags that lets them put loose tea-sized leaves in them, along with other yummy things like unground spices and dried fruit bits. the result is delicious. i want some of their flowering teas, but local stores don’t carry them. i also enjoy bigelow earl grey green and of course constant comment, which i am drinking right now. ^_^

i like my tea hot enough to make me slow down and enjoy it, without milk added, and sweetened with honey.

i voted for obama

yesterday i did my duty as a citizen and cast an (early) vote in the democratic primary. ordinarily the nc primary, which takes place on may 6, is too late in the race to make a difference, but obama and clinton have kept it going longer than usual.

i voted for obama because he will probably make foreign policy decisions more in line with my opinions. for example, clinton recently said “in the next 10 years, during which [iran] might foolishly consider launching [a nuclear] attack on israel, we would be able to totally obliterate them.” (source) i do not think presidential candidates ought to be so casually threatening genocide. this is just one example out of many. clinton was not even against the war until fairly recently; obama since before it started. the war is unpopular now, but if there is another terrorist attack then the us public will go temporarily insane again, and we need a president who will stick to their principles should this happen. also, obama is cuter.

for the nc senate primary i voted for jim neal. none of the other candidates stood out, and he is openly gay. he isn’t obviously a bad idea so that is enough to get my vote.

on how the two of us decide who to vote for: one might assume otherwise, but we actually rarely disagree. it would be nice to have mainstream candidates we could actually disagree about (james favors pragmatic technocrats a bit more), but we are both sufficiently left of the us center that we just end up agreeing on the leftmost mainstream candidate.

a really simple portfolio tracker script

around 4 am i couldn’t sleep so i was sitting around in the dark listening to stairway to heaven and coding. i wrote a mini-script in ruby to track my investment portfolio. it uses the yahoo finance ruby module and tracks the return of an unrebalanced portfolio from a specified date in the past to the most recent market close. when i run it for myself here are the results:

VTI: 37.5% of total
VEU: 37.5% of total
BND: 25.0% of total
Return since 2007-06-12: 2.9%

which i thought was pretty good; before i ran the numbers i expected a negative return. here is the code:

Read More »

first post

i’m trying this out as an experiment because livejournal seems to be going downhill what with the recent purchase by sup. my livejournal username is thehumangame. i’ve had good experiences with wordpress before. they let you save drafts of posts you’re working on, something livejournal doesn’t do. well, we’ll see how this goes.