FORDIFFX.OMS
Script File:
# Description:
# Compares the forward difference with the analytic derivative for
# a quadratic.
#
clear
# matrix defining the quadratic function
const A = diag(seq(3))
#
# function to be differentiated
function f(x) begin
return .5 * x' * A * x
end
# the analytic derivative
function df(x) begin
return x' * A
end
x = real(seq(3))
h = x / 10e0
format real "f10.5"
print "analytic derivative = ", df(x)
print "numerical derivative = ", fordiff(function f, x, h)
Output:
analytic derivative = [ 1.00000 , 4.00000 , 9.00000 ]
numerical derivative = [ 1.05000 , 4.20000 , 9.45000 ]