|
Say I have a function f(x), where x is a scalar, that I have previously evaluated on a uniform grid, that is for some given vector xx=(x1,x2,...xN)
with x2-x1=x3-x2=etc.
So I have values
yy = (f(x1),f(x2),...f(xN)).
I want to find the zero of the linear interpolated approximation of f(.).
I see two options:
fzero(@(x) interp1(xx,yy,x),[x1,xN])
or
interp1q(yy,xx,0)
A priori, is there a good reason to expect one of them to be faster?
My initial thought is that it may depend, because since xx is on a uniform grid and fzero may require relatively few evaluations of interp1 (which could be fast on a uniform grid). In contrast, since yy is not uniformly spaced (which is why I was thinking of interp1q instead of interp1), the single call to interp1q could be slower, since it has to locate the In other words, the fzero option is "skipping grid points" (it's not really on the grid though) in search of the near 0 value of yy, but the interp1q is exhaustively looking for the near 0 grid point, before it interpolates.
I have run some test and was surprised to find that the second option using interp1q always came out faster. How much depends on the function f(). But I may have not tried enough examples to find a case with the reverse performance.
Any ideas? Thanks
-Ivan
|