% solves numerically the ode in Ex 3.7.5

clear all
%timestep
dt = 0.01;
%final time
T = 50;
%no of steps
tsteps = ceil(T/dt);
% initial condition
x0 = 2;
% parameters
r = 1/4; s = 5;

time =0;
xn = x0;

% store the discrete time and solution values
tdata = time;
xdata = x0;

%the Euler method
for n=1:tsteps,
    fn = s -r*xn + xn^2/(1+xn^2); 
    xn = xn + dt*fn;
    time = dt*n;

	% store data
	tdata = [tdata time];
	xdata = [xdata xn];
end;	

figure(1); clf;
plot(tdata,xdata);
%plot(tdata,xdata,'ko')
xlabel(['\bf t'],'fontsize',12)
ylabel(['\bf x(t)'],'fontsize',12)
    
% the vector tdata contains the set of discrete times t0,t1,...,tn,...,T
% the vector xdata contains the approximations x0,x1,...,xn,...
% ploting xdata versus tdata means plotting the "numerical" solution
% x(end) represents the last element of this vector, it is the numerical
% solution at time =T.