You are on page 1of 7

Numerical analysis 8/06/22

Author: Huỳnh Lê Nhật Linh Report


§ Heun’s method §

Exercise 1: Redo the example 25.5 in the textbook


(Steven C.Chapra - Raymond P. Canala -
Numerical methods for engineers)

(1) Problem Statement: Use Heun’s method to integrate y′ = 4e0.8x − 0.5y from x = 0
to x = 4 with a step size of 1. The initial condition at x = 0 is y = 2

(2) Solution Before solving the problem numerically, we can use calculus to determine the
following analytical solution:
4
e0.8x − e−0.5x + 2e−0.5x

y=
1.3
This formula can be used to generate the true solution values in Table.
First, the slope at (x0 , y0 ) is calculated as
y0′ = 4e0 − 0.5(2) = 3
This result is quite different from the actual average slope for the interval from 0 to 1.0, which
is equal to 4.1946, as calculated from the differential equations.
The numerical solution is obtained by using the predictor to obtain an estimate of y at 1.0:
y10 = 2 + 3(1) = 5
Iterations of Heun’s Method
1 15
xi ytrue yHeun |ϵt | yHeun |ϵt |
0 2 2 0 2 0
1 6.7011 6.1946 0.0818 6.1946 0.0268
2 16.3198 14.8439 0.0994 14.8439 0.0309
3 37.1992 33.6772 0.1046 33.6772 0.0317
4 83.3378 75.3390 0.1062 75.3390 0.0318

Exercise 2: Try to do the assignment with different val-


ues of step size; h = 1, h = 0.5, h = 0.25
We using Matlab to compute the value table
Case h = 1

This report is due and the version of the problem set is 8/06/22.
LATEX template for this report is SEU-ML-Assign open source at tvj.one/ml-tex under the MIT License.
E-mail me@tvj.one for support.
2 Numerical analysis Report

xi ytrue yHeun |ϵt |


0 2 2 0
1 6.701082 6.194631 8.175635
2 16.31978 14.84392 9.942521
3 37.19925 33.67717 10.45835
4 83.33777 75.33896 10.61709
Case h = 1
xi ytrue yHeun |ϵt |
0 2 2 0
0.5 3.804325 3.751521 1.40752
1 6.316538 6.194631 1.967942
1.5 9.924068 9.707042 2.235759
2 15.1963 14.84392 2.373876
2.5 22.97594 22.42701 2.447606
3 34.51492 33.67717 2.487586
3.5 51.67681 50.41177 2.509411
4 77.23852 75.33896 2.521353
case h = 0.125
xi ytrue yHeun |ϵt |
0 2 2 0
0.25 2.813826 2.807781 0.215306
0.5 3.764357 3.751521 0.342151
0.75 4.886954 4.866362 0.423161
1 6.224212 6.194631 0.477516
1.25 7.827627 7.787509 0.515162
1.5 9.759634 9.707042 0.54179
1.75 12.09608 12.02861 0.560894
2 14.92923 14.84392 0.574733
2.25 18.37149 18.26467 0.584825
2.5 22.55983 22.42701 0.592218
2.75 27.66132 27.49698 0.597649
3 33.87979 33.67717 0.601648
3.25 41.46401 41.21483 0.604596
3.5 50.71766 50.41177 0.60677
3.75 62.01148 61.6365 0.608375
4 75.7982 75.33896 0.60956
We have the plot following:
Numerical analysis Report 3

Exercise 3: Plot the absolute percent relative global er-


ror at x = 4 as a function of step size
ytrue − ypredicted
percent relative error = 100%
ytrue
4 Numerical analysis Report

Exercise 4: Learn how to use switch command to make


the computation using Heun’s method with
different number of iteration (1,15,30)

Iterations of Heun’s Method


1 15 30
xi ytrue yheun |ϵt |% yheun |ϵt |% yheun |ϵt |%
0 2 2 0 2 0.00000000000 2 0
1 6.19463 6.70108 8.17564 6.3608654855867 2.68351897401 6.360865485588 2.68351897403
2 14.84392 16.31978 9.94252 15.3022366559692 3.08755833650 15.302236655973 3.08755833652
3 33.67717 37.19925 10.45835 34.7432760816337 3.16565868717 34.743276081643 3.16565868720
4 75.33896 83.33777 10.61709 77.7350961733669 3.18047061072 77.735096173387 3.18047061075

Exercise 5: Try to use Heun’s method to solve the ODE


problem in the previous lesson
Recall the previous example
dy
= −2x3 + 12x2 − 20x + 8.5
dx
Exac solution:
y = −0.5x4 + 4x3 − 10x2 + 8.58x + 1
from x = 0 to x = 4 with the step size of 0.5 and the initial condition y(0) = 1

(1) Make the comparison of the numerical solutions obtained by Eu-


ler’s method, Heun’s method to the exact solution, using the step
sizes 0.5, 0.25, 0.125.
Case: h = 0.5
xi ytrue yeuler yheun
0 1 1 1
0.5 3.21875 5.25 3.4375
1 3 5.875 3.375
1.5 2.21875 5.125 2.6875
2 2 4.5 2.5
2.5 2.71875 4.75 3.1875
3 4 5.875 4.375
3.5 4.71875 7.125 4.9375
4 3 7 3

Case: h = 0.25
Numerical analysis Report 5

xi ytrue yeuler yheun


0 1 1 1
0.25 2.5605 3.125 2.5898
0.5 3.2188 4.179688 3.2734
0.75 3.2793 4.492188 3.3555
1 3 4.34375 3.0938
1.25 2.5918 3.96875 2.6992
1.5 2.2188 3.554688 2.3359
1.75 1.9980 3.242188 2.1211
2 2 3.125 2.1250
2.25 2.2480 3.25 2.3711
2.5 2.7188 3.617188 2.8359
2.75 3.3418 4.179688 3.4492
3 4 4.84375 4.0938
3.25 4.5293 5.46875 4.6055
3.5 4.7188 5.867188 4.7734
3.75 4.3105 5.804688 4.3398
4 3 5 3

Case: h = 0.125

xi ytrue yeuler yheun


0 1 1 1
0.125 1.9139 2.0625 1.9177
0.25 2.5605 2.8354 2.5679
0.375 2.9823 3.3628 2.9929
0.5 3.2188 3.6855 3.2324
0.625 3.3065 3.8418 3.3230
0.75 3.2793 3.8667 3.2983
0.875 3.1678 3.7925 3.1892
1 3 3.6484 3.0234
1.125 2.8007 3.4609 2.8259
1.25 2.5918 3.2534 2.6187
1.375 2.3925 3.0464 2.4207
1.5 2.2188 2.8574 2.2480
1.625 2.0839 2.7012 2.1140
1.75 1.9980 2.5894 2.0288
1.875 1.9686 2.5308 1.9998
2 2 2.5313 2.0313
2.125 2.0936 2.5938 2.1248
2.25 2.2480 2.7183 2.2788
2.375 2.4589 2.9019 2.4890
2.5 2.7188 3.1387 2.7480
6 Numerical analysis Report

2.625 3.0175 3.4199 3.0457


2.75 3.3418 3.7339 3.3687
2.875 3.6757 4.0659 3.7009
3 4 4.3984 4.0234
3.125 4.2928 4.7109 4.3142
3.25 4.5293 4.9800 4.5483
3.375 4.6815 5.1792 4.6980
3.5 4.7188 5.2793 4.7324
3.625 4.6073 5.2480 4.6179
3.75 4.3105 5.0503 4.3179
3.875 3.7889 4.6479 3.7927
4 3 4 3

(2) Check the following behavior decreasing the step size decreases
the error at a fasster rate for Heun’s mthod than for Euler’s method
Numerical analysis Report 7

You might also like