One key issue here is that there are multiple points on your plot where y = 0.3
. If you want to find all of them in a simple way, you can follow these steps:
- Subtract 0.3 from your vector
y
, so that the points you want to find become zero crossings.
- Find the indices in the above vector where there is a sign change.
- For the
y
values on either side of the zero crossings, compute the percentage of the difference between them at which the value 0.3 lies. This essentially performs a linear interpolation between the two points on either side of the zero crossing.
- Use the above percentage to find the corresponding value of
x
for the zero crossing.
And here's the code along with a plot to show the points found:
>> yDesired = 0.3;
>> index = find(diff(sign(y-yDesired)));
>> pctOfDiff = (yDesired-y(index))./(y(index+1)-y(index));
>> xDesired = x(index)+pctOfDiff.*(x(index+1)-x(index))
xDesired =
0.8291 3.9528
>> plot(x,y);
>> hold on;
>> plot(xDesired,yDesired,'r*')
>> xlabel('x');
>> ylabel('y');
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…