0001 This function takes in 2 arrays of data and calculates the convex hull. Then it returns the point whose slope is closest to the input target slope.
0002
0003
0004 function h = convex ( x, y, target , VERBOSE)
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052 if (nargin == 3) VERBOSE = 0;
0053
0054 val = -1;
0055 [mm,nn] = size (x);
0056 if ((mm~=1) & (nn~=1))
0057 fprintf(2,'\nArrays must be 1 dimensional (vectors)');
0058 end
0059 end
0060 [mm,nn] = size (y);
0061 if ((mm~=1) & (nn~=1))
0062 fprintf(2,'\nArrays must be 1 dimensional (vectors)');
0063 end
0064 end
0065 clear mm,nn;
0066
0067 len = length(x);
0068
0069 if (len ~= length(y))
0070 fprintf(2,'\nArrays must be the same length');
0071 end
0072 end
0073
0074 elim = zeros (1,len);
0075 slopelist = zeros (1,len);
0076 min_x = 0;
0077 slope = zeros(1,len);
0078
0079
0080 cur_y = min(y);
0081 min_idx = find(y==min(y));
0082
0083 cur_x = x(min_idx);
0084
0085
0086 min_x = min(x);
0087 elim(min_idx) = 1;
0088 jj = 1;
0089
0090
0091
0092 while (cur_x ~= min_x)
0093 smallest = inf;
0094 for (ii = 1:len)
0095 if elim(ii) == 0
0096 if (x(ii) < cur_x)
0097 candidate = -1*(y(ii)-cur_y)/(x(ii)-cur_x);
0098 if (candidate < smallest)
0099 smallest = candidate;
0100 min_idx = ii;
0101 end
0102 end
0103 else
0104 elem(ii) = 1;
0105 end
0106 end
0107 cur_y = y(min_idx);
0108 cur_x = x(min_idx);
0109 elem(min_idx) = 1;
0110 slope (jj) = smallest;
0111 slopelist (jj) = min_idx;
0112 jj = jj+1;
0113 end
0114
0115
0116
0117 if ((slope(1) > target) | (slope(jj) < target))
0118 if (slope (1) > target )
0119 val = slopelist (1);
0120 else
0121 val = slopelist (jj);
0122 end
0123 end
0124
0125 smallest = abs(slope (slopelist(1)) - target);
0126 val = slopelist(1);
0127 for (ii = 1:jj)
0128 diff = abs(slope(ii)-target);
0129 if (VERBOSE) fprintf(2,'\nPoint : %d\t %f',slopelist(ii),slope(ii));
0130 if (diff < smallest)
0131 smallest = diff;
0132 val = slopelist (ii);
0133 end
0134 end
0135
0136 h = val;
0137 end
0138
0139
0140
0141 --------------------------------------------------------------------------------
0142 If you have questions/comments...
0143
0144 matlabboy@school
0145
0146
0147 previous script | jon's home | information coding lab | next script
0148