• Assignment #3:
    • Typo in problem 3: In the last paragraph it should be t=-20,-19,...,119,120 (instead of t=-20,21,...)
    • Typo at the end of problem 4: change to quad('f',a,b,tol,1), quad8('f',a,b,tol,1)
    • Problem 1: Solve a linear system or use Lagrange polynomials to find the interpolating polynomial. Do not use the Matlab function polyfit.

      Use plot(x,y,'o',xx,yy) to plot the given points together with the interpolating curve (the vectors x, y contain the coordinates of the given points, the vectors xx, yy contain the coordinates of the points on the interpolating curve).

    • Problem 2: Use spline twice: First to get xx from the given t and x-values, then to get yy from the given t and y-values. Use plot(x,y,'o',xx,yy); axis square to plot your 9 points together with the interpolating curve.
    • Problem 3: Do not use the Matlab function polyfit. Use plot(t,q,'o',tt,qq) to plot the given points together with the least squares curve (the vectors t, q contain the coordinates of the given points, the vectors tt, qq contain the coordinates of the points on the fitted curve).
    • Problem 4: To find the number of function evaluations, use an m-file like this:
      function y = f1(x)
      global nf            % declare nf as global variable
      y = x.*cos(x).^3;    % must use ".*","./",".^" since x may be vector
      nf = nf + length(x); % update total number of function evaluations

      Then you can use quad and find the number of function evaluations as follows:

      global nf           % declare nf as global (IMPORTANT!!)
      nf = 0              % reset nf
      q = quad('f1',0,1,.001)
      nf                  % show number of function evaluations

      Note that you have to reset nf before each call of quad.

    • Problem 5: In the m-file adquad.m you can evaluate func at a point x using y=feval(func,x).

      Note that a straight forward implementation is inefficient since it re-evaluates the function at the same points several times. Once you've got the straight forward implementation working, rewrite it as follows: Use the following adquad.m

      function q = adquad(func,a,b,tol)
      fa = feval(func,a); fb = feval(func,b);
      q = adquad1(func,a,b,tol,fa,fb);

      and write a function adquad1 which only uses one function evaluation fc=feval(func,c) at the midpoint c. If the estimated error in adquad1 is too large, it calls itself again for the two subintervals, passing on the already computed function values in the last two arguments.