The intersections functioned recommended below worked great for arrays up to 8000 values but if I had an array of 100000 values or more I would run out of memory, (and I have 16gig of ram) this most likely was caused due to the repmat command with the intersections function.
I'm trying to find the intersection points of lines created from an array. but keep getting an error "fzero: not a valid initial bracketing" I'm using octave 3.8.1 (which is an open source version of matlab) The image below is what I'm trying to get with the black circles at the intersection points. Do I need to have the fzero in a for loop to loop through the array of x and y values?
clear all,clf, clc,tic
%freq array here
x1=[20,30,40,50,60,70,80]';
x2=[20,30,40,50,60,70,80]';
y1=[2,4,3,7,1,8,4]';
y2=abs(y1-max(y1)); %used to switch high and low amplitude of freq
%fit linear polynomial
p1 = polyfit(x1,y1,1);
p2 = polyfit(x2,y2,1);
%calculate intersection
x_intersect = fzero(@(x) polyval(p1-p2,x),3);
y_intersect = polyval(p1,x_intersect);
line(x1,y1);
hold on;
line(x2,y2);
plot(x_intersect,y_intersect,'r*')
The intersections functioned recommended below worked great for arrays up to 8000 values but if I had an array of 100000 values or more I would run out of memory, (and I have 16gig of ram) this most likely was caused due to the repmat command with the intersections function.
So now I'm trying to:
1) cycle though each row in the array which represents a line
linea1-6 xvalues = 20 to 30, 30 to 40, 40 to 50, 50 to 60, 60 to 70, 70 to 80
linea1-6 yvalues =2 to 4, 4 to 3, 3 to 7, 7 to 1, 1 to 8, 8 to 4
lineb1-6 xvalues = 20 to 30, 30 to 40, 40 to 50, 50 to 60, 60 to 70, 70 to 80
lineb1-6 yvalues =6 to 4, 4 to 5, 5 to 1, 1 to 7, 7 to 0, 0 to 4
**I'm having problems coding the for loop to work with polyfit and fzero**
2) store the intersection values found for each line into an array.
This should solve running out of memory issues when using large arrays