Given a vector of Y values (ordered in the sense that the corresponding X values are steadily increasing), you may find easily near which X values the roots are located. The roots are either where a Y value is zero or between two consecutive Y values that change sign. The idea is illustrated in this code snippet:
X = -1:0.1:1;
Y = X.*X - 0.4;
root_exact_pos = find(Y==0);
root_approx_pos = find(diff(sign(Y))~=0);
The roots are in the X
values, either in X(root_exact_pos(k))
, or between X(root_approx_pos(k))
and X(root_approx_pos(k)+1)
, k
going from 1 to the number of elements of the respective root position array.
From here on you may apply whatever interpolation you'd like to find a better approximation of the root (I would go with linear, between the 2 points).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…