Docsity

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

Toggle sidebar
bg1
Numerical Computing S’10 MATH/CSCI 4800-01
Homework-5
Outline of Solutions
PROBLEMS
1. (Pencil-and-paper and Matlab)
(a) Briefly describe the three methods we learned in class to find the normal equations for a least-
squares fit. Use a simple example as an illustration of all three.
(b) Solve Text Problem 2b, Exercises 4.1, page 204 by hand, using the procedure outlined in Example
4.4 on page 200.
(c) (Extra Credit) Solve the above problem by hand by using the QR decomposition. This will
require adding an extra column to the 4 ×3 matrix, employing Gram-Schmidt orthogonalization
to generate Qand R, and then using the procedure outlined on page 222 to solve the least-
squares problem.
(d) Solve the problem with Matlab by using the QR decomposition. You may find the code segment
given in Example 4.15 on page 223 helpful.
(a) Let us begin with the following example as an illustration. Consider the inconsistent system
Ax =bdefined below.
1 1
2 1
3 1
x1
x2=
1
2
0
.
We shall find the least-squares solution by using the three methods discussed in class.
i. The first method uses calculus to choose x1and x2in such a way that the least-squares
error is minimized. The error norm squared is
E=||Ax −b||2= (x1−x2−1)2+ (2x1+x2−2)2+ (3x1−x2)2.
On setting ∂E /∂x1=∂ E/∂ x2= 0 we obtain the normal equations
2(x1+x2−1) + 2 ×2(2x1+x2−2) + 2 ×3(3x1+x2)=0,
2(x1+x2−1) + 2(2x1+x2−2) + 2(3x1+x2)=0.
Simplification leads to
14x1+ 6x2= 5,
6x1+ 3x2= 3
whose solution is x1=−1/2, x2= 2.
ii. The second method provides the prescription ATAx =ATbfor the normal equations. For
the given problem it reduces to
123
111
1 1
2 1
3 1
x1
x2=123
111
1
2
0
,
or, to the same set of normal equations as found above.
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

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

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

Outline of Solutions

PROBLEMS

  1. (Pencil-and-paper and Matlab)

(a) Briefly describe the three methods we learned in class to find the normal equations for a least- squares fit. Use a simple example as an illustration of all three. (b) Solve Text Problem 2b, Exercises 4.1, page 204 by hand, using the procedure outlined in Example 4.4 on page 200. (c) (Extra Credit) Solve the above problem by hand by using the QR decomposition. This will require adding an extra column to the 4 × 3 matrix, employing Gram-Schmidt orthogonalization to generate Q and R, and then using the procedure outlined on page 222 to solve the least- squares problem. (d) Solve the problem with Matlab by using the QR decomposition. You may find the code segment given in Example 4.15 on page 223 helpful.

(a) Let us begin with the following example as an illustration. Consider the inconsistent system Ax = b defined below. (^) 

[

x 1 x 2

]

We shall find the least-squares solution by using the three methods discussed in class. i. The first method uses calculus to choose x 1 and x 2 in such a way that the least-squares error is minimized. The error norm squared is

E = ||Ax − b||^2 = (x 1 − x 2 − 1)^2 + (2x 1 + x 2 − 2)^2 + (3x 1 − x 2 )^2.

On setting ∂E/∂x 1 = ∂E/∂x 2 = 0 we obtain the normal equations

2(x 1 + x 2 − 1) + 2 × 2(2x 1 + x 2 − 2) + 2 × 3(3x 1 + x 2 ) = 0 , 2(x 1 + x 2 − 1) + 2(2x 1 + x 2 − 2) + 2(3x 1 + x 2 ) = 0.

Simplification leads to

14 x 1 + 6x 2 = 5 , 6 x 1 + 3x 2 = 3

whose solution is x 1 = − 1 / 2 , x 2 = 2. ii. The second method provides the prescription AT^ Ax = AT^ b for the normal equations. For the given problem it reduces to

[ 1 2 3 1 1 1

]

[

x 1 x 2

]

[

]

or, to the same set of normal equations as found above.

iii. The third method requires the QR -factorization of A. We follow the procedure outlined in class; see also section 4.3 and Examples 4.12-4.14 in the Text (pages 218-222). First, we need to add a third column to A, linearly independent of the other two columns so that a QR -factorization can be performed. Thus the enhanced version of A is taken to be

Aˆ =

We denote by vi the columns of A. The QR -factorization will be obtained via the Gram- Schmidt orthogonalization process. We begin by setting

y 1 = v 1 =

Then r 11 = ||y 1 || =

12 + 2^2 + 3^2 =

14 , and the first unit vector is

q 1 =

y 1 ||y 1 ||

To find the second unit vector we set

y 2 = v 2 − r 12 q 1

where

r 12 = (qT 1 v 2 ) =

[1 2 3]

 = √^6

Therefore,

y 2 =

 − √^6

 =^1

With r 22 = ||y 2 || =

(3/7), the second unit vector is

q 2 =

y 2 ||y 2 ||

For the third unit vector we define

y 3 = v 3 − r 13 q 1 − r 23 q 2

where

r 13 = (qT 1 v 3 ) =

[1 2 3]

 = √^1

and

r 23 = (qT 2 v 3 ) =

[4 1 − 2]

 = √^4

Therefore,

y 3 =

 − √^1

 − √^4

(c) Similar to the QR -decomposition procedure followed in part (a) above. (d) The Matlab script is given below, followed by the numerical output.

% Script File HW5P1d.m % Uses QR factorization to solve a least-squares problem clc % Provide matrices A and b % A=[1 0 1;1 0 2;1 1 1;2 1 1]; b=[2;3;1;2]; % % QR decomposition of A [Q,R]=qr(A); % command qr format short disp(’A’); disp (A); disp(’R’); disp (R); % Vector bhat bhat=Q’b; % Solve the 3x3 block of Rx=bhat x=R(1:3,1:3)\bhat(1:3); disp (’x’); disp(x); %%%%%%%%%%%%%%% NUMERICAL OUTPUT

A 1 0 1 1 0 2 1 1 1 2 1 1

R -2.6458 -1.1339 -2. 0 0.8452 -0. 0 0 -1. 0 0 0

x

-1.

  1. (Mostly Matlab) Solve Text Problem 6, Computer Problems 4.2, page 217. You may use the Matlab built-in functions to do so. The sample Matlab codes for least-squares fitting posted on line may be helpful.

The data points are as follows. t y 1 6. 2 2 9. 5 3 12. 3 4 13. 9 5 14. 6 6 13. 5 7 13. 3 8 12. 7 9 12. 4 10 11. 9

The fitting function is given to be f (t) = c 1 tec^2 t.

This function is nonlinear in the coefficients. A function linear in the coefficients is obtained by taking logarithms: ln f (t) = ln c 1 + ln t + c 2 t.

Upon renaming ln c 1 as k and c 2 as c we can write the above as

F (t) = ln f (t) − ln t = k + ct.

Thus we seek a linear fit F (t) = k + ct to the data (ti, ln yi − ln ti), i = 1, 2 , · · · , 10. Once k and c (and hence c 1 and c 2 ) are known, the corresponding nonlinear function f (t) is determined. From the fit, the time at maximum concentration is computed by setting f ′(t) = 0. We get

f ′(t) = c 1 (1 + c 2 t)ec^2 t^ = 0

whence

tpeak = −

c 2

The peak concentration is

ypeak = f (tpeak) = −

c 1 c 2 e

The concentration yh at half life is one-half the peak value, i.e.,

yh = 0. 5 f (tpeak).

The half-life is achieved at time th given by

yh = f (th).

This is a nonlinear equation for th which can be solved by one of a number of methods such as bisection, secant method, etc. We use the built-in Matlab command fzero.

The requisite Matlab script is given below.

% Script File HW5P2.m % Uses polyfit to fit polynomial to data % Uses fzero to solve nonlinear equations %

close all clc

% Provide data

Hours

Concentration

Data Fit Half−Peak line

Figure 1: Data and exponential fit for problem 2.

below 15, the upper therapeutic limit of concentration is never exceeded.

COMPUTED RESULTS

Fitting function is f(t)=c1texp(c2*t) c1= 7.1220e+00, c2= -1.8385e- peak concentration = 14.25 ng/ml halflife = 14.57 hours time interval of therapeutic value = [0.63,19.21] hours

  1. (Pencil-and-paper) Text: 6.1 Exercises, page 294, problems 4(a) and 6(a). The IVP is

y′(t) = t + y, y(0) = 0.

The equation is linear. The general solution of the homogeneous equation is cet. A particular solution of the nonhomogeneous equation is − 1 − t. Therefore the general solution of the nonhomogeneous equation is − 1 − t + cet. The initial condition determines c = 1. Therefore the IVP has the solution

y = − 1 − t + et.

The script given below computes the solution using Euler’s method. The output is given at the end, and includes the global error at t = 1 for h = 1/ 4.

% script calling00.m % % Solves IVP for an ODE % % Input:

% ODE particulars (f(t,y), a, b, alpha) % Number of subintervals n % % Output: % Exact solution y % Computed solution w % Global error |y(n) - w(n)| % Error ratio % % Calls Euler routine euler clc format=’ t w y |y-w| \n\n’; fprintf(format) a=0; T=1; % initial and final times alpha=0; % initial value n=4; % number of subintervals h=(T-a)/n; % step size

s=euler1(’ydot1’,a,T,alpha,n); for i=1:n+ tt=s(i,1); yexact=-1-tt+exp(tt); err=abs(yexact-s(i,2)); format=’%5.3f %15.6e %15.6e %15.6e \n’; fprintf(format,tt,s(i,2),yexact’,err’) end %%%%%%%%%%%%% function f=ydot1(t,y) f=t+y; %%%%%%%%%%%%% OUTPUT

t w y |y-w|

0.000 0.000000e+00 0.000000e+00 0.000000e+ 0.250 0.000000e+00 3.402542e-02 3.402542e- 0.500 6.250000e-02 1.487213e-01 8.622127e- 0.750 2.031250e-01 3.670000e-01 1.638750e- 1.000 4.414062e-01 7.182818e-01 2.768756e-

  1. (Matlab) Consider the IVP dy dt

= 2te−y^ , y(0) = 0.

(a) Find an exact solution to the IVP. (b) Apply Euler’s method to the IVP with step sizes h = 1/ 2 , 1 / 4 , 1 / 8 , 1 /16 and 1/32 on the interval [0, 5]. For each h plot the corresponding approximate solution along with the exact solution. (c) Plot the global error at t = 5 as a function of h for the h values used in part (b) above. (d) Repeat part (c) for the explicit trapezoidal method discussed in class and in the text. You may wish to use the log-log plot here. In what way does the dependence of the global error on h in this case differ from that for the Euler method?

function f=ydot(t,y) f=2t.exp(-y); %%%%%%%%%%%%%%%%%% function sol=euler1(fcn,a,b,alpha,N) % Generates Euler solution to y’=f(t,y) using N steps % Initial condition y(a)=alpha % Interval [a,b] h=(b-a)/N; t=a+(0:N)h; y(1)=alpha; for k=1:N y(k+1)=y(k)+hfeval(fcn,t(k),y(k)); end sol=[t’,y’]; %%%%%%%%%%%%%%%%% function sol=trapde(fcn,a,b,alpha,N) % Generates Explicit trapezoidal solution to y’=f(t,y) using N steps % Initial condition y(a)=alpha % Interval [a,b] h=(b-a)/N; t=a+(0:N)h; w(1)=alpha; for k=1:N f = feval(fcn,t(k),w(k)); f1 = feval(fcn,t(k)+h,w(k)+hf); w(k+1)=w(k)+0.5h(f + f1); end sol=[t’,w’]; %%%%%%%%%%%%%%%%% NUMERICAL OUTPUT

Euler calculation

h global error

0.50000 4.980098e- 0.25000 2.306111e- 0.12500 1.115544e- 0.06250 5.493838e- 0.03125 2.727150e- 0.01562 1.358781e-

Trapezoidal calculation

h global error

0.50000 6.243684e- 0.25000 1.757563e- 0.12500 4.516049e- 0.06250 1.140416e- 0.03125 2.863616e- 0.01562 7.173898e-

The graphs of global error for the two cases are given below. The error varies linearly with h for the Euler case and parabolically with h for the trapezoidal case. This is clear from the numerical output for the global error and also from the plots.

(^00) 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.

h

global error

(^00) 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.

1

2

3

4

5

6

7 x 10

−

h

global error

Figure 2: Graphs of global error vs. h. Left: Euler method. Right: Trapezoidal method.

  1. (Pencil-and-paper and Matlab)

(a) Convert the following second-order IVP

y′′^ + ty = 0, y(0) = y′(0) = 1

into an IVP for a pair of first-order equations for the variables y 1 and y 2 , where y 1 = y, y 2 = y′. (b) Apply the trapezoidal rule to the above IVP with h = 0.1 on the interval [0, 5]. Plot the computed solutions for y 1 and y 2 on a well-labelled graph with appropriate legends.

(a) Let

y 1 = y, y 2 = y′.

Then the IVP reduces to the the first-order system

y′ 1 = y 2 , y′ 2 = −ty 1 ,

with initial conditions y 1 (0) = y 2 (0) = 1.

(b) The Matlab script is given below.

% script callingtrap.m

% Solves IVP for ODE system y’(t) = f(t,y), y(a)=alpha % % Input: % ODE particulars (f(t,y), tspan, alpha) % Number of subintervals n % % Output: