Posted to tcl by mjanssen at Tue Nov 11 18:12:54 GMT 2008view pretty

proc poly {coef f} {
	set i 0
	set body {}
	set coef [lreverse $coef]
	foreach c $coef {
	    	append body +$c*pow(\$x,$i)
		incr i
	}
	proc $f x "expr {$body}"
}

proc deriv {coef f} {
	set i 1
	set body {}
	set coef [lrange [lreverse $coef] 1 end]
	foreach c $coef {
	    	append body +$c*$i*pow(\$x,$i-1)
		incr i
	}
	proc $f x "expr {$body}"
}

proc newton {f f' x0} {
	# just do 4 iterations
	for {set i 0} {$i<10} {incr i} {
		set x0 [expr {$x0 - double([$f $x0])/[${f'} $x0]}]
		puts $x0		
	}
	return $x0

}

poly {1 0 -612} f
deriv {1 0 -612} f'
newton f f' 10