University of Waterloo

Faculty of Engineering

Department of Electrical and Computer Engineering

ECE 204A

Pre-Laboratory 9

Prepared by

Surname/Last Name, Legal Given/First Name(s)

UW Student ID Number: 2NNNNNNN

UW User ID: uwuserid @uwaterloo.ca

2A Electrical/Computer Engineering

18 September 2015

9.1a Implement the Runge-Kutta function described on the laboratory web site. Enter that implementation here.

function [ts, ys] = rk4( f, t_rng, y0, n )

Enter your function here

end

where trng = [t0, tf].

9.1b Using format long, demonstrate that the error drops approximately according to h4 by using the initial-value problem

where . The solution to this initial-value problem is the function (however, in general, we would not have the actual solution). Use n = 33, 65, and 129 points to approximate y(3) and calculate the absolute error for the approximation. Recall that the 33rd, 65th and 129th entries (ys(end) of each) of the three output vectors will approximate y(3). Fill in the Table 9.1b.

Table 9.1b. The absolute errors the approximation using n = 2k intervals as compared to the previous absolute error divided by 16 = 24.

k / n = 2k + 1 / /
5 / 33 / e5=abs(ys(end)-2*exp(-3)) / N/A
6 / 65 / e6=abs(ys(end)-2*exp(-3)) / e5/16
7 / 129 / e7=abs(ys(end)-2*exp(-3)) / e6/16

9.1c Consider the same initial-value problem:

Suppose we approximate y(0.1) with one step each of both Euler’s method and Heun’s method: what are your two approximations? Assign the approximation for Euler’s method to y1 and the approximation using Heun’s method to z1.

Your answers here.

Both of these approximations are, of course, approximating the same value which in this case is 2e–0.1. What is the absolute error of each approximation?

Your answer here.

What is the relative error of |y1 – z1| as an approximation to the absolute error of the approximation y1 found using Euler’s method? (Your answer should be somewhere close to 3 %).

Your answer here.

By comparing the difference between y1 and z1, could one estimate the error for Euler’s method. Recall that Euler’s method is O(h2) and therefore we may write the error, as a first approximation as

where c is the coefficient from the Taylor series. If we scale h by some positive scaling factor s, we can modify the error:

.

That is, if we halve h (s = 0.5), the error of the approximation drops by one quarter; while if we double h (s = 2), the error of the approximation increases by four. Suppose we are willing to accept some maximum error, say eabs. However, we should scale this error to the relative size of the interval: .

If our value of h is too small, we may have to take too many steps (cost) and we may get a better approximation than we require. Thus, it would make sense to make h larger (fewer steps). Alternatively, if our error is too large, we may need to make h smaller and try again. What we really want is to scale h to find an optimal value:

.

To ensure we satisfy this, we will simply use

.

Using Euler’s and Heun’s methods together, we have at this point:

.

Now, substitute into and this should give you an expression with s, |y1 – z1|, h, and eabs. Solve this for s.

Now, suppose that h = 0.1, = 0.001, y1 = 5.234238372 and z1 = 5.258057193. What is the value of s and therefore what is the new value of sh that you should have used for your step?

Your answer here with an explanation.

Now, suppose that h = 0.1, = 0.001, y1 = 5.258153162 and z1 = 5.258057193. What is the value of s and therefore what is the new value of sh that you should have used for your step?

Your answer here with an explanation.

9.2a Find the best-fitting line (using linear regression) which passes through the points

x1 = [1.3862 1.4929 2.5751 5.4722 6.9908 8.4072 8.9090 9.5929]';

y1 = [4.6006 4.8822 5.4627 6.9211 7.9681 8.7056 9.3139 9.4706]';

Plot both the eight points and a plot of the best-fitting straight line you found using the polyval function. Use an appropriate interval slightly larger than the x-values of the points which were found.

Figure 1. A best-fitting line.

9.2b Find the best-fitting line (using linear regression) which passes through the points

x2 = [1.3862 1.4929 2.5751 5.4722 6.9908 8.4072 8.9090 9.5929]';

y2 = [0.4191 1.1449 4.5667 0.7619 4.1291 2.6917 4.9807 0.3909]';

Plot both the eight points and a plot of the best-fitting straight line you found using the polyval function. Use an appropriate interval slightly larger than the x-values of the points which were found.

Figure 2. A best-fitting line.

9.2c In Question 9.2b, is this the best-fitting line passing through these points?

Your response here.

9.2d Considering the data in Question 9.2b, just because you can fit a line to data, does that necessarily mean the line has any significance?

Your response here.

9.2e Find the best-fitting line which passes through the points

x3 = [1.3862 1.4929 2.5751 5.4722 6.9908 8.4072 8.9090 9.5929]';

y3 = [3.5101 6.6099 2.1850 -16.1321 -33.2007 -47.7779 -55.9318 -67.5378]';

Plot both the eight points and a plot of the best-fitting straight line you found using the polyval function. Use an appropriate interval slightly larger than the x-values of the points which were found.

Figure 3. A best-fitting line.

Now, find the best-fitting quadratic polynomial which passes through the eight points. Plot both the eight points and a plot of the best-fitting quadratic polynomial you found using the polyval function. Use an appropriate interval slightly larger than the x-values of the points which were found.

Figure 4. A best-fitting line.

6