Mth 1 Numericl Anlysis (Bueler) December 9, 29 Solutions to Assignment #8 Problems 64, exercise 14: The nswer turns out to be yes, which mens tht I hve to be orgnized in writing it up There re lot of fcts to check; I count 8 Let s strt with the first nd second derivtives: 2 + (x + 1) 2 [ 1, ] 6(x + 1) [ 1, ] f (x) = 5 + 6x [, 1] f (x) = 6 [, 1] 11 + 6(x 1) (x 1) 2 [1, 2] 6 6(x 1) [1, 2] Next, note f ( 1) = nd f (2) = so we it is nturl Furthermore the second derivtive is continuous t x = (f () = 6) nd t x = 1 (f (1) = 6) The first derivtive is continuous t x = (f () = 5) nd t x = 1 (f (1) = 11) Finlly the function itself is continuous t x = (f() = ) nd t x = 1 (f(1) = 11) The function is, therefore, the unique nturl cubic spline through the points ( 1, ), (, ), (1, 11), (2, 24) (Checking continuity t n x-vlue mens plugging tht vlue into the polynomil on ech side nd checking tht results gree) Problems 64, exercise 17: This is good one to do by hnd, but I will only show here how to check your nswer with ncsplinem The progrm ncsplinem only computes the coefficients internlly, without displying them, so I copied it to new file ncspline dumpm, nd uncommented the line %[b c d] I modified just bit more to disply clen tble of coefficients Then I rn it, with result >> ncspline_dump([-1 1],[1 7 9],); tble [ b c d] of coefficients: ns = 1-8 2 7-2 6-2 Thus the nturl cubic spline with these vlues nd knots is { 1 8(x + 1) + 2(x + 1), x [ 1, ] S(x) = 7 2x + 6x 2 2x x [, 1] On the other hnd, producing clen plot is exctly the job for which ncsplinem ws built, so: >> x=[-1 1]; y=[1 7 9]; xf=-1:1:1; yf=ncspline(x,y,xf); plot(x,y, o,xf,yf) (The result is not shown) Computer Problems 64, exercise 1: (Sorry, I overlooked the severl test cses phrse I should hve sid tht one test cse is fine) My curve on grph pper looked like the script letter v It generted two lists of x(t) nd y(t) vlues, nd ech list generted cubic spline (with indepent vrible t in ech cse) The function ncsplinem generted finely-interpolted x nd y vlues, nd plotting these, without ny reference to t, gve bck the figure The rest is detils: lettervm % LETTERV Use nturl cubic splines (ncsplinem) to plot script letter % "v", using points tken from written letter on grph pper t = :1:1; % 11 points xy = [ 7; % I found it esiest to enter coordintes of points 4 79; % this wy, s list of (x y) pirs 48 6; 51 4;
2 5 26; 6 2; 71 ; 79 5; 82 6; 9 74; 1 76]; x = xy(:,1) ; y = xy(:,2) ; % extrct just x nd y seprtely tf = :1:1; % fine grid for plotting (on t-vs-x nd t-vs-y xes) xf = ncspline(t,x,tf); yf = ncspline(t,y,tf); % t-versus-x nd t-versus-y plots, using subplot: figure(1) subplot(2,1,1), plot(t,x, o,tf,xf), grid on, xlbel t, ylbel x title("prmeterized curve in pieces") subplot(2,1,2), plot(t,y, o,tf,yf), grid on, xlbel t, ylbel y % plot in x,y plne; prmeter "t" is never mentioned! figure(2), plot(x,y, o,xf,yf) xis equl, grid on, xlbel x, ylbel y, title("the letter v") Figure 1 The script letter v by cubic splines through by-hnd identified points This is figure(2) from the code lettervm Problems 72, exercise 1: Somewht tedious, but the method is cler Write down the Lgrnge polynomils l (x),, l (x) nd integrte them to find A,, A, respectively, in the integrtion rule f(x) dx A f() + A 1 f( 1 ) + A 2f( 2 ) + A f(1) Here is n lternte wy tht exploits our vilble tool, Mtlb/Octve The rule should be ble to integrte cubic polynomils exctly (Why?) This gives 4 equtions in the unknown
A i : 1 = 1/2 = 1/ = 1/4 = 1 dx = A + A 1 + A 2 + A, This system of equtions hs numericl solution: x dx = A + (1/)A 1 + (2/)A 2 + 1A x 2 dx = A + (1/9)A 1 + (4/9)A 2 + 1A x dx = A + (1/27)A 1 + (8/27)A 2 + 1A >> formt rt >> [1 1 1 1; (1/) (2/) 1; (1/9) (4/9) 1; (1/27) (8/27) 1] \ [1 (1/2) (1/) (1/4)] ns = 1/8 /8 /8 1/8 So Problems 72, exercise 14: Problems 72, exercise 1: pge 482 is f(x) dx 1 8 The derivtives of the integrnd re [f() + f( 1 ) + f(2 ) + f(1) ] Similr to previous This is firly similr to Exercise 2 below The error term on (b ) h 2 f (ξ) 12 f (x) = 1 2xe x2, f (x) = ( 2 + 4x 2 )e x2 The mximum of f (x) on the intervl is bounded: f (x) = 2 + 4x 2 e x2 14e 1 = 515 (This is quite pessimistic becuse the mgnitude of f is in fct less thn 1 on the intervl [1, 2] But it suffices) Require the error to be smller thn the given number: (b ) h 2 f (ξ) 12 = h2 12 f (ξ) 515h2 5 1 7 12 This equivlent to h 1165 1 7 = 4 1 4 which sys n = 1/h 29298 so I get n = 29 subintervls Exercise 1: () Keeping the nme x 1 for the center point (+b)/2, the Lgrnge polynomils simplify to l (x) = (x x 1)(x b) ( x 1 )( b) = 2 (b ) 2 ( x 2 (x 1 + b)x + x 1 b ), l 1 (x) = (x )(x b) (x 1 )(x 1 b) = 4 ( x 2 (b ) 2 ( + b)x + b ), l 2 (x) = (x )(x x 1) (b )(b x 1 ) = 2 (b ) 2 ( x 2 ( + x 1 )x + x 1 )
4 The right-hnd expressions re one of severl forms in which the Lgrnge polynomils re reltively esy to integrte The result of integrtion is given in the problem sttement These integrls were needed to compute the coefficients A, A 1, A 2 in Simpson s rule, b f(x) dx 2 i= A if(x i ) (b) Z b I ll give some detil here: Z b» (x )(b x) dx = x 2 x ( + b)x + b dx = = b + ( + b) b2 2 b2 + b x2 ( + b) 2 + bx ( + b) 2 2 + 2 b = 1 ( b)(2 + b + b 2 ) + 1 2 (b )(2 + b 2 ) = 1 6 (b ) ˆ 2 2 2b 2b 2 + 2 + b 2 = 1 6 (b )(b (b )2 ) = 6 This integrl ws necessry to complete the error formul for the trpezoid rule, Exercise 2: b f(x) dx = b 2 [f() + f(b)] f (ξ) 12 (b ) The error formul for the composite Simpson s rule on pge 484 sys b f(x) dx = S n b 18 h4 f(4)(ξ) for S n the composite Simpson s pproximtion nd h = (b )/n In our cse f(x) = π + sin(x) so f (4) (x) = 81 sin(x) Thus f(x) dx S n = 1 18 h4 81 sin(ξ) 9 2 h4, using the stndrd inequlity sin θ 1 Reclling h = 1/n, we wnt to choose 9 2 h4 1 8 9 1 4 2 n 4 1 8 n 4 45 1 12 n 1456475 But n must be n even integer, so n = 1458 is the smllest vlue we expect to get tht ccurcy Does it get tht ccurcy? On the one hnd, the exct nswer is On the other hnd, π + sin(x) dx = 1π + 1 (1 cos()) >> f=@(x) pi + sin(*x); >> n=1458; h=1/n; xi=:h:1; >> Sn = (h/) * sum([1 repmt([4 2],1,n/2-1) 4 1] * f(xi)) Sn = 16978427195495 >> exct = 1*pi+(1/)*(1-cos()) exct = 16978427192687 >> bs(sn - exct) ns = 2875675118888e-1 Thus the error of 28 1 1 is smller thn 1 8 Why the gp? Tht is, why is it tht when we used n = 1458 the error ws quite bit less thn 1 8? Becuse of sin(ξ) 1, which is true but not precise In fct I find tht the smllest n for which the error is less thn 1 8 is n = 598; this comes from Mtlb/Octve progrm-t-the-commnd line: >> for n=8:2:1458 > h=1/n; xi=:h:1; Sn=(h/)*sum([1 repmt([4 2],1,n/2-1) 4 1]*f(xi)); > if bs(exct-sn)<1e-8, n, brek, > n = 598
5 Witness the power of super-clcultor eh? Exercise : From the sttement of the problem I immeditely ws ble to set up equtions for the coefficients In prticulr, s with cubic splines, for j =,, n 1 let h j = t j+1 t j nd m j = y j+1 y j h j The requirement Q j (t j ) = y j implies j = y j The other two requirements immeditely become y j + b j h j + c j h 2 j = y j+1 j =,, n 1, b j + 2c j h j = b j+1 j =,, n 2 This is 2n 1 equtions in the 2n unknowns b j, c j (for j =,, n 1), so the dditionl requirement c = is enough to hope for unique solution In prticulr, tke the first of the bove equtions nd solve it for b j : b j = y j+1 y j c j h 2 j h j = m j c j h j Substitute this into the second set of equtions, for ech of b j nd b j+1, to get or, equivlently, m j c j h j + 2c j h j = m j+1 c j+1 h j+1 h j c j + h j+1 c j+1 = m j+1 m j for j =,, n 2 This lst is set of n 1 equtions in the n unknowns c j, j =,, n 1, so the dditionl condition c = is needed We cn now write mtrix eqution for the c j : 1 h h 1 h 1 h 2 h n 2 h n 1 c c 1 c 2 c n 1 m 1 m = m 2 m 1 m n 1 m n 2 This is lower-tringulr nd bidigonl system As long hs h 1,, h n 1 re non-zero, which they re merely becuse we require distinct nodes, the solution c j is unique Once c j re found we cn find b j immeditely The solution cn be found by firly-obvious forwrd substitution, s in the first for loop in the following code: qsplinem function yi = qspline(t,y,xi) % QSPLINE yi = qspline(t,y,xi) % Computes not-prticulrly-nturl qudrtic spline through points % t = t(1),,t(n+1) % y = y(1),,y(n+1) % nd evlutes t the x-vlues in xi, to give result yi Requires % t(1) < t(2) < < t(n+1) nd xi in [t(1),t(n+1)], nd finds % qudrtic polynomils in this form: % Q_i(x) = y_i + b_i (x - t_i) + c_i (x - t_i)ˆ2 % Try: >> x=:; y=exp(x); xp=:1:; yq = qspline(x,y,xp); % >> plot(x,y, o,xp,exp(xp),xp,yq) % >> leg( dt, underlying function, qudrtic spline ) % Compre to ncsplinem for nturl cubic splines t = t(:); y = y(:); % force t nd y to be column vectors % check tht inputs mke sense if (length(t) = length(y)), error( t,y must be vectors of sme size ), n = length(t) - 1; if n <=, error( must hve t lest two points to produce spline ),
6 if ny(xi < t(1)) ny(xi > t(n+1)) error( xi must be in interior (intervl [t(1),t(n+1)]) ) h = diff(t); % gps: h(i) = t(i+1) - t(i) m = diff(y) / h; % slopes: m(i) = (y(i+1) - y(i)) / h(i) % directly solve bidigonl system for coeffs c_i, by forwrd substitution c = zeros(size(h)); % c_ = lredy for j=2:n c(j) = ( m(j)- m(j-1) - h(j-1)*c(j-1) ) / h(j); % determine other coeffs from c: b = m - h * c; % evlute qudrtics; requires finding intervl in which xi lies xi = xi(:); yi = zeros(size(xi)); for k = 1:length(xi) i = 1; while xi(k) > t(i+1) i = i + 1; % now xi(k) is in [t(i),t(i+1)], so pply Hoerner to evlute Q_i(x): dx = xi(k) - t(i); yi(k) = y(i) + dx * (b(i) + dx * c(i)); The geometric consequences of the choice c = re tht the left-most qudrtic is stright line, which is somewht ugly, while the lgorithmic consequence is lredy mentioned: the system of equtions is lower tringulr Another, possibly better choice is to require c = c 1, implying extr smoothness t the first interior node This condition cn be esily delt-with, but it does brek the literl lower-tringulrness of the system But both of these suggestions re obviously flwed in tht they re symmetric, becuse they tret the left of the spline differently from the right And, t this point, I don t hve relly gret suggestion