# find first time where data crosses 1

36 views (last 30 days)

Show older comments

##### 0 Comments

### Answers (2)

Mark Sherstan
on 11 Apr 2019

This example should help you with your data set

x = -2*pi:pi/12:2*pi;

y = 2*sin(x);

idx = find(y>1);

solution = idx(1)

fprintf("Solution at index %0.0f, x = %0.2f, y = %0.2f\n",solution,x(solution),y(solution))

figure(1)

hold on

plot(x,y)

plot(x(solution),y(solution),'*r')

##### 2 Comments

Mark Sherstan
on 11 Apr 2019

Star Strider
on 11 Apr 2019

I created ‘zci’ to do just that:

zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector

Then either use the interp1 function to do the interpolation, or create your own linear interpolation function:

x = 0:0.2:5; % Create Data

y = x.^2 - rand(size(x))*5; % Create Data

zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector

yzi = zci(y-1);

lintrp = [[x(yzi(1));x(yzi(1)+1)] ones(2,1)] \ [y(yzi(1));y(yzi(1)+1)] % Linear Interpolation

xint = (1-lintrp(2))/lintrp(1); % y=1 X-Value

figure

plot(x, y, xint, 1, '+')

grid

Experiment to get the result you want.

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!