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,6*pi,80);
y = 1.0 .* sin(0.075 .* x .^ 2);
ylim_y = 4.0 .* [-1.5 1.5];
ylim_yp = 4.0 .* [-1.5 1.5];
ylim_ypp = 4.0 .* [-1.5 1.5];
np = 77; % the last array index to plot y'(x). Must be between 3 and 77
npp =78; % the last array index to plot y''(x). Must be between 3 and 77
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 = 2.0;
bt = y(np) - yp(np-1) .* x(np);
xt = linspace(x(np)-range,x(np)+range,10);
yt = yp(np-1) .* 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)')
grid on;
box on;
hold off;
if (np > 2)
subplot(3,3,3);
axis([0 1 0 1]);
if (npp <= 3)
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 = 2.0;
btt = yp(npp-1) - ypp(npp-2) .* x(npp);
xtt = linspace(x(npp)-range,x(npp)+range,10);
ytt = ypp(npp-2) .* xtt .+ btt;
subplot(3,3,[4 5]);
hold on;
plot(x(2:np),yp(1:np-1),'b-','LineWidth',thickness_f);
if(npp <= 3)
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)');
grid on;
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)');
grid on;
box on;
hold off;
|