我希望在以下数据中使用抛物线。
x = [-10:2:16];
y = [0.0334,0.0230,0.0145,0.0079,0.0033,0.0009,0.0006,0.0026,0.0067,0.0130,0.0213,0.0317,0.0440,0.0580];
[p,~,~] = polyfit(x,y,2);
x2 = linspace(-10,16,100);
y2 = polyval(p,x2);
y3 = 0.0003.*x2.^2 -0.0006.*x2 + 0.0011;
figure
plot(x,y,'o',x2,y2,x2,y3)
然而,拟合与数据完全不匹配。在将数据放入excel并使用二阶多项式进行拟合之后,我得到了非常好的拟合。 y = 0.0003x2 - 0.0006x + 0.0011(excel截断系数使拟合偏斜一点)。 polyfit与这些数据有什么关系?
答案 0 :(得分:3)
解决。
Matlab检查用户请求的输出数量。由于我请求三个输出,即使我没有使用它们,polyfit会更改系数以映射到不同的域xhat。
如果我改为:
p = polyfit(x,y,2);
plot(x2,polyval(p,x2));
然后我会取得适当的结果。要使用三个输出恢复相同的答案:
[p2,S,mu] = polyfit(x,y,2);
xhat = (x2-mu(1))./mu(2)
y4 = polyval(p2,xhat)
plot(x2,y4)
答案 1 :(得分:0)