I'm trying to find zeros of a function. See my code below.
Because fft
expects a numerical array, I didn't define the symbolic function to use fzero
.
However, this approach is not accurate and depend on step
. Do you have a better idea?
step=2000;
x=0:pi/step:2*pi;
y= 4+5*cos(10*x)+20*cos(40*x)+cos(100*x);
fy = fft(y');
fy(1:8) =0;
fy(12:end-10)=0;
fy(end-6:end)=0;
ffy = ifft(fy);
t=diff(ffy);
x=0:pi/step:2*pi-pi/step;
plot(x,t)
indices= find(t<5e-4 & t>-5e-4);
You could proceed along the array
t
and look for points where the values change sign. That would indicate the presence of a zero.Actaully, MATLAB's
fzero
function uses a similar method. You said you didn't use it because you required an array, rather than an anonymous function, but you could convert the array into an anonymous function using simple linear interpolation like so:EDIT : Just to clarify what I mean. If you have an array
t
and you want to find its zeros...The zeros I get are correct: