-1

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 

enter image description here

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Rick T
  • 3,349
  • 10
  • 54
  • 119
  • I'm not familiar with the language you are using; however, you do have single quote at the end of your lines declaring x1, x2, and y1. Perhaps that is related to your problem? – Bobort Sep 26 '14 at 14:47
  • @Ander Biguri No it's not a duplicate please read updated question the original answer will not work for large arrays. – Rick T Sep 26 '14 at 15:29
  • I think it would help if you could better describe your input data. Is X2 always equal to x1 and are they equally spaced and monotonic? Is y2=abs(y1-max(y1)) only in your example. If we can make those assumptions this would greatly simplify the calculations. No need for fzero or polyval in this case. – Andy Sep 28 '14 at 14:51

1 Answers1

0

I am not sure why you are not using the solution given to your previous question Finding where plots may cross with octave / matlab

But here's what's happening here (from the doc):

If X0 is a single scalar then several nearby and distant values are probed in an attempt to obtain a valid bracketing. If this is not successful, the function fails.

So what's happening is that your initial guess of 3 is too far away from the solution. Try that instead:

>> x_intersect = fzero(@(x) polyval(p1-p2,x),30)
x_intersect =  46.667

However, I am not sure what you are trying to do by fitting a first degree polynomial to your data, it doesn't make sense to me...

Community
  • 1
  • 1
am304
  • 13,758
  • 2
  • 22
  • 40
  • 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 2) store the intersection value in an array. This should solve running out of memory issues using large arrays – Rick T Sep 26 '14 at 15:05