Math 449 - Homework 7 - Solutions 1) Exercise 2 (b), section 5.1, page 259. The data points are: x =[-6 -2 0 2 6] y =[-5.3000 -3.5000 -1.7000 0.2000 4.0000] I will use the notation e = [1 1 1 1 1] and u.v for the dot product of two vectors u and v. The constants A and B for the least squares line y = Ax + B satisfy the linear system x.x A + x.e B = x.y x.e A + 5 B = y.e From the above data we obtain the numerical values for the coefficients: x.x = 80, x.e = 0, x.y = 63.2, y.e = -6.3 This immediately yields 80 A = 63.2 and 5 B = -6.3, or A = 0.79, B = -1.26 Therefore, the least squares line is y = 0.79 x - 1.26 The error E_2(f), using Matlab notation, is given by E_2(f) = sqrt((1/5)*sum((A*x+B - y).^2)) = 0.5299 ************************************************************************** 2) Exercise 8 (a), section 5.1, page 261. We want to find the power fits y = A x^2 and Y = B x^3 for the data: x = [2.0000 2.3000 2.6000 2.9000 3.2000] y = [5.1000 7.5000 10.6000 14.4000 19.0000] and find the best fit based on the error E_2(f). A and B are given by A = sum(x.^2.*y)/sum(x.^4) = 1.6866 B = sum(x.^3.*y)/sum(x.^6) = 0.5902 The respective errors are E_2(Ax^2) = sqrt((1/5)*sum((A*x.^2 - y).^2)) = 1.2971 E_2(Bx^3) = sqrt((1/5)*sum((B*x.^3 - y).^2)) = 0.2870 Therefore, the cubic polynomial is a better fit for the data. ************************************************************************** 3) Algorithms and programs 2 (a), section 5.1, page 262. We wish to estimate the gravitational constant g from the following data time, t = [0.2000 0.4000 0.6000 0.8000 1.0000] (in seconds) distance, d = [0.1960 0.7835 1.7630 3.1345 4.8975] (in meters) These variables are related by the formula : d = (1/2)*g*t^2 We set (1/2)*g = A and use the expression for A that obtains best quadratic fit: A = sum(t.^2.*d)/sum(t.^4) = 4.8975. This gives g = 2*A = 9.7950 m/sec^2. ************************************************************************** 4) Exercise 2 (b), section 5.2, page 275. We wish to find the least squares parabola y = A x^2 + B x + C for the data set: x = [-2 -1 0 1 2] y = [2.8000 2.1000 3.2500 6.0000 11.5000] The values of A, B, C are obtained by solving the linear system L*[A B C]'= Y, where Y = [sum(y.*x.^2) sum(y.*x) sum(y)]' = [65.3000 21.3000 25.6500]' and L is the symmetric matrix whose entries are: L(1,1) = sum(x.^4) = 34 L(1,2) = L(2,1) = sum(x.^3) = 0 L(1,3) = L(3,1) = L(2,2) = sum(x.^2) = 10 L(2,3) = L(3,2) = sum(x) = 0 L(3,3) = 5 This gives the system | 34 0 10||A| |65.30| | 0 10 0||B| = |21.30| | 10 0 5||C| |25.65| We obtain the solution: A = 1.0000, B = 2.1300, C = 3.1300. Therefore, the least squares parabola for the given data is y = x^2 + 2.13 x + 3.13 ************************************************************************** 5) Exercise 6 (a), section 5.2, page 276. We assume that a population size as function of time, is modeled by the expression P(t) = L/(1 + Ce^(At)). The value of L is assumed known, L = 1000. We wish to find A and C that give best fit for the data t = [0 1 2 3 4] P = [200 400 650 850 950] Set new coordinates and constants: y = log(1000/P - 1), x = t, B = log(C) x = [0 1 2 3 4] y = [1.3863 0.4055 -0.6190 -1.7346 -2.9444] Then A and B are obtained by standard linear least squares. Using the notations of the first problem above, the constants A and B for the least squares line y = Ax + B satisfy the linear system x.x A + x.e B = x.y x.e A + 5 B = y.e Evaluating the coefficients: 30 A + 10 B = -17.8142 10 B + 5 B = -3.5063 Solving the linear system yields: A = -1.0802, B = 1.4591. Thus C = exp(B) = 4.3019. Therefore, P(t) = 1000/(1 + 4.3019 exp(-1.0802 t)) ************************************************************************** 6) Exercise 7 (b), section 5.3, page 294. This is essentially the same as the previous problem, with the data set: t = [0 2 4 6 8] P = [76.1000 106.5000 132.6000 180.7000 226.5000] and L= 800 (million) We set x = t, y = log(800./P - 1), B=log(C). x = [0 2 4 6 8] y = [2.2526 1.8736 1.6161 1.2318 0.9290] The system of equations for A, B is: 120 A + 20 B = 25.0340 20 A + 5 B = 7.9030 This yields A = -0.1645, B = 2.2384, C = exp(B) = 9.3784. Therefore P(t) = 800/(1 + 9.3784*exp(-0.1645*t)) The year 2000 corresponds to t = 10. Then P(10) = 284.6824 million. (The US Census Bureau web site gives the number 281,421,906.) ************************************************************************** 7) Exercise 14, section 5.2, page 277. The coordinate change: X = x, Y = y^(-1/2) turns the equation y = (A x + B)^(-2) into the linear equation Y = A X + B. ************************************************************************** 8) Exercise 2 (a, b), section 5.3, page 294. We wish to find the cubic polynomial S(x) = a_0 + a_1x + a_2x^2 +a_3x^3 satisfying the conditions: S(1) = 3, S'(1) = -4, S(2) = 1, S'(2) = 2. These conditions immediately give the equations, respectively, a_0 + a_1 + a_2 + a_3 = 3 a_1 + 2a_2 + 3a_3 = -4 a_0 + 2a_1 + 4a_2 + 8a_3 = 1 a_1 + 4a_2 + 12a_3 = 2 The solution to this system is easily obtained (by hand or computer) to be [a_0 a_1 a_2 a_3] = [5 2 -6 2]. Therefore, S(x) = 5 + 2x -6x^2 + 2x^3. See graph on separate page. ************************************************************************** 9) Exercise 2, section 5.4, page 307. Find the Fourier series representation of the function f(x) defined by: f(x) = pi/2 + x on interval -pi <= x < 0 and f(x) = pi/2 - x on interval 0 <= x < pi and graph on the same coordinate system f(x) and the partial sums S_2(x), S_3(x), S_4(x). I will denote the integral of a function g(x) from a to b by the symbol Int_[a,b] g(x) dx. Since f(x) is an even function of period 2pi, we can use a cosine Fourier series of the form f(x) = a_0/2 + a_1cos(x) + a_2cos(2x) + ... The coefficients a_j are given by a_j = (2/pi)Int_[0 pi] f(x)*cos(jx) dx for j= 0, 1, 2, ... This is an elementary integral, whose value is a_j = 4/(j^2pi) if j is odd and a_j = 0 if j is even (including 0). Therefore, the Fourier series of f(x) is f(x) = (4/pi^2)(cos(x) + cos(3x)/3^2 + cos(5x)/5^2 + ...) See graphs on the separate page. (Note that S_2(x) = S_1(x) and S_4(3) = S_3(x).) ************************************************************************** 10) Exercise 7, section 5.4, page 307. It immediately follows from the previous problem, by substituting 0 for x, that pi/2 = (4/pi)(1 + 1/3^2 + 1/5^2 + 1/7^2 ...) Therefore, pi^2/8 = 1 + 1/3^2 + 1/5^2 + 1/7^2 + ... ************************************************************************** 11) Exercise 1 (b), section 5.4, page 308. Enter on the Matlab command line: k=1:12; X=-pi+2*pi*k/12; Y=(X<0).*(X + pi/2) + (X>=0).*(-X +pi/2); %function of exe. 2 sect. 5.4. M=5; %Degree of the trigonometric polynomial %Now, obtain A, B using the function tpcoeff (see below) [A, B]=tpcoeff(X,Y,M) %This produces the vectors of coefficients: %A = [0 1.4212 0.0000 0.1904 0.0000 0.1020] %The vector B is nearly zero. Therefore, the trigonometric polynomial has the form: T_5(x) = 1.4212*cos(x) + 0.1904*cos(3*x) + 0.1020*cos(5*x) %To plot the graphs, we do the following: x=-pi:.01:pi; y=tp(A,B,x,M); %See the function tp below. plot(x,y,X,Y,'--o') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [A,B]=tpcoeff(X,Y,M) %Input - X is a vector of equally spaced abscissas in [-pi, pi] % - Y is a vector of ordinates % - M is the degree of the trigonometric polynomial %Output - A is a vector containing the coefficients of cos(jx) % - B is a vector containing the coefficients of sin(jx) % N=length(X)-1; max1=fix((N-1)/2); if M>max1 M=max1; end A=zeros(1,M+1); B=zeros(1,M+1); A(1)=sum(Y); for j=1:M A(j+1)=cos(j*X)*Y'; B(j+1)=sin(j*X)*Y'; end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function z=tp(A,B,x,M) z=A(1); for j=1:M z=z+A(j+1)*cos(j*x)+B(j+1)*sin(j*x); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%