% Example: Euler Method for Radioactive Decay % Author: F. P. Schloerb clear k = 0.05; % decay constant N0 = 1000; % initial value dt = 1; % step size t = 0:dt:100; % array of times for calculation N = zeros(length(t),1); % define solution array % Main Loop to calculate N for all times t N(1) = N0; for i=1:length(t)-1 N(i+1) = N(i) - k*N(i)*dt; end % make some nice graphs plot(t,N,'md') % the numerical solution hold on plot(t,N0*exp(-k*t),'k') % the exact solution hold off xlabel('t'); ylabel('N'); legend('Euler Method','Exact')