从数组创建线时如何找到交点 [英] how to find intersection points when lines are created from an array

查看:101
本文介绍了从数组创建线时如何找到交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下推荐的交集功能最适合8000个值的数组,但是如果我有100000个或更多值的数组,我将用光内存(并且我有16gig的ram),这很可能是由于归功于带有交点功能的repmat命令.

我正在尝试查找从数组创建的线的交点.但是不断出现错误" fzero:不是有效的初始包围",我使用的是倍频程3.8.1 (这是matlab的开源版本),下图是我想在交点处用黑眼圈得到的结果. 我是否需要在for循环中使用fzero才能遍历x和y值的数组?

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*')

以下推荐的交集功能最适合8000个值的数组,但是如果我有100000个或更多值的数组,我将用光内存(并且我有16gig的ram),这很可能是由于归功于带有交点功能的repmat命令.

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 

推荐答案

我不确定您为什么不使用针对上一个问题的解决方案

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):

如果X0是单个标量,则几个附近和远处的值是 尝试获取有效的包围式曝光.如果不是这样 成功,功能失败.

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.

因此,正在发生的事情是您对3的最初猜测离解决方案太远了.试试看吧:

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

这篇关于从数组创建线时如何找到交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆