% compute and plot derivatives and models of function -log(x)-5*(x-.5)^3+7*x 
%
% To get help for a command "command_name" type
%   help command_name
%
% To get an overview over all help topics type
%   help 
% without any arguments.

%
% specify points (domain) of evaluation
%

x=0.001:.01:2;                   % generate vector of x-values

%
% plot function, first and second derivative
%

y=-log(x)-5*(x-.5).^3+7*x;       % compute function componentwise
y1=-1./x-15*(x-.5).^2+7;         % compute first derivative componentwise
y2=1./(x.^2)-30*(x-.5);          % compute second derivative componentwise

figure(1);                       % output next plot on window of figure 1 
hold off;                        % overwrite old plot in figure 1
plot(x,[y;y1;y2]);               % plot function, first and second derivative 
axis([0 2 -10 10]);              % set value range to plot

%
% plot function, first order and second order model for point xp
%

xp=.101;                         

fp=-log(xp)-5*(xp-.5).^3+7*xp;   % function value at xp
f1p=-1./xp-15*(xp-.5).^2+7;      % first derivative at xp
f2p=1./(xp.^2)-30*(xp-.5);       % second derivative at xp

ym1=fp+f1p'*(x-xp);                  % first order model with respect to xp
ym2=fp+f1p'*(x-xp)+f2p*(x-xp).^2/2;  % second order model with respect to xp

figure(2);                       % output next plot on window of figure 2
hold off;                        % overwrite old plot in figure 2
plot(x,[y;ym1;ym2]);             % plot function, first and second order model
axis([0 2 -10 10]);              % set value range to plot

%
% do the same for another point in figure 3
%

xp=1.001;

fp=-log(xp)-5*(xp-.5).^3+7*xp;
f1p=-1./xp-15*(xp-.5).^2+7;
f2p=1./(xp.^2)-30*(xp-.5);

ym1=fp+f1p'*(x-xp);
ym2=fp+f1p'*(x-xp)+f2p*(x-xp).^2/2;

figure(3);

plot(x,[y;ym1;ym2]);

axis([0 2 -10 10]);
