I want to find particular values of t at which y becomes 2 (by writing some commands in code itself). Can I extract such values of t as a vector?
4 views (last 30 days)
Star Strider on 23 Apr 2018
N=2000; % Number of time stpes
dt=0.05; %Time step
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
idx_2 = zci(y - 2); % Vector Of Approximate Indices Where ‘y=2’
for k = 1:numel(idx_2)
t2(k) = interp1(y(idx_2(k)+[-1 1]), t(idx_2(k)+[-1 1]), 2, 'linear','extrap'); % Interopolate: ‘t’ Where ‘y=2’
plot(t2, 2*ones(size(t2)), 'pg')
This uses the ‘zci’ utility function to find the approximate indices where ‘t=2’, then uses the ‘idx_2’ index vector to loop through your data to find the closest values of ‘t’ that the interp1 function can calculate where ‘y=2’.
More Answers (1)
Benjamin Großmann on 23 Apr 2018
You could rewrite your function so that your wanted data points are maxima and search them with findpeaks.
[~,locs] = findpeaks(-abs(y-2));
The pretty simple and elegant t2 = t(y==2) does not work here, since your data is not exaxtly two at any given entry.