SUNY Geneseo Department of Mathematics
Monday, April 4
Math 230 02
Spring 2016
Prof. Doug Baldwin
function [ fPrime ] = derivative( f )
%DERIVATIVE Generates a new function that approximates the derivative of f
h = 1e-10;
fPrime = @(x) ( f(x+h) - f(x-h) ) / (2*h);
end
derivative
and then
calling it later with x>> f = @(x) x^2
f =
@(x)x^2
>> fPrime = derivative( f )
fPrime =
@(x)(f(x+h)-f(x-h))/(2*h)
>> fPrime( 5 )
ans =
10.0000
>> fPrime( -2 )
ans =
-4.0000
>> g = derivative( @sin )
g =
@(x)(f(x+h)-f(x-h))/(2*h)
>> g( 0 )
ans =
1