问题描述
以下推荐的交集功能最适合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):
因此,正在发生的事情是您对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...
这篇关于从数组创建线时如何找到交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!