% Lab 5, Oct. 20, 2016 % Recall the forward Euler method for approximating the solution % curve x(t) from the initial value problem dx/dt=f(x), x(t_0)=x0 with a % uniform step size h: % dx/dt~[x(t+h)-x(t)]/h % [x(t+h)-x(t)]/h=f(x) % x(t+h)=x(t)+h*f(x) % Consider the linear univariate continuous deterministic model: % dx/dt=rx, x(t_0)=x0 % Example: Take r=2, x0=0.2 % a) Use the forward Euler method with h=0.05 to estimate x(1) % First we need to calculate how many steps of size h it will take to get % from x(0) to x(1) % h=(b-a)/N; The step size is the length of the time interval divided by % the number of steps. Rearrange this to solve for N %% h=0.05; N=(1-0)/h; %% % Now do the usual setup of initial conditions Xnext=0; %will replace these values with the appropriate value from the loop tnext=0; Xcurr=0.2; tcurr=0; r=2; f=@(x) (2*x); %% for(i=1:N) tnext=tcurr+h; Xnext=Xcurr+h*f(Xcurr); tcurr=tnext; Xcurr=Xnext; end %% disp('The approximate value of x(1) is:') disp(Xcurr); %% % b) Now calculate x(t) explicitly % dx/dt=r*x % dx/x=r*dt % ln(x)=r*t+C % x(t)=A*e^(rt) % sub initial condition: x(0)=0.2 % 0.2=A % explicit solution: x(t)=0.2e^(2*t) x1=0.2*exp(2*1); disp('The exact value of x(1) is:') disp(x1); %% % Example 2: Logistic Equation % dx/dt=r*x*(1-x/K) % What are the fixed points? % x*=0 and x*=K % What is the stability of the fixed points? % f(x)=r*x*(1-x/K) % Take the derivative of f(x) and evaluate at the fixed points % f'(x)=r-2*r*x/K=r*(1-2*x/K) % f'(0)=r Therefore x*=0 is stable if r<0 % f'(K)=-r and x*=K is stable if r>0