Ordinary differential equations: Slope field and solution curves with R
Forward Euler method [R task]
Create an R script that implements the forward Euler method for any function \(\varphi(t,y)\) and then apply it to the following initial value problems:
\[\begin{aligned} \frac{\dd y}{\dd t}&=y\,(1-\tfrac{1}{3}y),\quad &y(-1)=.25\\ \\
\frac{\dd y}{\dd t}& =y\,(1-\tfrac{1}{3}y), &y(-1)=2.5\\ \\
\frac{\dd y}{\dd t}&=y\,(1-\tfrac{1}{3}y), &y(-1)=5.0\\ \\
\frac{\dd y}{\dd t}&=y\,(1-\tfrac{1}{3}y), &y(-1)=-0.25\end{aligned}
\]
The most important function in your program is called Euler
and it has the following structure
Euler <- function(phi, t0, y0, t1, N=100) { <
# phi is the function for the ODE dy/dt = phi(t,y)
# initial value y(t0) = y0
# time interval is [t0, t1]
# N is the numer of steps in the Euler method
# df is a table with successively computed points of the solution curve
...
return(df)
}
Always make a diagram in which the slope field and the graph of the solution curve are drawn. If the differential equation stays the same, but the initial values change, you can draw the solution curves in one figure using different colours.