Created
January 22, 2014 13:22
-
-
Save cfstras/8558619 to your computer and use it in GitHub Desktop.
Demonstration of the Secant method (Sekantenmethode) with an initial Newton step.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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