function [xi,yi] = find_intersection(x,y,w) %[xi,yi] = find_intersection(x,y,w) finds the intersection % point(s) of the two curves given by the vectors 'y' and 'w'. % The vector 'x' is the collection of x-axis inpedendent data % points. All vectors must be the same length. % Note: this function does not work on matrices yet. % % Example use: % x=0:0.01:3; y=x.*sin(x); w=0.07*exp(x)+0.4; % [xi,yi]=find_intersection(x,y,w); % plot(x,y,'b',x,w,'g',xi,yi,'ro'); grid on; % % Last Edit by Colin Joye, 6/27/03F % n is the index of the point just before intersection. n = find( abs( diff( ((y-w)>0)-((y-w)<0) ) )>0 ); % compute line slopes: m1 = ( y(n+1) - y(n) )./( x(n+1) - x(n) ); m2 = ( w(n+1) - w(n) )./( x(n+1) - x(n) ); % compute line points: b1 = y(n) - m1 .* x(n); b2 = w(n) - m2 .* x(n); % compute line intersections: xi = (b2 - b1) ./ (m1 - m2); yi = m2 .* xi + b2; % ------------- END find_intersection.m -----------------