Spring Math 54 Homework 5 - Solutions BF 3.4.4. d. The spline interpolation routine below produces the following coefficients: i a i b i c i d i -..869948.75637848.656598 -.5.9589.487644.9847639.887.9863.34456976.489747 -.9393648 which gives the natural cubic spline: S () =.869948 +.75637848( + ) +.656598( + ) 3, [,.5], S () =.9589 +.487644( +.5) +.9847639( +.5) +.887( +.5) 3, [.5, ], S () =.9863 +.34456976 +.489747.9393648 3, [,.5]..3.5..5..5.95.9.85.5.5 Main Code function z = spline(n,, f) clear all load e344d n =length(f); for i = :n- h(i) = (i+)-(i); a = f; for i = :n-
alpha(i-) = (3./h(i)).*(a(i+)-a(i))-(3./h(i-)).*(a(i)-a(i-)); rhs = [ alpha ] ; for i = :n- l(i) = *(h(i)+h(i+)); T = [ h(:n-) ; l(:n-) ; h(:n-) ] ; c = trisolve(t, rhs); for j = :n- b(j) = (a(j+)-a(j))./h(j)-h(j)*(c(j+)+*c(j))./3; d(j) = (c(j+)-c(j))./(3*h(j)); y = min():.:ma(); Sy = evalspline(a,b,c,d,,y); plot(y, Sy, -,, f, o ) print -depsc spline_free.eps Evaluation Subroutine function S = evalspline(a,b,c,d,,y) Inputs: a-b-c-d the coefficients that define the spline are the node-points y the point(s) where you want to evaluate the spline function S = evalspline(a,b,c,d,,y) if( length(a) ~= length() ) error([ The first argument (a) must be the same length as the 5th... argument () ]); if( length(b) < length()- ) error([ The second argument (b) cannot be more then one element... shorter than the 5th argument () ]); if( length(c) ~= length() ) error([ The third argument (a) must be the same length as the 5th... argument () ]); if( length(d) < length()- )
error([ The second argument (b) cannot be more then one element... shorter than the 5th argument () ]); if( min(y) < min() ) error([ The values in the 6th argument (y) should not be less than... the values in the 5th argument () ]); if( ma(y) > ma() ) error([ The values in the 6th argument (y) should not be greather... than the values in the 5th argument () ]); S = zeros(size(y)); for k = :length(y) n = min([length(b) ma(find(<=y(k)))]); S(k) = polyval([d(n) c(n) b(n) a(n)],(y(k)-(n))); Tridiagonal solver Solver for tri-diagonal matrices. T must be in compact form: the sub-diagonal elements in the first column, the diagonal in the second column, and the super-diagonal in the third column. Note that T(,) and T(3,n) are never accessed, i.e. the subdiagonal entries start on the second row, and the superdiagonal elements on the (n-)st row. function []=trisolve(t,b) if nargin ~= error( trisolve:: two input arguments required. ) if nargout ~= error( trisolve: one output argument required. ) [n,m] = size(t); if m ~= 3 error( trisolve: matri must have three columns. )
[N,M] = size(b); if N ~= n error( trisolve: rhs and matri must have same number of rows. ); work = zeros(n,); work() = T(,); (,:) = b(,:); Forward sweep. for i=:n beta = T(i,)/work(i-); (i,:) = b(i,:) - beta*(i-,:); work(i) = T(i,) - beta*t(i-,3); (n,:) = (n,:)/work(n); Backward sweep. for i=n-:-: (i,:) = ((i,:) - T(i,3)*(i+,:)) / work(i); BF 3.4.6. d. The data for the above problem were generated from the function f() = ln(e + ). We evaluate the f() and S () at =.5 and find the absolute error giving: S (.5) =.99454 f(.5) =.896993 S (.5) f(.5) =.353. Similarly, we evaluate the derivative of the spline and the function to obtain: S (.5) =.39739958 f (.5) =.399935 S (.5) f (.5) =.6488. BF 3.4.8. d. The clamped spline interpolation routine below produces the following coefficients: i a i b i c i d i -..869948.55364.653747467.6367 -.5.9589.3739567.893795867.54.9863.33338433.999467.87579733 which gives the clamped cubic spline:
S () =.869948 +.55364( + ) +.653747467( + ) +.6367( + ) 3, [,.5], S () =.9589 +.3739567( +.5) +.893795867( +.5) +.54( +.5) 3, [.5, ], S () =.9863 +.33338433 +.999467 +.87579733 3, [,.5]..3.5..5..5.95.9.85.5.5 Main Code function z = spline_clp(n,, f) clear all load e344d fp =.55364; fpn =.458676; n =length(f); for i = :n- h(i) = (i+)-(i); a = f; alpha = 3*(a()-a())/h()-3*fp; alphan = 3*fpn -3*(a(n)-a(n-))/h(n-); for i = :n- alpha(i-) = (3./h(i)).*(a(i+)-a(i))-(3./h(i-)).*(a(i)-a(i-)); rhs = [alpha alpha alphan] ; t = *h();
tnn = *h(n-); for i = :n- l(i) = *(h(i)+h(i+)); T = [ h(:n-) ; t l(:n-) tnn ; h(:n-) ] ; c = trisolve(t, rhs); for j = :n- b(j) = (a(j+)-a(j))./h(j)-h(j)*(c(j+)+*c(j))./3; d(j) = (c(j+)-c(j))./(3*h(j)); y = min():.:ma(); Sy = evalspline(a,b,c,d,,y); plot(y, Sy, -,, f, o ) print -depsc spline_clpd.eps BF 4..5. The three-point formulae for derivatives are f ( ) = h [ 3f( ) + 4f( + h) f( + h)] + h 3 f (3) (ξ ), f ( ) = h [ f( h) + f( + h)] + h 6 f (3) (ξ ), f ( ) = h [f( h) 4f( h) + 3f( + h)] + h 3 f (3) (ξ ), f() f () 8. 6.944 3.95 8.3 7.5649 3.65 8.5 8.956 3.39975 8.7 8.89 3.6355 BF 4.4.(a). For trapezoid rule, we see that with n = 4.5.5 BF 4.4.(c). We see that with n = 8.75.75 cos ()d.993343. (sin ()) sin() + )d.48937. The trapezoid rule is given by the MatLab program: Trapezoid Method for BF 4.4; f = inline( (cos())^ ); n = 4;
a = -.5; b =.5; h = (b-a)/n; fprintf( ------------------------------------------------\n ); fprintf( n h sum \n ); fprintf( ------------------------------------------------\n ); sum = h*(f(a)+f(b))/; for j = :(n-) = a+j*h; sum = sum + h*f(); fprintf( 3d 8.6e.8f \n,n, h, sum); BF 4.4.4(a). For Simpson s rule, we see that with n = 4.5 cos ()d.98865..5 BF 4.4.4(c). We see that with n = 8.75 (sin ()) sin() + )d.4898..75 The Simpson s rule is given by the MatLab program: Simpson Method; f = inline( (cos())^ ); n = 4; a = -.5; b =.5; h = (b-a)/n; k = n/; fprintf( ------------------------------------------------\n ); fprintf( n h sum \n ); fprintf( ------------------------------------------------\n ); sum = h*(f(a)+f(b))/3; = a+h; sum = sum + (4*h/3)*f(); for j = :(k-) = a+(*j)*h; = a+(*j+)*h; sum = sum + (*h/3)*f()+(4*h/3)*f(); fprintf( 3d 8.6e.8f \n,n, h, sum);
BF 4.4.. We integrate and obtain π cos()d = π = 6.838538. The codes above to increase n until the accuracy is within tolerance ( 4 ). a. The Composite Trapezoid Rule requires n = 8 with h =.3779 to obtain the approimation: π cos()d 6.83847. b. The Composite Simpson s Rule requires n = 8 with h =.74533 to obtain the approimation: π cos()d 6.838755. c. The Composite Midpoint Rule requires n = 3 with h =.97565 to obtain the approimation: π cos()d 6.83856. BF 4.6.6(a). We want to compute. ( sin d =.4558834, ) where Maple gives us the value above. We use the Adaptive Quadrature with the Composite Simpson s rule. The tolerance is set at 3 for the program (given in class). The graphs below show the function and the refinement levels to obtain the solution to the level of desired accuracy. The computed value of the integral is.454998, with an error of.68 4. Adaptive CSR The Function 8 Adaptive CSR Refinement Levels.8 7 f().6.4...4.6 Refinement Level 6 5 4 3.8.5.5.5.5 BF 4.7.(b). We use Maple to show that e d = 5e =.66794.
To use Gaussian quadrature, we first convert the integral to be defined on t [, ], so we have e d = ( ) t + t+ e ( ) dt. Using Gaussian quadrature with n =, we have e d [ (r ) + e ( r ) ( ) + r + + e ( r ) ] + =.594439, where r =.5773569 = r. The absolute error is.94. BF 4.7.(g). We use Maple to show that 3.5 3 4 d = 4 3.5 3 =.63633458. To use Gaussian quadrature, we first convert the integral to be defined on t [, ], so we have 3.5 3 4 d = (t + 3)/4 4 (t + 3) /6 4 dt. Using Gaussian quadrature with n =, we have 3.5 3 4 d [ ] (r + 3)/4 4 (r + 3) /6 4 + (r + 3)/4 (r + 3) /6 4 where r =.5773569 = r. The absolute error is.678. =.63696565, BF 4.7.(b). We use the conversions above to get the form for the Gaussian quadrature. Using Gaussian quadrature with n = 3, we have e d [ ( ) r + c e ( r ) ( ) + + c e ( ( ) r + ) + c e ( r ) ] + =.65953868, where r =.774596669 = r and c =.5555555556 and c =.8888888889. The absolute error is.7473. BF 4.7.(g). We use the conversions above to get the form for the Gaussian quadrature. Using Gaussian quadrature with n = 3, we have 3.5 3 4 d [ ] c (r + 3)/4 4 (r + 3) /6 4 + c 3/4 (3) /6 4 + c (r + 3)/4 (r + 3) /6 4 =.636396, where r =.774596669 = r and c =.5555555556 and c =.8888888889. The absolute error is.499 7.
BF 4.8.(a). We use Maple to solve.5.4.. y dyd =.5. y 3 3.4 =.35733334.. Net we approimate the double integral with Simpson s rule using m = n = 4. The program below produces.5.4.. y dyd.357333. We note that there is no error between the actual value of the double integral and the Simpson rule approimation, which should be true as Simpson s rule solves polynomials degree less than or equal to 3. (The integrand is linear in and quadratic in y, so the values will be eact in each direction.) f = inline( *y^ ); c = inline(. ); d = inline(.4 ); a =.; b =.5; m = 4; n = 4; h = (b-a)/n; An = ; Ae = ; Ao = ; for i = :n = a + i*h; ya = c(); yb = d(); hy = (d() - c())/m; Bn = f(,ya) + f(,yb); Be = ; Bo = ; for j = :m- y = ya + j*hy; z = f(,y); if rem(j,)== Be = Be + z; else Bo = Bo + z; A = (Bn + 4*Bo + *Be)*hy/3; if i == i == n An = An + A; else if rem(i,)== Ae = Ae + A;
else Ao = Ao + A; Dint = (An + 4*Ao + *Ae)*h/3; fprintf(, \n Double Integral is.8f \n,dint); BF 4.8.(c). We use Maple to solve. ( ). ( + y 3 y )dyd = + y4 4 =. ( 3 + 5 ) 4 4 d = 6.5864. Net we approimate the double integral with Simpson s rule using m = n = 4. The program below produces. ( + y 3 )dy d 6.586463. Again we note that there is almost no error between the actual value of the double integral and the Simpson rule approimation. BF 4.8.(a). The notes above about Simpson s rule solving eactly any polynomial of degree less than or equal to 3 means that we can use n = m =, the smallest possible values of n and m to get the actual value of the integral, which requires 9 function evaluations. BF 4.8.(c). The notes above about Simpson s rule solving eactly any polynomial of degree less than or equal to 3 means that we can use m =, the smallest possible values of m to get the actual value of the integral in the y direction. With n =, the approimation is not quite sufficiently accurate (in the direction it is a polynomial of order 4), but increasing to n = 4 gives the approimate double integral accurate to 6 of the actual value. Thus, if we require m = n, then m = n = 4, which requires 5 function evaluations. BF 4.9.(a). u =, so This integral has a singularity at =, so we transform the integral with which is singular at u =. With e e u d = du = [ P 4 (u) ] du + G(u)du, u e u P 4 (u) = + u + u + u3 6 + u4 4 The integral with P 4 (u) is easily handled, giving and G(u) = P 4 (u) u du =.93544974. { e u P 4 (u) u, u,, u =, We apply Simpson s rule with n = 6 to the integral for G(u), so G(u)du (G() + 4G(/6) + G(/3) + 4G(/) + G(/3) + 4G(5/6) + G()) 8 =.7666.
e d = [ P 4 (u) ] du + G(u)du (.93544974+.7666) =.7659786. e u e BF 4.9.3(c). With the transformation of = t, we have cos() cos(/t) 3 d = t 3 ( t )dt = t cos(/t)dt. Clearly, lim t t cos(/t) =, so we directly apply the Composite Simpson s rule with n = 6. With G(t) = t cos(/t), the result is t cos(/t)dt = (G() + 4G(/6) + G(/3) + 4G(/) + G(/3) + 4G(5/6) + G()) 8.556869. The actual value is.876, so there a significant error in this calculation as one might epect from the rapid oscillations of the cosine function near t =. Problem. A number of these integrals have singularities, so it is useful to use a quadrature technique that does not evaluate at the points. Gaussian quadrature has the approimately the same accuracy as the Simpson s method. Below is a Composite Gaussian quadrature program that does successive halving of the interval size until the tolerance is met. f = inline( sin()/ ); a = ; b = *pi; tol = ^(-6); ma_iter = ; n = ; fprintf( ------------------------------------------------\n ); fprintf( n h sum \n ); fprintf( ------------------------------------------------\n ); h = b-a; u = a + h*(-/sqrt(3))/; v = a + h*(+/sqrt(3))/; sum = h*(f(u)+f(v))/; sum = sum; fprintf( 3d 8.6e.8f \n,n, h, sum); while(n < ma_iter) m = ^n; sum = ; h = (b-a)/m; for j = :m u = a + (j-)*h + h*(-/sqrt(3))/; v = a + (j-)*h + h*(+/sqrt(3))/; sum = sum + h*(f(u)+f(v))/; diff = abs(sum - sum);
fprintf( 3d 8.6e.8f \n,m, h, sum); if (diff < tol) return; sum = sum; n = n+; Part A. We split the integral into two parts and transform the second integral with t =, so e t4 dt = = e t4 dt + e t4 dt + e t4 dt e 4 d Each of these integrals are solved using the algorithm above. We obtain e 4 e t4 dt + d.84483859 +.656386 =.96445. We iterated these integrals until the tolerance was 6. The stepsize for the first was h = /3, and the second was h = /6. A graph of the integrand is seem below.
y = e t4 y = sin(t)/t y.8.8.6.6 y.4.4.. 3 4 5 Problem A. 3 4 5 6 Problem B Part B. We used the Gaussian quadrature algorithm above for the integral below and iterated until the tolerance was 6. The final stepsize was h = /3, and the resulting integral is approimated by π sin(t) dt.4856. t A graph of the integrand is seem above. Part C. The integrand for this integral oscillates wildly. We tried both the Gaussian quadrature formula above and the adaptive CSR. The latter should be more efficient computing the integral as the integrand for smaller t is well-behaved. The Gaussian quadrature needed to use a stepsize of h = /496 to be within a tolerance of 5, while the graph below shows the stepsizes for the adaptive CSR for a tolerance of 5 The resulting integral is approimated by 5 sin(t 4 ) dt.34883349 (for ACSR =.34883335). A graph of the integrand is seem below. y = sin(t 4 ) 4 Adaptive CSR Refinement Levels y.5.5 Refinement Level 8 6 4 3 4 5 Problem C 3 4 5 Problem C steps