As an example, suppose we want to solve the one-dimensional ordinary
differential equation
To view the exact solution in Matlab, we create a file called ``yexact.m'' with the following lines of text:
function r = yexact(t,y0,K,s)
r = y0*exp(K*t) + s*(1 - exp(K*t));
Note that this function takes four arguments, the time t, the initial condition y0, and the constants K and s from (5). Suppose that we want the solution for y0=100, K=1, and s=20. First, in Matlab we type:
t = 0:0.01:5;
which creates a vector t = (0,0.01,0.02,...,4.98,4.99,5), then
plot(t,yexact(t,100,1,20))
which plots yexact vs. t at the times given in the vector t. This creates a plot as shown in Figure 1 below.