Visualizing Solutions to ODEs


In this assignment, we are going to investigate differential equations of the form

(1) dydx = ƒ(x,y)

using slope fields. The slope field of this differential equation is a diagram in the (x,y) plane in which there is a small line segment drawn with slope ƒ(x,y) at the point (x,y). For example, the slope field of the differential equation

(2) dydx = x

looks as follows:

Two possible solutions (of infinitely many) to (2) are shown on the graph below. One solution passes through the point (0,0), while the other passes through the point (0,-1). Note that for each point a solution passes through, the corresponding curve below follows the same direction as the line segments around it.

The above slope fields were drawn with the help of a MATLAB M-File called slopefield.m, with which we will shortly become familiar. But first, let's think about the above slope field without the aid of a computer.

The differential equation (2) can be solved analytically, with solution

(3) y(x) = x22 + C

where C is a constant. Notice how the slope field above confirms that (3) is the solution to the differential equation (2): if we start at any point on the graph, and we follow the lines of the slope field, we get a curve of the form given in (3). The constant C is determined by our starting point (i.e., the initial conditions).

Include full-sentence response. Do work by hand.
  1. Sketch (by hand, without using MATLAB) the slope field of the differential equation dydx = y5 for x and y values between -5 and 5. (You do not need to include this sketch in your write-up.)
  2. On your slope field, add a curve (by hand) that approximates the solution passing through the point x = 0, y = 1.
  3. Now solve the differential equation given in part (a), either working it out by hand or using the dsolve command that we saw in Assignment 1. Compare your answers to parts (a) and (b).

It is apparent from the above exercises that drawing the slope fields associated to more complicated differential equations will be a tedious task. Let us now take a closer look at some convenient tools that can help us.

Drawing Slope Fields & Solutions

Before we begin, you will need to make sure you have the two M-Files slopefield.m and drawode.m. The M-Files should be moved into your working directory.

SLOPEFIELD

The M-File slopefield.m defines a new function, slopefield, which draws slope fields of a given first-order ordinary differential equation. If you like, you can type help slopefield into MATLAB to read the documentation for the function. We're going to work through an example to see how slopefield works.

Let's try to use slopefield to draw a slope field for the differential equation

(4) dydt = (1 - t2) ey.

If we check the documentation, we'll see that slopefield takes four inputs: a function, a range of values for the independent variable, a range of values for the dependent variable, and finally a number (density) that controls how many segments we'll draw on our field.

Thus, to use slopefield, we first need to define a new function (say, ƒ(t,y); the name doesn't matter) that represents the right-hand side of this ODE. Recall that we can do that by typing

>> f = @(t,y) (1 - t^2)*exp(y)

Suppose we'd like to see the slope field for t values between -4 and 4 and y values between -3 and 5, with about 20 marks in each direction. Then we simply need to type

>> slopefield(f, [-4,4], [-3,5], 20)

MATLAB produces the following plot:

For the remainder of this lab, we will refer to the independent variable in our differential equations as time, although the name of that variable need not be t, and the independent variable for the equations in question may not literally be time.

Now we can use MATLAB to quickly produce slope fields. Let's explore how these slope fields relate to the solutions of the ODE by learning how to plot some solutions.

DRAWODE

MATLAB includes a number of functions designed to numerically approximate solutions to ODEs. However, they are somewhat inconvenient for our purposes because they typically only compute either forwards or backwards in time, but not both. Thus, for instance, you can't call on these functions to plot the solution to dydt = (1 - t2) ey for t between -4 and 4 with the initial value y(1) = -1, because t = 1 is neither the start time nor the end time.

The M-File drawode.m rectifies this issue. It defines a new function drawode that can plot solutions to a first-order initial value problem regardless of where the "initial" time is located. In the example we just mentioned, it would be able to go backwards from t = 1 to t = -4 and also forwards from t = 1 to t = 4. Let's go ahead and use drawode to do that. Again, it may be useful to check the documentation by entering help drawode.

The function drawode takes four inputs: the function that represents dydt (which, in the case of equation (4), we already named f), a range of times, an initial time t0, and finally an initial value y0 = y(t0).

Again, we want t to be between -4 and 4, and now we'd like y(1) to be -1. So we plug all that in to the appropriate places in drawode:

>> drawode(f, [-4,4], 1, -1)

Now MATLAB draws this plot:

Why do you think the graph ends around t = -2.6, even though we asked it to show t values down to -4? A look at the slope field from the first example may help to explain. In fact, it would be very nice if we could look at both the slope field and our solution curve at the same time.

Combining Plots

Unfortunately, if we try to run slopefield and drawode in succession, the first plot (our slope field) disappears; MATLAB overwrites it with the new graph made by drawode. The keys to solving this are the commands hold on and hold off.

If you use MATLAB to produce a figure and then enter hold on, MATLAB will start drawing any new plots on the same figure, without erasing whatever was there before. After you're done, hold off turns off this behavior so that you can erase your work and draw new plots. Let's see this in action using the work we've already done. (Here, your up-arrow key may prove useful.)

We'd like to draw a slope field for the ODE given in (4) and then plot the solution passing through (1,-1) on that slope field. In MATLAB's command prompt, we'll enter the following:

>> slopefield(f, [-4,4], [-3,5], 20)
hold on
drawode(f, [-4,4], 1, -1)
hold off

Finally, we get our combined plot:

We're not limited to graphing just one solution curve, however. Let's try a few more initial values:

>> slopefield(f, [-4,4], [-3,5], 20)
hold on
drawode(f, [-4,4], 1, -1)
drawode(f, [-4,4], 1, 0)
drawode(f, [-4,4], -1, 0)
drawode(f, [-4,4], -1, 2)
drawode(f, [-4,4], 3, -1)
drawode(f, [-4,4], 3, 1)
hold off

You can copy and paste this code into MATLAB to try it for yourself.

Adjusting the Viewing Area

In Assignment 1, we learned that we could adjust the axes on a plot by using the menu option Edit ❯ Axes Properties…. However, this will not work with slopefield, since slopefield only draws the slope field over a limited area. To change the boundaries on the axes, we'll have to change the bounds we include in slopefield.

We could go through and adjust the range of t values every single time we call slopefield and drawode, but when you're drawing a lot of solution curves the way we did in Example 2.3, that can be a tedious task. Instead, if we had anticipated that we might want to zoom in or out, it would have been better from the beginning to do the following:

>> tmin = -4; tmax = 4;
slopefield(f, [tmin,tmax], [-3,5], 20)
hold on
drawode(f, [tmin,tmax], 1, -1)
drawode(f, [tmin,tmax], 1, 0)
drawode(f, [tmin,tmax], -1, 0)
drawode(f, [tmin,tmax], -1, 2)
drawode(f, [tmin,tmax], 3, -1)
drawode(f, [tmin,tmax], 3, 1)
hold off

Now, if we simply change the value of tmin to -10, change the value of tmax to 10, and adjust the y values in slopefield as desired, we can quickly rescale the plot with all six solution curves included.

If you think you'll enjoy the challenge, you might want to devise an even more efficient way to draw a lot of solution curves, perhaps using for loops and matrices.

When looking at graphs like these, you might find it useful to try the commands grid on and grid off, which turn on and off the grid lines on the plot, respectively.

Include full-sentence response. Include plots and graphs.

Consider the differential equation

(5) dydx = (e-x - y)(e-x + 2 + y).
  1. Plot a slope field for (5) for x and y between -10 and 10. Use the hold commands and drawode to plot at least two solution curves on this slope field, one of which passes through the point (2,3) (that is, x = 2 and y = 3). Paste your plot into your Word document.
  2. Considering how complicated differential equation (5) appears to be, why do you think we might want to plot a slope field?
Include full-sentence response. Include plots and graphs.

Suppose the differential equation

(6) dydx = x + 2y

arises as a model in a physics experiment.

Plot a slope field for (6) with x and y between -5 and 5. On this slope field, plot the solution curve passing through (0,-14). Then draw three more solution curves passing through points very close to (0,-14) on the same figure. Paste your plot into your Word document.

Using this plot, think about what would happen if the initial value in the problem were not exactly (0,-14). Would this greatly affect the solution of the differential equation?

Include full-sentence response. Include plots and graphs.

Now suppose that

(7) dydx = y - 1

arises as a model.

Plot a slope field for (7) with x and y between -6 and 6. Suppose our experiment reveals that the initial value is about (1,1). On your slope field, plot the solution curve passing through (1,1), and also plot several solution curves going through other points near (1,1). Paste your plot into your Word document.

If the initial value were not exactly (1,1), how would this affect the solution?

Modeling: Newton's Law of Cooling

We'll now look at a real-world application in which slope field plots will give us valuable information about the solutions to our differential equation. Newton's law of cooling models the temperature change of an object at a certain temperature when placed in a surrounding environment of a different temperature. The law can be stated as follows:

(8) dydt = k(A - y)

where y(t) is the temperature of the object in degrees Fahrenheit at time t in hours, and A and k are constants.

Include full-sentence response. Include plots and graphs.

Plot a slope field for (8) with A = 1, k = 2, and where the minimum value of t is zero (since we are not interested in negative times here). You can choose an appropriate maximum value for t and minimum and maximum values for y. Include the slope field in your Word document. Now plot some slope fields for other values of A, and graph a few solution curves on those slope fields using drawode by choosing some initial values. What property do you think A represents in real life? [Hint: Think about the temperature at which the solutions stabilize.]

Include full-sentence response.

Let us try to figure out how long it will take to defrost a frozen chicken breast in the fridge, which keeps a constant temperature of 41°F. The chicken breast has been in the freezer for a while, so its temperature is uniform at -6°F. We'll suppose k = 0.4, based on the properties of the chicken.

  1. Recall that an initial value problem consists of a differential equation along with an initial condition. Write out the initial value problem that we must solve here. (We already have the differential equation, so this means you need to find the appropriate initial condition.)
  2. To simulate the conditions in the fridge, we must pick the parameter A. What do you think the value of A should be?
  3. Let us consider the chicken breast fully defrosted when the temperature reaches 39°F. How long does it take to defrost a chicken breast under the above conditions? A rough estimate from a slope field plot is sufficient. [Hint: You may need to adjust the axes on your plots.]
  4. How much time would be saved if the chicken breast were thawed on the kitchen counter instead, given that room temperature is around 69°F?

Systems of ODEs

So far we have examined differential equations of the form

dx(t)dt = f(x(t)).

For example, if f(x) = 2x + 3, then this equation says x′(t) = 2x(t) + 3. A system of differential equations involves several equations that tie together one or more variables. For example, a 3×3 system of first-order differential equations

dx(t)dt = f1(x(t), y(t), z(t))
dy(t)dt = f2(x(t), y(t), z(t))
dz(t)dt = f3(x(t), y(t), z(t))

constrains the functions x(t), y(t), z(t). A specific example is given by:

(9) dx(t)dt = 2x(t) + 3y(t) - 4z(t)
dy(t)dt = -3x(t) - y(t) + 2z(t)
dz(t)dt = x(t) + y(t) - z(t)

Here, f1(x, y, z) = 2x + 3y - 4z, f2(x, y, z) = -3x - y +2z, and f3(x, y, z) = x + y - z.

Mathematical models like Newton's laws produce systems like these, and the initial conditions (x(0), y(0), z(0)) = (x0, y0, z0) are determined by whatever data you measure at t = 0. Systems with hundreds of equations or more are commonplace in engineering, biochemistry, and most types of technology. One major conceptual point is that such a system produces a slope field (in this case in three dimensions). Then the solutions can be seen as curves, with very similar properties to what you saw with slopefield for 2×2 systems. The course has not yet covered systems of equations, but don't worry! Our goal in these labs is to give you a taste of what a system of differential equations is, a few examples, and an idea where the course is going.

A two-dimensional first-order system is a pair of differential equations of the form

dx(t)dt = f1(x(t), y(t))
dy(t)dt = f2(x(t), y(t))

with an initial condition (x(0), y(0)) = (x0, y0). Under conditions on f1 and f2 (namely, that each is continuous and differentiable), specifying (x0, y0) determines the solutions (x(t), y(t)) uniquely—that is, there can be no other functions (x(t), y(t)) that satisfy both differential equations and also satisfy the initial conditions (x(0), (0)) = (x0, y0).

Conclusion

In this lab, we have seen that plots of slope fields can be useful when trying to understand the behavior of solutions of ordinary differential equations. This is particularly important when we cannot solve the equation analytically. We have also seen how useful some relatively simple MATLAB M-Files can be in generating slope fields and plots of solutions. It is important to understand that these techniques do not in any obvious way scale to systems of hundreds of equations.