% solves numerically the ode dx/dt = mu + sin(x) (Ex 4.5.3)

clear all
%timestep
dt = 0.001;
%final time
T = 2;
%no of steps
tsteps = ceil(T/dt);
% initial condition
x0 = 0;
% parameter
mu = 2.0;

time =0;
xn = x0;

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

%the Euler method
for n=1:tsteps,
    fn = mu + sin(xn); 
    xn = xn + dt*fn;
    time = dt*n;

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

plot(tdata,xdata);
%plot(tdata,xdata,'ko')
xlabel(['\bf t'],'fontsize',12)
ylabel(['\bf \theta(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.