1 | %FINDX Single-variable linear level finding ("inverse" INTERP1). |
---|
2 | % XI = findX(X,Y,YI) estimates the XI values at which the dependent |
---|
3 | % variables Y reach or cross a specified target level YI (scalar value). |
---|
4 | % If there are multiple solutions, findX finds all of them. |
---|
5 | % It may be seen as the "inverse" operation of the MATLAB's function |
---|
6 | % INTERP1, under 'linear' method. |
---|
7 | % |
---|
8 | % X is a vector containing a sequence of monotone increasing values of |
---|
9 | % the independent variable. Y is a vector containing the discrete values |
---|
10 | % of the dependent variable (underlying function). |
---|
11 | % |
---|
12 | % [XI,IDEXACT] = findX(X,Y,YI) returns in IDEXACT the indices where the |
---|
13 | % original values of Y exactly reach the target level YI, |
---|
14 | % i.e. Y(IDEXACT)=YI, so interpolation was not needed. |
---|
15 | % |
---|
16 | % Antoni J. Canos. |
---|
17 | % Microwave Heating Group, GEA. |
---|
18 | % ITACA, Technical University of Valencia. |
---|
19 | % Valencia (Spain), April 2009 |
---|
20 | |
---|
21 | function [XI,IDEXACT] = findX(X,Y,YI) |
---|
22 | |
---|
23 | % Check dimensions of input arrays |
---|
24 | m=length(X); |
---|
25 | r=length(Y); |
---|
26 | |
---|
27 | if ~isvector(X) | ~isvector(Y) |
---|
28 | error('X and Y must be vectors.'); |
---|
29 | end |
---|
30 | |
---|
31 | if (m ~= r) |
---|
32 | error('Lengths of X and Y vectors must be the same.'); |
---|
33 | end |
---|
34 | |
---|
35 | % Initialize outputs |
---|
36 | XI = []; |
---|
37 | IDEXACT = []; |
---|
38 | IDINTERP = []; |
---|
39 | |
---|
40 | % Subtract target level to simplify subsequent code |
---|
41 | Y = Y - YI; |
---|
42 | |
---|
43 | % Find exact values. They can be crossings or "kisses". |
---|
44 | IDEXACT = find(Y==0); |
---|
45 | |
---|
46 | % Find crossings |
---|
47 | IDINTERP = find ( Y(1:end-1) .* Y(2:end) < 0 ); |
---|
48 | |
---|
49 | % Calculating XI values and combining |
---|
50 | XI = union(X(IDEXACT),X(IDINTERP) + (X(IDINTERP+1) - X(IDINTERP)) .* Y(IDINTERP) ./ (Y(IDINTERP) - Y(IDINTERP+1))); |
---|
51 | |
---|