mysql 多项式_多项式拟合和最小二乘问题

背景知识

The method of least squares is a standard approach in regression analysis to the approximate solution of overdetermined systems, i.e., sets of equations in which there are more equations than unknowns. "Least squares" means that the overall solution minimizes the sum of the squares of the errors made in the results of every single equation.

多项式拟合

The first clear and concise exposition of the method of least squares was published by Legendre in 1805.[5] The technique is described as an algebraic procedure for fitting linear equations to data and Legendre demonstrates the new method by analyzing the same data as Laplace for the shape of the earth. The value of Legendre's method of least squares was immediately recognized by leading astronomers and geodesists of the time.

实验一

%%给定数据对,(x0,y0)

x0=0:0.1:1;

y0=[-.447,1.978,3.11,5.25,5.02,4.66,4.01,4.58,3.45,5.35,9.22];

%%求拟合多项式

n=3;%假设数据对(x0,y0)一共有m对,则通常情况下,应当保证n

%%求得x0,y0数组所给数据的n阶拟合多项式系数向量p,为了保证较好的拟合效果,多项式的阶数要取得适当,过低,残差较大;过高,拟合模型将包含噪声影响。

P=polyfit(x0,y0,n)

%图示拟合情况

xx=0:0.01:1;

yy=polyval(P,xx);

% y = polyval(p,x) returns the value of a polynomial of degree n evaluated

% at x. The input argument p is a vector of length n+1 whose elements are

% the coefficients in descending powers of the polynomial to be evaluated.

%

% y = p1xn + p2xn–1 + … + pnx + pn+1

plot(xx,yy,'-b',x0,y0,'.r','MarkerSize',20)

legend('拟合曲线','原始数据','Location','SouthEast')

xlabel('x')

ed1670453d3aeb888ca7f449ae0b42c9.png

最小二乘

Least squares problems fall into two categories: linear or ordinary least squares and non-linear least squares, depending on whether or not the residuals are linear in all unknowns. The linear least-squares problem occurs in statistical regression analysis; it has a closed-form solution. The non-linear problem is usually solved by iterative refinement; at each iteration the system is approximated by a linear one, and thus the core calculation is similar in both cases.

实验二

%%给定自变量x数据组

x0=(0:0.1:1);

%%给定自变量y数据组

y0=[-.447,1.978,3.11,5.25,5.02,4.66,4.01,4.68,4.45,5.35,9.22]';

m=length(x0);

n=3;

%多项式阶数

%%初始化数据阵X

X=zeros(m,n+1);

%%将X设置成各列等比的形式

for k=1:n

X(:,n-k+1)=(x0.^k);

end

X(:,n+1)=ones(m,1);

%%利用最小二乘原理求解多项式系数

aT=(X\y0)'

aT =

51.9713  -80.2234   37.7844   -0.8078

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值