Docsity

Numerical Computing - Solved Homework 5 Questions | MATH 4800, Assignments of Mathematics

Toggle sidebar
bg1
Numerical Computing Summer ’10 MATH/CSCI 4800-01
Homework-5
Assigned Monday August 2, 2010
Due Tuesday August 10, 2010
PROBLEMS
1. (Pencil-and-paper) Text page 204, Exercises 4.1, Problem 8(a). Show that both the calculus and
the algebraic approaches discussed in class yield equivalent sets of normal equations. Then find the
best linear fit and the RMSE. The data points are (0,0),(1,3),(2,3),(5,6).Let the linear fit be
f(x) = c1+c2x. Then the squared error is given by
E=
4
X
i=1
[f(xi)−yi]2=
4
X
i=1
[c1+c2xi−yi]2.
In the calculus approach we seek to minimize the error by setting ∂E/∂c1and ∂E/∂c2to zero. This,
following elementary manipulations, yields the normal equations
4
X
i=1
[c1+c2xi−yi]=0,
4
X
i=1
[c1+c2xi−yi]xi= 0.
These can be rewritten as
4c1+ 4
X
i=1
xi!c2=
4
X
i=1
yi,
4
X
i=1
xi!c1+ 4
X
i=1
x2
i!c2=
4
X
i=1
xiyi.
For the data at hand these reduce further to
4c1+ +8c2= 12,
8c1+ 30c2= 39.
The solution is c1= 6/7, c2= 15/14,leading to the fit
f(x) = 6
7+15
14x.
From the expression above the squared error is found to be E= 1.92857.Then the RMSE is given by
RMSE = rE
4= 0.69437.
The algebraic approach would begin with the linear system Ac =y,obtained by assuming that the
function f(x) interpolates the data. Then,
A=
1 0
1 1
1 2
1 5
,c=c1
c2,y=
0
3
3
6
.
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Numerical Computing - Solved Homework 5 Questions | MATH 4800 and more Assignments Mathematics in PDF only on Docsity!

Numerical Computing Summer ’10 MATH/CSCI 4800- Homework-

Assigned Monday August 2, 2010 Due Tuesday August 10, 2010

PROBLEMS

  1. (Pencil-and-paper) Text page 204, Exercises 4.1, Problem 8(a). Show that both the calculus and the algebraic approaches discussed in class yield equivalent sets of normal equations. Then find the best linear fit and the RMSE. The data points are (0, 0), (1, 3), (2, 3), (5, 6). Let the linear fit be f (x) = c 1 + c 2 x. Then the squared error is given by

E =

∑^4

i=

[f (xi) − yi]^2 =

∑^4

i=

[c 1 + c 2 xi − yi]^2.

In the calculus approach we seek to minimize the error by setting ∂E/∂c 1 and ∂E/∂c 2 to zero. This, following elementary manipulations, yields the normal equations

∑^4

i=

[c 1 + c 2 xi − yi] = 0 ,

∑^4

i=

[c 1 + c 2 xi − yi]xi = 0.

These can be rewritten as

4 c 1 +

i=

xi

c 2 =

∑^4

i=

yi, ( (^4) ∑

i=

xi

c 1 +

i=

x^2 i

c 2 =

∑^4

i=

xiyi.

For the data at hand these reduce further to

4 c 1 + +8c 2 = 12 , 8 c 1 + 30c 2 = 39.

The solution is c 1 = 6/ 7 , c 2 = 15/ 14 , leading to the fit

f (x) =

x.

From the expression above the squared error is found to be E = 1. 92857. Then the RMSE is given by

RMSE =

E

The algebraic approach would begin with the linear system Ac = y, obtained by assuming that the function f (x) interpolates the data. Then,

A =

 ,^ c^ =

[

c 1 c 2

]

, y =

Of course this system is overdetermined (four equations and only two unknowns) and has no solution in general. But as we discussed in class, it is rendered square by premultiplication with AT^ , thereby leading to the normal equations AT^ Ac = AT^ y, i.e.,

[

]

[

c 1 c 2

]

[

]

These simplify to (^) [ 4 8 8 30

] [

c 1 c 2

]

[

]

which are the same as found above by the calculus approach.

  1. (MATLAB) Consider the following experimental data:

1 3. 00 2 2. 50 3 3. 46 4 3. 19 5 2. 26 6 3. 45 7 3. 49 8 2. 00 9 3. 32 10 3. 92 11 1. 76

(a) Suppose that the function f (x) = a + b sin x + c sin 2x is selected to fit these data in the least squares sense. Set up a MATLAB code to (i) find the coefficients a, b, and c, and (ii) plot the fit and the data on a well-labelled graph. Comment on the quality of the fit in capturing the trend of the data. (b) Suggest another function g(x) that may better capture the trend of the data. Then use your code to (i) find the coefficients of g, and (ii) plot f, g and the data on a single, well-labelled graph. Comment on whether g fulfilled your expectations.

We shall use the algebraic approach. That begins by first writing down the interpolation conditions, which are c 1 + c 2 sin xi + c 3 sin 2xi = yi, i = 1, 2 ,... n. (We have changed the notation slightly by replacing a, b, c with c 1 , c 2 , c 3 respectively.) Here, n = 11. These 11 equations can be written in the matrix form Ac = y, where A is an 11 × 3 matrix, y is an 11 × 1 vector, and

c =

c 1 c 2 c 3

is a 3 × 1 vector. The solution would then be given by the normal equations

AT^ Ac = AT^ y.

As discussed in class, the MATLAB solution to the normal equations can be obtained most simply by applying the backslash function directly to the overdetermined system Ac = y. The corresponding MATLAB script and results for the vector c are given below, along with the graphs of the data and the fit. The graph shows the data to have an oscillatory character, with a frequency of approximately three units and with amplitude growing approximately linearly. While the assumed fit captures the

x

x

Figure 1: Left: Data and the original option fit. Right: Data and the modified option fit. The modified option does a better job of capturing data trends.

  1. (Pencil-and-paper) Text page 251, Exercises 5.1, Problems 8 and 10(a, b). Hint: Taylor series will be useful.

(a) We observe that he functions f (x + h) and f (x − 2 h) appear in the approximation formula. We begin by expanding these in Taylor series about h = 0. Thus we have

f (x + h) = f (x) + hf ′(x) +

h^2 2!

f ′′(x) +

h^3 3!

f ′′′(c 1 ),

f (x − 2 h) = f (x) − 2 hf ′(x) +

4 h^2 2! f ′′(x) −

8 h^3 3! f ′′′(c 2 ).

Here, c 1 lies between x and x + h while c 2 lies between x − 2 h and x. We then combine these series to get

4 f (x + h) − f (x − 2 h) = 3f (x) + 6hf ′(x) + 4 h^3 3!

[f ′′′(c 1 ) + 2f ′′′(c 2 )],

which can be rearranged as

f ′(x) =

4 f (x + h) − f (x − 2 h) − 3 f (x) 6 h

+ T,

where the truncation error T is given by

T = −

h^2 9

[f ′′′(c 1 ) + 2f ′′′(c 2 )].

By using the generalized intermediate-value theorem the truncation error can be expressed in the simpler form T = −

h^2 3 f ′′′(c) where c now lies between x − 2 h and x + h. The power of h in the truncation error defines the order of the approximation formula for f ′(x); it is of second order. (b) We have f (x) = 1/x. The forward difference formula is

f ′(x) ≈ f (x + h) − f (x) h = 1 /(x + h) − 1 /x h = −

x(x + h)

The exact expression is f ′(x) = −

x^2

Therefore the error (exact -approximate) is

x^2

x(x + h)

h x^2 (x + h)

For small h, the error is approximately proportional to h.

  1. (Pencil-and-paper) Consider the following open Newton-Cotes quadrature rule with three nodes (n =
    1. : ∫ (^4) h

0

f (x) dx ≈ Q 3 (f ) =

4 h 3

[2f (h) − f (2h) + 2f (3h)].

(a) Verify that this formula has degree of precision 3. (b) Derive the error term associated with this quadrature rule.

(a) To verify that the degree of precision is 3, we need to show that the quadrature formula is exact for f (x) = x^3 but is not exact for f (x) = x^4. For f (x) = x^3 ,

4 h 3

[2f (h) − f (2h) + 2f (3h)] =

4 h 3

[2h^3 − 8 h^3 + 54h^3 ]

= 64 h^4.

Also, ∫ (^4) h

0

x^3 dx = 64h^4.

Thus the formula is exact. For f (x) = x^4 ,

4 h 3

[2f (h) − f (2h) + 2f (3h)] =

4 h 3

[2h^4 − 16 h^4 + 162h^4 ]

=

h^5 ,

whereas (^) ∫ 4 h

0

x^4 dx =

1024 h^5 5

Thus the formula is not exact, so that the degree of precision is 3. (b) If the degree of precision is 3, then the error is proportional to the fourth derivative, i.e., ∫ (^4) h

0

f (x) dx =

4 h 3

[2f (h) − f (2h) + 2f (3h)] + Kf iv^ (c),

where K is a constant to be found and c lies between 0 and 4 h. Applying the above to f (x) = x^4 and using the computations of part (a) above, one obtains

1024 h^5 5

h^5 + 24K

Therefore K =

h^5 ,

and hence the error term is 14 45

h^5 f iv^ (c).

We begin by applying Simpson’s rule to the full interval, first with h = 1/2 and then in a composite fashion with h = 1/ 4. Here are the results and the error estimate.

S(0, 1) =

[f (0) + 4f (1/2) + f (1)] = 7. 8333 e − 01

S¯(0, 1) = 1 12

[f (0) + 4f (1/4) + 2f (1/2) + 4f (3/4) + f (1)] = 7. 8539 e − 01

|E(0, 1)| =

| S¯ − S| =

| S¯ − S| = 1. 3725 e − 04 < δ.

Since the error tolerance is already met, we accept S¯(0, 1) and are done. Let us, however, make the tolerance smaller, say δ = 10−^5. Then the above result is not sufficiently accurate and we need to continue. We divide the interval (0, 1) into two panels of equal width. On each, the acceptable tolerance is δ/2 = 0. 5 × 10 −^5.

(a) Panels (0, 1 /2) and (1/ 2 , 1).

S(1/ 2 , 1) =

[f (1/2) + 4f (3/4) + f (1)] = 3. 2167 e − 01

S¯(1/ 2 , 1) = 1 24

[f (1/2) + 4f (5/8) + 2f (3/4) + 4f (7/8) + f (1)] = 3. 2175 e − 01

|E(1/ 2 , 1)| =

| S¯ − S| =

| S¯ − S| = 5. 2534 e − 06 < δ/ 2.

Since tolerance is satisfied, accept S¯(1/ 2 , 1). (b) Similarly,

S(0, 1 /2) =

[f (0) + 4f (1/4) + f (1/2)] = 4. 6373 e − 01

S¯(0, 1 /2) = 1 24

[f (0) + 4f (1/8) + 2f (1/4) + 4f (3/8) + f (1/2)] = 4. 6365 e − 01

|E(0, 1 /2)| =

| S¯ − S| = 4. 8555 e − 06 < δ/ 2.

Since tolerance is satisfied, accept S¯(0, 1 /2). Result: I = S¯(0, 1 /2) + S¯(1/ 2 , 1) = 7. 8540 e − 01. Iexact = π/4 = 7. 8540 e − 01. To 5 decimal places, the approximate and the exact solutions agree.

  1. (Pencil-and-paper and MATLAB) It can be shown that the period T of a pendulum with length L and maximum angle of deflection θ 0 is given by

T = 4

L

g

∫ (^) π/ 2

0

1 − k^2 sin^2 x

dx,

where k = sin θ 0 and g is the acceleration due to gravity. For L = 1 m and θ 0 = 10◦, find T to within an absolute error of 10−^6 seconds. Take g = 9.81 m/s. Use a code of your own making, without using any MATLAB built-in functions for integration. Be sure to justify why you believe your answer satisfies the prescribed tolerance. We use adaptive quadrature using Simpson’s rule. The MATLAB code given below is taken from the text (page 271), with suitable modifications. The result of this code is compared with that of the MATLAB quad function computed with the stronger tolerance of 10 −^7 , and deemed to be ‘exact.’ The two results are found to agree to 9 digits.

% Script File HW5P7.m % Adaptive Quadrature using Simpson’s rule % Specify function and limits of integration clc clear all % Specify function parameters L=1;k=sin(pi/18);g=9.81; % Specify function f=@(x) 4sqrt(L/g)./sqrt(1-k^2(sin(x)).^2); a0=0; b0=pi/2; % Specify tolerance tol0=0.5e-6; % Begin adaptive procedure sum=0; n=1; a(1)=a0; b(1)=b0; tol(1)=tol0; app(1)=simp(f,a,b); while n> c=(a(n)+b(n))/2; oldapp=app(n); app(n)=simp(f,a(n),c); app(n+1)=simp(f,c,b(n)); if abs(oldapp-(app(n)+app(n+1)))<15*tol(n) sum=sum+app(n)+app(n+1); % success n=n-1; % done with interval else b(n+1)=b(n);b(n)=c; % set up new intervals a(n+1)=c; tol(n)=tol(n)/2; tol(n+1)=tol(n); n=n+1; % go to end of list, repeat end end % "Exact" solution using quad exact=quad(f,a0,b0,tol0/10); % Print result fprintf(’Numerical = %12.8e, Exact = %12.8e \n’, sum, exact); %%%%%%%%%%% NUMERICAL OUTPUT Numerical = 2.02145126e+00, Exact = 2.02145126e+