Skip to content

Instantly share code, notes, and snippets.

@cfstras
Created January 22, 2014 13:22
Show Gist options
  • Save cfstras/8558619 to your computer and use it in GitHub Desktop.
Save cfstras/8558619 to your computer and use it in GitHub Desktop.
Demonstration of the Secant method (Sekantenmethode) with an initial Newton step.
# functions
f = (x) ->
2 - x*x - Math.exp(x)
fd = (x) ->
-2*x - Math.exp(x)
newt = (x) ->
x - f x / fd x
sek = (xp, x) ->
x - (x - xp) / ( f x - f xp ) * f x
log = (i,s) ->
console.log i + ': ' + s
# go!
xp = 2.5;
log 0,xp
# one newton step
x = newt xp
log 1,x
# 7 secant steps
for i in [2..8]
oldx = x
x = sek xp,x
xp = oldx
log i,x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment