how to find intersection points when lines are created from an array [duplicate]

五迷三道 提交于 2019-12-13 08:51:39

问题


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 


回答1:


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...



来源:https://stackoverflow.com/questions/26062191/how-to-find-intersection-points-when-lines-are-created-from-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!