%euler: quick and dirty Matlab script to show Euler's polygonal method % on simple logistic model: % % dx/dt = x ( 1 - x ) , 0 < t < T , x(0) = x0 % % usage: edit below and run % ~/matlab/misc/euler.m % % C.G. (11-98) % C.G. (02-06) minor touch ups x0 = .01 ; % initial data for x(0) T = 10 ; % time interval for integration ntimes = 8 ; % number of curves to generate and draw n = 1 ; % initial value for number of steps (doubled each time) clf , hold on % initialize plotting window for time = 1 : ntimes n = 2 * n ; % double number of time steps dt = T / n ; % time-step size x = zeros( 1, n+1 ) ; % pre-allocate x(1:n+1) x(1) = x0 ; % initial condition for k = 1 : n x(k+1) = x(k) + dt * x(k) * ( 1 - x(k) ) ; end t = linspace( 0, T, n+1 ) ; % equally spaced 't' values for plotting plot( t, x ) % plot polygonal approximation end % pretty up plot set( gca, 'FontSize', 12 ) box on xlabel( '\it t' ) ylabel( '\it x' ) title( 'Logistic Model: Euler''s Method ({\it n} = 2, 4, ... , 256 )', ... 'FontSize', 14 ) axis tight