MATLAB: How to find X for a specific Y; there may be more than 1 value of X for a particular Y (not strictly increasing/decreasing)

curve fittinginterp1interpolationsolve

I have an empirical formula for which the curve comes as a sinusoid over a cycle. I wish to know, for which values of X the function teaches the maximum and minimum value and for which xs it yields zero value. Here is the code:
Dn=1:0.01:365;
T=2*pi*(Dn-1)/365;% T = Day Angle %
declination = (0.006918-0.399912*cos(T)+0.070257*sin(T)-0.006758*cos(2*T)+0.000907*sin(2*T)-0.002697*cos(3*T)+0.00148*sin(3*T))*180/pi;
X=Dn;
Y=declination;
plot(X,Y)
Y0=0;
Y1=max(Y);
Y2=min(Y);
X0=interp1(Y,X,Y0,'nearest','extrap')
X1=interp1(Y,X,Y1,'nearest','extrap')
X2=interp1(Y,X,Y2,'nearest','extrap')
X0 returns only one of the 2 possible values. how do I get both?

Best Answer

For min() and max(), you can utilize the second output
[Ymax,Imax]=max(Y)
Xmax=X(Imax)
For zero, you can use the following, noting that you can't compare it to zero due to floating point representation
Index=find(abs(Y)<1e-2)
Xzero=X(Index)