Derivatives - Exponential

 

 

 

 

 

 

Followings are the code that I wrote in Octave to creates all the plots shown in this page. You may copy these code and play with these codes. Change variables and try yourself until you get your own intuitive understanding.

 

< Code 1 >

 

x = linspace(0,4,80);

y = 2.0 .* exp(-x);

 

ylim_y = [0 2];

ylim_yp = [-2 0];

ylim_ypp = [0 2];

 

np = 76;  % the last array index to plot y'(x). Must be between 3 and 77

npp = 76; % the last array index to plot y''(x). Must be  between 1 and 76

 

yp = [];

for i = 2:length(y)-1

  dx = x(i+1) - x(i-1);

  dy = y(i+1) - y(i-1);

  yp = [yp (dy ./ dx)];

end  

 

ypp = [];

for i = 2:length(yp)-1

  dx = x(i+1) - x(i-1);

  dy = yp(i+1) - yp(i-1);

  ypp = [ypp (dy ./ dx)];

end  

 

hFig = figure(1,'Position',[300 300 600 500]);

 

% Calculate the tangent line for y(x)

range = 1.0;

bt = y(np) - yp(np) .* x(np);

xt = linspace(x(np)-range,x(np)+range,10);

yt = yp(np) .* xt .+ bt;

 

thickness_f = 1

thickness_t = 2

 

subplot(3,3,[1 2]);

hold on;

plot(x,y,'k-','LineWidth',thickness_f);

if (npp < 4)

  plot(xt,yt,'b-','LineWidth',thickness_t);

  plot(x(np),y(np),'bo','MarkerFaceColor',[0 0 1],'MarkerSize',8);

end

xlim([x(1) x(end)]);

ylim(ylim_y);

ylabel('\bf \fontsize{20}  f(x)')

box on;

hold off;

 

if (np > 2)

  subplot(3,3,3);

  axis([0 1 0 1]);

  if(npp <= 1)

    tStr = sprintf('Slope at %0.02f',x(np));

    text(0,0.7,tStr,'FontSize',14);

    tStr = sprintf('= %0.02f',yp(np-1));

    text(0.0,0.5,tStr,'FontSize',14);

  end

  set(gca,'Visible','off')

  

  % Calculate the tangent line for y''(x)

  range = 1.0

  btt = yp(npp) - ypp(npp) .* x(npp+1);

  xtt = linspace(x(npp)-range,x(npp)+range,10);

  ytt = ypp(npp) .* xtt .+ btt;

  

  subplot(3,3,[4 5]);

  hold on;

  plot(x(2:np),yp(1:np-1),'b-','LineWidth',thickness_f);

  if(npp <= 1)

    plot(x(np),yp(np-1),'bo','MarkerFaceColor',[0 0 1],'MarkerSize',8);

  end

  if (npp > 3)

    plot(xtt,ytt,'r-','LineWidth',thickness_t);

    plot(x(npp),yp(npp-1),'ro','MarkerFaceColor',[1 0 0],'MarkerSize',8);

  end

  xlim([x(1) x(end)]);

  ylim(ylim_yp);

  ylabel('\bf \fontsize{20}  f\prime(x)');

  box on;

  hold off;

end

 

if (npp > 3)

  subplot(3,3,6);

  axis([0 1 0 1]);

  tStr = sprintf('Slope at %0.02f',x(npp));

  text(0,0.7,tStr,'FontSize',14);

  tStr = sprintf('= %0.02f',ypp(npp-2));

  text(0.0,0.5,tStr,'FontSize',14);

  set(gca,'Visible','off')

end  

  

  subplot(3,3,[7 8]);

  hold on;

if (npp > 3)  

  plot(x(3:npp),ypp(1:npp-2),'r-','LineWidth',thickness_f);

  plot(x(npp),ypp(npp-2),'ro','MarkerFaceColor',[1 0 0],'MarkerSize',8);

end  

  xlim([x(1) x(end)]);

  ylim(ylim_ypp);

  ylabel('\bf \fontsize{20}  f\prime\prime(x)');

  box on;

  hold off;