quad('f',a,b,tol,1), quad8('f',a,b,tol,1)
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).
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.
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).
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.
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.