【发布时间】:2014-02-04 23:38:41
【问题描述】:
我一直在尝试将抛物线拟合到 y 为正的部分数据。有人告诉我,P1(x1,y1) 是第一个数据点,Pg(xg,yg) 是最后一个数据点,最高点是 x=(x1+xg)/2. 我写了以下内容:
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')
然后我得到这条完全错误的抛物线。完全不符合数据!谁能告诉我代码有什么问题?
【问题讨论】: