0

I would like to find the zeros of function given as vector. It comes from ode45.

The vector y(x) does not necessarily have point on y = 0 axis. The code below would work if the function is crossing the y = 0 from above, but what if it is tangential. The vectors resemble dumped sine wave, but not quite. Fitting with 7th degree polynomial works only for half period.

yx = find(y>0,1,'first');                   % Choose 1st ‘y’ Point >0
y0 = 0;
x0 = interp1(y(yx-1:yx),x(yx-1:yx),y0);     % Find Zero Cross Between `enter code here`Bracket Points
Anonymous
  • 167
  • 11
  • 1
    Can you not just use `ode45`'s [event detection](http://www.mathworks.com/help/matlab/math/ordinary-differential-equations.html#f1-669698) [capabilities](http://stackoverflow.com/a/16681767/2278029) to directly obtain your zero crossings? You're right, `fzero` is a true root solver and requires that the function actually crosses zeros. Tangency is not sufficient. You can try `fsolve` if this is the case. – horchler May 20 '15 at 17:04

1 Answers1

2

If your data is really clean, then you look for the places where the function changes sign or hits zero:

ix_solutions = find(y(1:end-1) .* y(2:end) <=0 );

However, this has the issue that, if one point is on the zero axis, is going to be counted twice as a solution. Taking in account also the suggestion of s.bandara from the comment, one could write a better implementation:

signs = sign(y(:));
ix_zeros     = find(signs == 0);
ix_crosses   = find(abs(diff(signs)) == 2);
ix_solutions = [ix_zeros; ix_crosses];
Community
  • 1
  • 1