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 >
clear;
t = 0:0.02*pi:10*pi;
global k = 1.0;
global m = 4.0;
function dy_dt = f(y, t)
global k;
global m;
dy_dt = zeros (2,1);
dy_dt(1) = y(2);
dy_dt(2) = -k/m .* y(1);
endfunction
y2_init = 0.0;
y1_init = 1.0;
y0 = [y2_init y1_init];
y = lsode ("f", y0, t);
hFig = figure(1);
subplot(1,5,[1 2 3]);
plot(t,y(:,1),'r-','LineWidth',2,t,y(:,2),'b-','LineWidth',2);
xlabel('time');
xlim([0 t(end)]);
ylim([-3 3]);
xtick = 0:pi:10*pi;
set(gca,'xtick',xtick);
set(gca,'xticklabel',{'0','pi','2pi','3pi','4pi','5pi','6pi','7pi','8pi','9pi','10pi'});
legend('y2(t)','y1(t)');
grid on;
subplot(1,5,[4 5]);
plot(y(:,1),y(:,2),'r-','LineWidth',2); xlabel('y(2)');ylabel('y(1)');
axis([-3 3 -3 3]);
pbaspect([1 1 1])
tStr = sprintf("y1(0)=%0.2f,y2(0)=%0.2f \nk=%0.2f,m=%0.2f",y1_init,y2_init,k,m);
title(tStr);
set(hFig,'Position',[300 300 780 300]);
|