简体   繁体   English

八度中具有数据的抛物线图

[英]Parabola plot with data in Octave

I have been trying to fit a parabola to parts of data where y is positive. 我一直在尝试将抛物线拟合到y为正的部分数据。 I am told, that P1(x1,y1) is the first data point, Pg(xg,yg) is the last, and that the top point is at x=(x1+xg)/2. 有人告诉我, P1(x1,y1)是第一个数据点, Pg(xg,yg)是最后一个数据点,最高点是x=(x1+xg)/2. I have written the following: 我写了以下内容:

x=data(:,1);
y=data(:,2);
filter=y>0;
xp=x(filter);
yp=y(filter);
P1=[xp(1) yp(1)];
Pg=[xp(end) yp(end)];
xT=(xp(1)+xp(end))/2;
A=[1 xp(1) power(xp(1),2) %as the equation is y = a0 + x*a1 + x^2 * a2
   1 xp(end) power(xp(end),2)
   0 1 2*xT]; % as the top point satisfies dy/dx = a1 + 2*x*a2 = 0
b=[yg(1) yg(end) 0]'; %'
p=A\b;
x_fit=[xp(1):0.1:xp(end)]; 
y_fit=power(x_fit,2).*p(3)+x_fit.*p(2)+p(1);

figure 
plot(xp,yp,'*')
hold on 
plot(x_fit,y_fit,'r')

And then I get this parabola which is completely wrong. 然后我得到这个抛物线,这是完全错误的。 It doesn't fit the data at all! 它根本不适合数据! Can someone please tell me what's wrong with the code? 有人可以告诉我代码有什么问题吗?

My parabola 我的抛物线

Well, I think the primary problem is some mistake in your calculation. 好吧,我认为主要问题是您的计算中存在一些错误。 I think you should use three points on the parabola to obtain a system of linear equations. 我认为您应该在抛物线上使用三个点来获得线性方程组。 There is no need to calculate the derivative of your function as you do with 无需像使用方法那样计算函数的导数

dy/dx = a1 + 2*x*a2 = 0 dy / dx = a1 + 2 * x * a2 = 0

Instead of a point on the derivative you choose another point in your scatter plot, eg the maximum: PT = [xp_max yp_max]; 您可以在散点图中选择另一个点,而不是导数上的点,例如最大值:PT = [xp_max yp_max]; and use it for your matrix A and b. 并将其用于矩阵A和b。

The equation dy/dx = a1 + 2*x*a2 = 0 does not fulfill the basic scheme of your system of linear equations: a0 + a1*x + a2*x^2 = y; 方程dy / dx = a1 + 2 * x * a2 = 0不满足您的线性方程组的基本方案:a0 + a1 * x + a2 * x ^ 2 = y;

By the way: If you don't have to calculate your parabola necessarily in this way, you can maybe have a look at the Matlab/Octave-function polyfit() which calculates the least squares solution for your problem. 顺便说一句:如果您不必一定要用这种方式来计算抛物线,您可以看看Matlab / Octave函数polyfit()来计算问题的最小二乘解。 This would result in a simple implementation: 这将导致一个简单的实现:

p = polyfit(x, y, 2);
y2 = polyval(p, x);
figure(); plot(x, y, '*'); hold on;
plot(x, y2, 'or');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM