SNEWTON.OMS
Script File:
# Description:
# Finds multiple roots of a function.
#
# function to find zero of
function f(x) begin
return sin(x)
end
#
# derivative of function to find zero of
function df(x) begin
return cos(x)
end
# value we are solving for is 0
fval = fill(0., 10, 1)
# start Newton's method at 1, 2, .., 10
xini = real(seq(10))
# maximum number of iterations
mitr = 10
xsolve = snewton(function f, function df, fval, xini, mitr)
# put solutions in order
xsolve = sort(xsolve)
#
print "The roots of sin(x) between 1 and 10 are"
for i = 1 to rowdim(xsolve) begin
ok = 1. <= xsolve(i) and xsolve(i) <= 10.
if i <> 1 then ok = ok and |xsolve(i) - xsolve(i-1)| > 1e-4
if ok then print xsolve(i)
end
Output:
The roots of sin(x) between 1 and 10 are
3.14159
6.28319
9.42478