WEBVTT

00:00:04.610 --> 00:00:06.380
PROFESSOR: The cost
of a numerical method

00:00:06.380 --> 00:00:08.640
for solving ordinary
differential equations is

00:00:08.640 --> 00:00:12.240
measured by the number of times
it evaluates the function f

00:00:12.240 --> 00:00:13.570
per step.

00:00:13.570 --> 00:00:16.780
Euler's method evaluates
f once per step.

00:00:16.780 --> 00:00:20.700
Here's a new method that
evaluates it twice per step.

00:00:20.700 --> 00:00:23.820
If f is evaluated once at
the beginning of the step

00:00:23.820 --> 00:00:26.900
to give a slope
s1, and then s1 is

00:00:26.900 --> 00:00:31.240
used to take Euler's step
halfway across the interval,

00:00:31.240 --> 00:00:33.820
the function is evaluated in
the middle of the interval

00:00:33.820 --> 00:00:36.480
to give the slope s2.

00:00:36.480 --> 00:00:39.750
And then s2 is used
to take the step.

00:00:39.750 --> 00:00:44.690
For obvious reasons, this is
called the midpoint method.

00:00:44.690 --> 00:00:46.870
Here's ode2.

00:00:46.870 --> 00:00:49.390
It implements the
midpoint method,

00:00:49.390 --> 00:00:53.760
evaluates the function
twice per step.

00:00:53.760 --> 00:00:57.450
The structure is
the same as ode1.

00:00:57.450 --> 00:01:02.870
Same arguments, same
for loop, but now we

00:01:02.870 --> 00:01:07.900
have s1 at the beginning
of the step, s2

00:01:07.900 --> 00:01:10.500
in the middle of the
step, and then the step

00:01:10.500 --> 00:01:12.470
is actually taken with s2.

00:01:15.320 --> 00:01:19.270
Here's an example
involving a trig function.

00:01:19.270 --> 00:01:23.700
Dy dt is the square root
of 1, minus y squared.

00:01:23.700 --> 00:01:30.530
Starting at the origin on the
interval from 0 to pi over 2.

00:01:30.530 --> 00:01:34.590
Now, because I've called
it a trig example,

00:01:34.590 --> 00:01:39.170
you might just-- this is
a separable equation--

00:01:39.170 --> 00:01:43.480
do the integral, or you can
just guess at the-- guess

00:01:43.480 --> 00:01:48.000
that the answer is sine t.

00:01:48.000 --> 00:01:52.910
Because the derivative of
sine t is the cosine of t,

00:01:52.910 --> 00:01:57.530
and that's the square
root of 1 minus y squared.

00:01:57.530 --> 00:02:00.770
Let's set it up.

00:02:00.770 --> 00:02:04.520
F is the anonymous
function square

00:02:04.520 --> 00:02:07.720
root of 1 minus y squared.

00:02:07.720 --> 00:02:12.020
T0 is 0.

00:02:12.020 --> 00:02:16.260
I'm going to take
h to be pi over 32.

00:02:16.260 --> 00:02:21.900
And tfinal is pi over 2.

00:02:21.900 --> 00:02:26.790
And y0 is 0.

00:02:26.790 --> 00:02:37.590
And here's my call to ode2,
with these five arguments,

00:02:37.590 --> 00:02:40.440
and it produces this output.

00:02:40.440 --> 00:02:42.310
Now I want to plot it.

00:02:42.310 --> 00:02:47.150
Let's get t to go along with it.

00:02:47.150 --> 00:02:50.110
There is the t
values as a column--

00:02:50.110 --> 00:02:54.340
vector-- and let's plot.

00:02:59.570 --> 00:03:03.550
And do some annotation
on the plot.

00:03:03.550 --> 00:03:05.490
Here's our plot.

00:03:05.490 --> 00:03:13.390
So there's the graph of our,
there's the graph of sine t,

00:03:13.390 --> 00:03:14.885
the points generated by ode2.

00:03:23.060 --> 00:03:25.643
Now I can't help but go
look at these answers.

00:03:28.190 --> 00:03:31.290
This is supposed to be
the values of sine t.

00:03:31.290 --> 00:03:34.990
This should be getting
to 1 at pi over 2.

00:03:34.990 --> 00:03:37.680
We've got 0.997.

00:03:37.680 --> 00:03:40.700
That gives you a rough idea
of what kind of accuracy

00:03:40.700 --> 00:03:45.525
we're getting out of this
crude numerical method.

00:03:48.690 --> 00:03:50.530
Let's take a look
at an animation

00:03:50.530 --> 00:03:54.420
of the midpoint method.

00:03:54.420 --> 00:04:00.755
The differential equation is
y prime is 2y, starting at t0

00:04:00.755 --> 00:04:07.620
equals 0 with a step
size of 1, going up to 3,

00:04:07.620 --> 00:04:15.880
and starting with y0
equals 10, and using ode2.

00:04:15.880 --> 00:04:18.075
Here is the animation.

00:04:21.870 --> 00:04:26.800
Here's t0 and y0.

00:04:26.800 --> 00:04:28.960
Evaluate the function at y0.

00:04:28.960 --> 00:04:34.750
2 times y0 is 20, step
halfway across the interval

00:04:34.750 --> 00:04:39.590
with that slope,
that gets us to 20.

00:04:39.590 --> 00:04:41.860
Evaluate the function
there, the slope

00:04:41.860 --> 00:04:45.670
is 40, so we take a
step with slope 40

00:04:45.670 --> 00:04:49.190
all the way across the
interval to get up to 50.

00:04:49.190 --> 00:04:51.250
That's the first step.

00:04:51.250 --> 00:04:55.410
Now we'll rescale
the plot window.

00:04:55.410 --> 00:04:57.440
Here we are at 50.

00:04:57.440 --> 00:04:58.920
Evaluate the function there.

00:04:58.920 --> 00:05:03.500
The slope is 100, step
halfway with that slope,

00:05:03.500 --> 00:05:04.920
get to the middle
of the interval,

00:05:04.920 --> 00:05:06.550
evaluate the function there.

00:05:06.550 --> 00:05:11.460
The slope is 200, so we
take a step with slope 200

00:05:11.460 --> 00:05:13.620
to get up to 250.

00:05:13.620 --> 00:05:15.600
That's the second step.

00:05:15.600 --> 00:05:18.000
Rescale the plot window.

00:05:18.000 --> 00:05:19.670
Evaluate the function there.

00:05:19.670 --> 00:05:21.830
The slope is 500.

00:05:21.830 --> 00:05:24.270
Take that step halfway
across the interval,

00:05:24.270 --> 00:05:27.830
evaluate the slope there.

00:05:27.830 --> 00:05:33.930
The slope is 1,000, so we take
a step with slope of 1,000

00:05:33.930 --> 00:05:37.890
to get up to 1,250
as our final value.

00:05:37.890 --> 00:05:41.630
Since this is a rapidly
increasing function of y,

00:05:41.630 --> 00:05:45.480
the values we generate here
with the midpoint method

00:05:45.480 --> 00:05:50.640
are far larger than the values
generated with the Euler method

00:05:50.640 --> 00:05:52.130
that we saw with ode1.

00:05:55.760 --> 00:05:58.350
Here's an exercise.

00:05:58.350 --> 00:06:05.900
Modify ode2, creating ode2t,
which implements the companion

00:06:05.900 --> 00:06:09.530
method, the trapezoid method.

00:06:09.530 --> 00:06:13.130
Evaluate the function
at the beginning

00:06:13.130 --> 00:06:16.180
of the interval to get s1.

00:06:16.180 --> 00:06:20.140
Use s1 to go all the
way across the interval.

00:06:20.140 --> 00:06:24.360
Evaluate the function at
the right-hand endpoint

00:06:24.360 --> 00:06:27.560
of the interval to get s2.

00:06:27.560 --> 00:06:34.900
And then, use the average of
s1 and s2 to take the step.

00:06:34.900 --> 00:06:37.640
That's the trapezoid method.