如何寻找最好的拟合曲线?以及matlab中Polyf
编辑: admin 2017-23-02
-
4
【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)【范数】.
类似问题
类似问题1:用matlab求最小二乘法(polyfit)曲线拟合用最小二乘(polyfit)曲线拟合下列数据,并画出多项式曲线x 0.5 1 1.5 2 2.5 3y 1.75 2.45 3.81 4.8 8 8.6 用matlab要怎么做啊,求程序解法[数学科目]
clc
%原数据节点
x=[0.5 1 1.5 2 2.5 3];
y=[1.75 2.45 3.81 4.8 8 8.6];
plot(x,y,'*');
hold on;
%求最小二乘意义下拟合多项式的系数
n=3;%待拟合多项式的次数;
p=polyfit(x,y,n);
%新的插值节点
xx=0:0.01:3;
yy=polyval(p,xx);
plot(xx,yy);
你可以再Matlab帮助文件下,搜索polyfit和polyval这两个命令的含义和用法.
这样,这个问题是很容易实现的.
类似问题2:matlab中polyfit与polyval的功能?
polyfit用于多项式曲线拟合
p=polyfit(x,y,m)
其中,x,y为已知数据点向量,分别表示横,纵坐标,m为拟合多项式的次数,结果返回m次拟合多项式系数,从高次到低次存放在向量p中.
y0=polyval(p,x0)
可求得多项式在x0处的值y0
类似问题3:matlab polyfit拟合出来的函数怎样分析其精确程度?用误差分析怎么算?结果具体怎样分析?
Year=[1625 1830 1930 1960 1974 1987 1999];
Population=[5 10 20 30 40 50 60];
Year1=1625:2020;
Year2=2000:2020;
[P2 S2]=polyfit(Year,Population,3);
Population1=polyval(P2,Year1);
Population2=polyval(P2,Year2);
plot(Year,Population,'*',Year2,Population2,'X',Year1,Population1);
这里的S2是误差的大小~
类似问题4:MATLAB的拟合函数polyfit 的程序代码是什么啊
polyfit.m 在MATLAB安装目录下 \toolbox\matlab\polyfun
function [p,S,mu] = polyfit(x,y,n)
%POLYFIT Fit polynomial to data.
% P = POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) of
% degree N that fits the data Y best in a least-squares sense.P is a
% row vector of length N+1 containing the polynomial coefficients in
% descending powers,P(1)*X^N + P(2)*X^(N-1) +...+ P(N)*X + P(N+1).
%
% [P,S] = POLYFIT(X,Y,N) returns the polynomial coefficients P and a
% structure S for use with POLYVAL to obtain error estimates for
% predictions.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).If the data Y are random,
% an estimate of the covariance matrix of P is (Rinv*Rinv')*normr^2/df,
% where Rinv is the inverse of R.
%
% [P,S,MU] = POLYFIT(X,Y,N) finds the coefficients of a polynomial in
% XHAT = (X-MU(1))/MU(2) where MU(1) = MEAN(X) and MU(2) = STD(X).This
% centering and scaling transformation improves the numerical properties
% of both the polynomial and the fitting algorithm.
%
% Warning messages result if N is >= length(X),if X has repeated,or
% nearly repeated,points,or if X might need centering and scaling.
%
% Class support for inputs X,Y:
% float:double,single
%
% See also POLY,POLYVAL,ROOTS.
% Copyright 1984-2004 The MathWorks,Inc.
% $Revision:5.17.4.5 $ $Date:2004/07/05 17:01:37 $
% The regression problem is formulated in matrix format as:
%
% y = V*p or
%
% 3 2
% y = [x x x 1] [p3
% p2
% p1
% p0]
%
% where the vector p contains the coefficients to be found.For a
% 7th order polynomial,matrix V would be:
%
% V = [x.^7 x.^6 x.^5 x.^4 x.^3 x.^2 x ones(size(x))];
if isequal(size(x),size(y))
error('MATLAB:polyfit:XYSizeMismatch',...
'X and Y vectors must be the same size.')
end
x = x(:);
y = y(:);
if nargout > 2
mu = [mean(x); std(x)];
x = (x - mu(1))/mu(2);
end
% Construct Vandermonde matrix.
V(:,n+1) = ones(length(x),1,class(x));
for j = n:-1:1
V(:,j) = x.*V(:,j+1);
end
% Solve least squares problem.
[Q,R] = qr(V,0);
ws = warning('off','all');
p = R\(Q'*y); % Same as p = V\y;
warning(ws);
if size(R,2) > size(R,1)
warning('MATLAB:polyfit:PolyNotUnique',...
'Polynomial is not unique; degree >= number of data points.')
elseif condest(R) > 1.0e10
if nargout > 2
warning('MATLAB:polyfit:RepeatedPoints',...
'Polynomial is badly conditioned.Remove repeated data points.')
else
warning('MATLAB:polyfit:RepeatedPointsOrRescale',...
['Polynomial is badly conditioned.Remove repeated data points\n' ...
' or try centering and scaling as described in HELP POLYFIT.'])
end
end
r = y - V*p;
p = p.'; % Polynomial coefficients are row vectors by convention.
% S is a structure containing three elements:the triangular factor from a
% QR decomposition of the Vandermonde matrix,the degrees of freedom and
% the norm of the residuals.
S.R = R;
S.df = length(y) - (n+1);
S.normr = norm(r);
类似问题5:matlab中使用polyfit函数报错试用了几个例子,执行polyfit函数时无一例外报错:Attempt to execute SCRIPT polyfit as a function.不知道有没有人可以指点下这错误到底是因为什么?matlab使用一直正常,执行其
>> x=[2138 2150 2177 2204 2241]
y=[825 834 853 873 900]
x =
2138 2150 2177 2204 2241
y =
825 834 853 873 900
>> p1=polyfit(x,y,1)
p1 =
0.7271 -729.4239
%%---------------------
你的语句没有问题.检查一下你是不是自己编了一个polyfit.m的脚本文件,删掉它.或者改个名字.
open polyfit试试