1 00:00:04,610 --> 00:00:06,380 PROFESSOR: The cost of a numerical method 2 00:00:06,380 --> 00:00:08,640 for solving ordinary differential equations is 3 00:00:08,640 --> 00:00:12,240 measured by the number of times it evaluates the function f 4 00:00:12,240 --> 00:00:13,570 per step. 5 00:00:13,570 --> 00:00:16,780 Euler's method evaluates f once per step. 6 00:00:16,780 --> 00:00:20,700 Here's a new method that evaluates it twice per step. 7 00:00:20,700 --> 00:00:23,820 If f is evaluated once at the beginning of the step 8 00:00:23,820 --> 00:00:26,900 to give a slope s1, and then s1 is 9 00:00:26,900 --> 00:00:31,240 used to take Euler's step halfway across the interval, 10 00:00:31,240 --> 00:00:33,820 the function is evaluated in the middle of the interval 11 00:00:33,820 --> 00:00:36,480 to give the slope s2. 12 00:00:36,480 --> 00:00:39,750 And then s2 is used to take the step. 13 00:00:39,750 --> 00:00:44,690 For obvious reasons, this is called the midpoint method. 14 00:00:44,690 --> 00:00:46,870 Here's ode2. 15 00:00:46,870 --> 00:00:49,390 It implements the midpoint method, 16 00:00:49,390 --> 00:00:53,760 evaluates the function twice per step. 17 00:00:53,760 --> 00:00:57,450 The structure is the same as ode1. 18 00:00:57,450 --> 00:01:02,870 Same arguments, same for loop, but now we 19 00:01:02,870 --> 00:01:07,900 have s1 at the beginning of the step, s2 20 00:01:07,900 --> 00:01:10,500 in the middle of the step, and then the step 21 00:01:10,500 --> 00:01:12,470 is actually taken with s2. 22 00:01:15,320 --> 00:01:19,270 Here's an example involving a trig function. 23 00:01:19,270 --> 00:01:23,700 Dy dt is the square root of 1, minus y squared. 24 00:01:23,700 --> 00:01:30,530 Starting at the origin on the interval from 0 to pi over 2. 25 00:01:30,530 --> 00:01:34,590 Now, because I've called it a trig example, 26 00:01:34,590 --> 00:01:39,170 you might just-- this is a separable equation-- 27 00:01:39,170 --> 00:01:43,480 do the integral, or you can just guess at the-- guess 28 00:01:43,480 --> 00:01:48,000 that the answer is sine t. 29 00:01:48,000 --> 00:01:52,910 Because the derivative of sine t is the cosine of t, 30 00:01:52,910 --> 00:01:57,530 and that's the square root of 1 minus y squared. 31 00:01:57,530 --> 00:02:00,770 Let's set it up. 32 00:02:00,770 --> 00:02:04,520 F is the anonymous function square 33 00:02:04,520 --> 00:02:07,720 root of 1 minus y squared. 34 00:02:07,720 --> 00:02:12,020 T0 is 0. 35 00:02:12,020 --> 00:02:16,260 I'm going to take h to be pi over 32. 36 00:02:16,260 --> 00:02:21,900 And tfinal is pi over 2. 37 00:02:21,900 --> 00:02:26,790 And y0 is 0. 38 00:02:26,790 --> 00:02:37,590 And here's my call to ode2, with these five arguments, 39 00:02:37,590 --> 00:02:40,440 and it produces this output. 40 00:02:40,440 --> 00:02:42,310 Now I want to plot it. 41 00:02:42,310 --> 00:02:47,150 Let's get t to go along with it. 42 00:02:47,150 --> 00:02:50,110 There is the t values as a column-- 43 00:02:50,110 --> 00:02:54,340 vector-- and let's plot. 44 00:02:59,570 --> 00:03:03,550 And do some annotation on the plot. 45 00:03:03,550 --> 00:03:05,490 Here's our plot. 46 00:03:05,490 --> 00:03:13,390 So there's the graph of our, there's the graph of sine t, 47 00:03:13,390 --> 00:03:14,885 the points generated by ode2. 48 00:03:23,060 --> 00:03:25,643 Now I can't help but go look at these answers. 49 00:03:28,190 --> 00:03:31,290 This is supposed to be the values of sine t. 50 00:03:31,290 --> 00:03:34,990 This should be getting to 1 at pi over 2. 51 00:03:34,990 --> 00:03:37,680 We've got 0.997. 52 00:03:37,680 --> 00:03:40,700 That gives you a rough idea of what kind of accuracy 53 00:03:40,700 --> 00:03:45,525 we're getting out of this crude numerical method. 54 00:03:48,690 --> 00:03:50,530 Let's take a look at an animation 55 00:03:50,530 --> 00:03:54,420 of the midpoint method. 56 00:03:54,420 --> 00:04:00,755 The differential equation is y prime is 2y, starting at t0 57 00:04:00,755 --> 00:04:07,620 equals 0 with a step size of 1, going up to 3, 58 00:04:07,620 --> 00:04:15,880 and starting with y0 equals 10, and using ode2. 59 00:04:15,880 --> 00:04:18,075 Here is the animation. 60 00:04:21,870 --> 00:04:26,800 Here's t0 and y0. 61 00:04:26,800 --> 00:04:28,960 Evaluate the function at y0. 62 00:04:28,960 --> 00:04:34,750 2 times y0 is 20, step halfway across the interval 63 00:04:34,750 --> 00:04:39,590 with that slope, that gets us to 20. 64 00:04:39,590 --> 00:04:41,860 Evaluate the function there, the slope 65 00:04:41,860 --> 00:04:45,670 is 40, so we take a step with slope 40 66 00:04:45,670 --> 00:04:49,190 all the way across the interval to get up to 50. 67 00:04:49,190 --> 00:04:51,250 That's the first step. 68 00:04:51,250 --> 00:04:55,410 Now we'll rescale the plot window. 69 00:04:55,410 --> 00:04:57,440 Here we are at 50. 70 00:04:57,440 --> 00:04:58,920 Evaluate the function there. 71 00:04:58,920 --> 00:05:03,500 The slope is 100, step halfway with that slope, 72 00:05:03,500 --> 00:05:04,920 get to the middle of the interval, 73 00:05:04,920 --> 00:05:06,550 evaluate the function there. 74 00:05:06,550 --> 00:05:11,460 The slope is 200, so we take a step with slope 200 75 00:05:11,460 --> 00:05:13,620 to get up to 250. 76 00:05:13,620 --> 00:05:15,600 That's the second step. 77 00:05:15,600 --> 00:05:18,000 Rescale the plot window. 78 00:05:18,000 --> 00:05:19,670 Evaluate the function there. 79 00:05:19,670 --> 00:05:21,830 The slope is 500. 80 00:05:21,830 --> 00:05:24,270 Take that step halfway across the interval, 81 00:05:24,270 --> 00:05:27,830 evaluate the slope there. 82 00:05:27,830 --> 00:05:33,930 The slope is 1,000, so we take a step with slope of 1,000 83 00:05:33,930 --> 00:05:37,890 to get up to 1,250 as our final value. 84 00:05:37,890 --> 00:05:41,630 Since this is a rapidly increasing function of y, 85 00:05:41,630 --> 00:05:45,480 the values we generate here with the midpoint method 86 00:05:45,480 --> 00:05:50,640 are far larger than the values generated with the Euler method 87 00:05:50,640 --> 00:05:52,130 that we saw with ode1. 88 00:05:55,760 --> 00:05:58,350 Here's an exercise. 89 00:05:58,350 --> 00:06:05,900 Modify ode2, creating ode2t, which implements the companion 90 00:06:05,900 --> 00:06:09,530 method, the trapezoid method. 91 00:06:09,530 --> 00:06:13,130 Evaluate the function at the beginning 92 00:06:13,130 --> 00:06:16,180 of the interval to get s1. 93 00:06:16,180 --> 00:06:20,140 Use s1 to go all the way across the interval. 94 00:06:20,140 --> 00:06:24,360 Evaluate the function at the right-hand endpoint 95 00:06:24,360 --> 00:06:27,560 of the interval to get s2. 96 00:06:27,560 --> 00:06:34,900 And then, use the average of s1 and s2 to take the step. 97 00:06:34,900 --> 00:06:37,640 That's the trapezoid method.