%% Linear Function Approximation
% Use the gradient at a particular point to linearly approximate the function value at a nearby point and compare it to the actual value.
% The equation for linear approximation of a function value is
% f(x) \approx f(x_0) + \left( \nabla f \right)_{x_0} \cdot (x-x_0).
% That is, if you know the value of a function f(x_0) and the slope of the derivative \left(\nabla f\right)_{x_0} at a particular point x_0, then you can use this information to approximate the value of the function at a nearby point f(x) = f(x_0 + \epsilon).
% Calculate some values of the sine function between -1 and 0.5. Then calculate the gradient.

y = sin(-1:0.25:0.5);
yp = gradient(y,0.25);

%
% Use the function value and derivative at x = 0.5 to predict the value of sin(0.5005).

y_guess = y(end) + yp(end)*(0.5005 - 0.5)

%
% Compute the actual value for comparison.

y_actual = sin(0.5005)

% Copyright 2012 The MathWorks, Inc.