Ordinary differential equations: Slope field and solution curves with Matlab
Forward Euler method <br> [MATLAB task]
The figure below shows the, via the forward Euler method calculated, function values for the initial value problem \(\frac{\dd y}{\dd t}=1-2t y,\; y(0)=1\) in a line graph for a step size of \(dt=0.25\) (in the colour red). In order to better interpret the curve, the slope field is also drawn in the diagram. As the step size of \(dt\) becomes smaller, the graphs of the calculated function values becomes smoother and start to look more and more like the graph of an exact solution. To illustrate this, the solution \(\frac{\dd y}{\dd t}=1-2t y,\; y(0)=1.5\) is added to the diagram (in blue, with a step size of \(0.125\) ).
MATLAB assignment
Part 1:
Create a MATLAB program that implements the forward Euler method for any function \(\varphi(t,y)\) and then apply it to the following initial value problems:
\[\frac{\dd y}{\dd t}=1-2ty,\qquad y(0)=1.5\]
\[\frac{\dd y}{\dd t}=1-2ty,\qquad y(0)=.25\]
\[\frac{\dd y}{\dd t}=y(1-\tfrac{1}{3}y),\qquad y(-1)=.25\]
\[\frac{\dd y}{\dd t}==y(1-\tfrac{1}{3}y),\qquad y(-1)=2.5\]
The most important function in your program is called Euler
and it has the following structure:
function [eta_data] = Euler(phi, t0, y0, t1, n)
% phi: the function in the ODE dy/dt = phi(t,y)
% t0: time at the initial value y(t0) = y0 of the first solution curve
% y0: initial value of the first solution curve
% t1: terminal time for the interval of the solution curves
% n: the number of steps in the Euler method
% OUTPUT: eta_data. This is an array of two sequences. The first sequence
% contains all points in time and the second sequence contains all
% computed function values.
...
end
Always make a diagram in which the slope field and the graphs of the solution curves are drawn. If the differential equation stays the same, but the initial values change, you can draw the solution curves in one diagram.
Part 2:
Compare the solution curve of \[\frac{\dd y}{\dd t}=1-2ty,\qquad y(0)=1.5\] for several values for \(n\) with the result of this differential equation using an ODE solver in MATLAB (search 'ode solver' in the manual).