
%graphics_toolkit('gnuplot');

%%% settings for the graphical output
close all;
figure('position',[1000,300,900,800]), hold on
 axis([ 0 1  0 8  ]), grid on;


%% number of grid points, discretization step

N=30;
h=1/N;

%% initial condition

x0=1;


%% the exact solution 

ex = @(t) x0 ./ (1 - x0.*t);
dt = 0.01;
I = 0:dt:1;

	%% plot as blue line
plot(I,ex(I),'-b');


%%% 1st order Euler's method

Xeuler = zeros(1,N);
Xeuler(1) = x0;


for i = 1:(N-1)

	Xeuler(i+1) = Xeuler(i) + h*(Xeuler(i))^2;

end

T = h*(0:N-1);

	%% plot as magenta circles
plot(T,Xeuler,'om');


%pause

%%% 2nd order method                              

Xsecond = zeros(1,N);
Xsecond(1) = x0;

for i = 1:(N-1)
	Xsecond(i+1) = Xsecond(i) + h*(Xsecond(i))^2 + h^2*(Xsecond(i))^3;
end

	%% plot as red stars
plot(T,Xsecond,'*r');

%%% add some legends to the plot
leg = legend( "exact solution","1st order","2nd order" );
legend( leg , "location", "northwest");
legend( leg , "boxoff") ;
set (leg, "fontsize", 15);

%%%% optionally, save the picture as .pdf or .png file
%print -dpdf "demo1.pdf"
%print -dpng "demo1.png"

pause;
