The differential equation we just looked at is quite easy to solve symbolically. Let's do that.
Exercise 3.4
a. As can be easily verified, the solution to the initial value problem in Example 3.3 is
h(x) = -cos(x) + 2
Now with the solution of the above differential equation, we can compare the results
obtained by using ode45 to the actual values. Type the following commands into MATLAB:
>> h = @(x) -cos(x) + 2;
[x, y, h(x), abs(y - h(x))]
Include this code and the output in your writeup. The second and third columns give us the
estimate that we found using ode45 and the actual function value at each specified value
of x. How do the values in the two columns compare? The fourth column gives the absolute
value of the difference between the function and our approximation; this is the error of our
approximation.
b. Now let us compare the results that ode45 gives us with the results we would get from
using Euler's method. Enter the following commands into MATLAB:
>> [x, z] = Euler(0.25, 0, 1, 10, g);
[y, z, h(x), abs(y - h(x)), abs(z - h(x))]
Copy the input and output to your Word document. In the first column, we have the results
of ode45; in the second, the results of our Euler's Method routine; and in the third, the
values at x of the real solution to our differential equation. Columns four and five give the
error of ode45 and of Euler's Method, respectively. Which method seems to give more
accurate answers in this situation?