Finding the first intersection point between 2 lines

8 views (last 30 days)
Hello all,
I need to find the time constant for which I plotted a line at y = 63% of the maximum value (y_max)
The other line comes from a Simulink model in which I can switch between different parameters, the output (y) differs every time (the line isn't plotted from a function). I'm using the following code which uses the 'interp1' function:
y_tau = 0.63*y_max;
[y,idx] = unique(y);
t = t(idx);
t_constant = interp1(y,x,y_max)
However, this always gives the second intersection point in the graph (see picture below). In this particular case:
t_constant = 190.2194
.
How can I get the first intersection point using this code or any other method? Thank you in advance.

Accepted Answer

Mathieu NOE
Mathieu NOE on 23 Dec 2022
hello
try this
code is fairly simple to use as you only have to specify the y threshold value
threshold = 0.63*max(y); % 63% of peak amplitude
% dummy data
n = 1000;
x = 10*(0:n-1)/n;
y = 1+square(x);
[B,A] = butter(1,0.004);
y = filter(B,A,y);
% main code
threshold = 0.63*max(y); % 63% of peak amplitude
t0_pos1 = find_zc(x,y,threshold);
t0_pos1 = t0_pos1(1); % keep only first crossing point
figure(1)
plot(x,y,'b',x,threshold*ones(size(x)),'k--',t0_pos1,threshold*ones(size(t0_pos1)),'*r','linewidth',2,'markersize',12);grid on
legend('signal','threshold','signal positive slope crossing points');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Zx] = find_zc(x,y,threshold)
% positive slope "zero" crossing detection, using linear interpolation
y = y - threshold;
zci = @(data) find(diff(sign(data))>0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
Zx = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
end

More Answers (2)

KSSV
KSSV on 22 Dec 2022

Torsten
Torsten on 22 Dec 2022
If you have the t-y values as vectors available, you can use this code to determine the t-values where y-y_tau=0:
y_tau = 0.63*y_max;
y = y - y_tau;
i = find(y(1:end-1).*y(2:end) <= 0)
t_root = t(i)-y(i).*(t(i+1)-t(i))./(y(i+1)-y(i))

Categories

Find more on Specialized Power Systems in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!