从polyfit图(Matlab)中提取给定y阈值的x值 [英] Extracting x value given y threshold from polyfit plot (Matlab)

查看:318
本文介绍了从polyfit图(Matlab)中提取给定y阈值的x值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如实线和虚线所示,我想创建一个函数,在该函数中我从该阈值设置y(强度)的阈值,从而为我提供了对应的x值(虚线).非常简单,但我的while语句已关闭.任何帮助将非常感激!

As shown by the solid and dashed line, I'd like to create a function where I set a threshold for y (Intensity) from that threshold it gives me corresponding x value (dashed line). Very simple but my while statement is off. Any help would be much appreciated!

%% Curve fit plotting %%

x1 = timeStamps(1:60); % taking timestamps from 1 - 120 given smoothed y1 values 
y1 = smooth(tic_lin(1:60),'sgolay',1);


% Find coefficients for polynomial (order = 4 and 6, respectively)
fitResults1 = polyfit(x1',y1, 7);

% evaluate the fitted y-values
yplot1 = polyval(fitResults1,x1');

% interpolates to find yi, the values of the underlying function Y at the points in the vector or array xi. x must be a vector. 
Time_points = interp1(yplot1, x1', yplot1);


figure( 'Name', 'Curvefit1_poly' );
h = plot(x1', y1);%smoothed-points
hold on;
plot(x1', yplot1);%polyfit points
hold on;
plot(Time_points, yplot1, '*r');%interpolated points of x given y


%given y-threshold, output x(corresponding time_point).
broken = false;

while broken == false
    if yplot1 >= 2024671226502.99
        index = find(yplot1);
        xDesired = x1(index);
        broken = true;
    else
        disp("next iteration through");
    end
end

推荐答案

这里不需要while循环...您可以使用阈值条件的逻辑索引和find的逻辑索引来获取第一个索引:

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 );

请注意,如果没有y值超过阈值,则xDesired将为空.

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

或者,您已经具有多项式拟合,因此您可以使用fzero来获取给定y的多项式上的x值(在这种情况下为阈值).

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) )

请注意,如果阈值不在y范围内,则此方法可能会产生意外结果.

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

这篇关于从polyfit图(Matlab)中提取给定y阈值的x值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆