MATLAB: Can i mark the first point and the last point on the plot

markerplot

if true
% clear;
clc;
t=[0 20];
y0=[1 0];
[t,y] = ode45(@(t,y) sfunc(t,y),t,y0);
plot(t,y(:,1),'blue');
hold on;
pfirst=find(t==0);
plast=find(t==20);
plot(t,y(:,2),'red');
title('a simple harmonic oscillator with ODE45');
xlabel('time t');
ylabel('y1(metres) y2(metres/sec)');
legend('y1','y2');
function dydt=sfunc(~,y)
dydt=zeros(2,1);
w=0.3;
dydt(1)=y(2);
dydt(2)=(-w^2)*y(1);
end
end

Best Answer

t=[0 20];
y0=[1 0];
[t,y] = ode45(@(t,y) sfunc(t,y),t,y0);
plot(t,y(:,1),'blue');
hold on;
plot(t,y(:,2),'red');
Y = y(:,1) %to avoid confusion

plot(t(1,1),Y(1,1),'ob') %plots first point

plot(t(end),Y(end),'ob') %plots end point

Y2 = y(:,2) %to avoid confusion
plot(t(1,1),Y2(1,1),'*r') %plots first point
plot(t(end),Y2(end),'*r') %plots end point
title('a simple harmonic oscillator with ODE45');
xlabel('time t');
ylabel('y1(metres) y2(metres/sec)');
legend('y1','y2');
function dydt=sfunc(~,y)
dydt=zeros(2,1);
w=0.3;
dydt(1)=y(2);
dydt(2)=(-w^2)*y(1);
end