Extracting x value given y threshold from polyfit plot (Matlab)

懵懂的女人 提交于 2019-12-02 08:31:52

No while loop is needed here... You can do this with logical indexing for the threshold condition and find to get the first index:

% Start with some x and y data
% x = ...
% y = ...

% Get the first index where 'y' is greater than some threshold
thresh = 10; 
idx = find( y >= thresh, 1 ); % Find 1st index where y >= thresh

% Get the x value at this index
xDesired = x( idx );

Note that xDesired will be empty if there was no y value over the threshold.


Alternatively, you already have a polynomial fit, so you could use fzero to get the x value on that polynomial for a given y (in this case your threshold).

% x = ...
% y = ...

thresh = 10;
p = polyfit( x, y, 3 ); % create polynomial fit

% Use fzero to get the root of y = a*x^n + b*x^(n-1) + ... + z when y = thresh
xDesired = fzero( @(x) polyval(p,x) - thresh, x(1) )

Note, this method may give unexpected results if the threshold is not within the range of y.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!