Example of Vector Field - Trajectory - Damped Oscillation

 

 

 

 

 

 

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 >

 

v = -1:0.2:1;

 

[y1,y2] = meshgrid(v);

 

u1=y2;

u2=-y1 - 0.25 .* y2;

 

 

function dy_dt = f(y, t)

  dy_dt = zeros (2,1);

 

  dy_dt(1) = y(2);

  dy_dt(2) = -y(1) - 0.25 .* y(2);

endfunction

 

 

hFig = figure(1);

 

subplot(1,2,1);

 

hold on;

y1_end = 1.0;

 

for y1_init = -1.0:0.1:y1_end

 

   y0 = [y1_init;1.0];

   t = 0:0.2:25.0;

   y = lsode ("f", y0, t);

 

   tStr = sprintf("y1(0) = %.2f, y2(0) = %.2f",y0(1),y0(2));

 

   plot(t,y(:,1),'r-',t,y(:,2),'b-');

   xlabel('time');

   xlim([0 25]);

   ylim([-1.5 1.5]);

   legend('y2(t)','y1(t)');

   %title(tStr);

   

end;

 

box on;

 

hold off;

 

subplot(1,2,2);

 

quiver(y1,y2,u1,u2);

box on;

 

hold on;

for y1_init = -1.0:0.1:y1_end

 

   y0 = [y1_init;1.0];

   t = 0:0.2:25.0;

   y = lsode ("f", y0, t);

 

   tStr = sprintf("y1(0) = %.2f, y2(0) = %.2f",y0(1),y0(2));

   

   plot(y(:,1),y(:,2),'r-'); xlabel('y(2)');ylabel('y(1)');

   axis([-1 1 -1 1]);

 

   plot(y0(1),y0(2),'bo','MarkerFaceColor',[0 0 1],'MarkerSize',10);

   plot(y(end,1),y(end,2),'ro','MarkerFaceColor',[1 0 0],'MarkerSize',10);

   %title(tStr);

   

end;

 

hold off;

 

 

set(hFig,'Position',[300 300 780 300]);