如何寻找最好的拟合曲线?以及matlab中Polyfit的用法?

2025-03-04 15:55:33
推荐回答(1个)
回答1:

【1】用cftool
>>cftool
回车,出现一个对话框。【1】Data输入相应的x,y,然后create data set;【2】fitting,选择提供的各种模型,逐个试试,总能找到残差最小的,相关系数最大的一种模型。

【2】给个例子。
clc;clear
x=-1:0.1:1
for k=1:length(x)
y(k)=x(k)^2+0.1*rand
end
[p,s]=polyfit(x,y,2)
s.R
plot(x,y,'o',x,polyval(p,x))

结果:
p =

1.0072 0.0091 0.0439

s =

R: [3x3 double]
df: 18
normr: 0.1153

ans =

-2.2509 -0.0000 -3.4208
0 2.7749 0.0000
0 0 -3.0492

s是个结构数组。
S contains fields for the triangular factor (R) from a QR decomposition of the Vandermonde matrix 【范德蒙特矩阵】of X, the degrees of freedom (df)【自由度】, and the norm of the residuals (normr)【范数】.