% This function creates and returns a handle to the linear function % through ( x1, f(x1) ) and ( x1+h, f(x1+h) )... in other words the % function for the secant to f through those two points. % f handle to a function % x1 start of interval % h length of interval (should be non-zero) function sec = secantLine( f, x1, h ) if (sum(h==0)) error('secantLine:', "Can\'t compute a secant over an empty interval."); end slope = ( f(x1+h) - f(x1) ) ./ h; % Build a string representation of anonymous function for the secant line. str = [ '@(x)' num2str( f(x1) ) '+' num2str( slope ) '.* (x -' num2str(x1) ')' ]; % Evaluate and return the string. sec = eval( str );