% Igor Yanovsky
%
% Created for Math 151A class
%
% This is an example of numerical differentiation being used.
% The function is f(x) = sin(x). We know that the derivative
% of this function is f'(x) = cos(x). By looking at the output,
% the derivative that we calculated using numerical differentiation
% is what we expected.
%

h = 0.1
x = -2*pi:h:2*pi;
size(x)
f = sin(x);

for(i=1:size(x')-1)
    fprime(i) = (f(i+1)-f(i)) / h;
end


figure
subplot(2,1,1)
plot(x,f)
xlabel(['\bf x'], 'FontSize', 11, 'Color',[0 0 0.3]);
ylabel(['\bf f(x) = sin(x)'], 'FontSize', 11, 'Color',[0 0 0.3]);
title(['\bf Plot of f(x) = sin(x)'], 'FontSize', 11, 'Color',[0 0 0.3]);
grid on

subplot(2,1,2)
plot(x,fprime)
xlabel(['\bf x'], 'FontSize', 11, 'Color',[0 0 0.3]);
ylabel(['\bf f`(x)'], 'FontSize', 11, 'Color',[0 0 0.3]);
title(['\bf Plot of f`(x)'], 'FontSize', 11, 'Color',[0 0 0.3]);
grid on
