matlab曲线拟合 新浪,Matlab曲线拟合

曲线拟合

已知离散点上的数据集

,即已知在点集

上的函数值

,构造一个解析函数(其图形为一曲线)使

在原离散点

上尽可能接近给定的

值,这一过程称为曲线拟合。最常用的曲线拟合方法是最小二乘法,该方法是寻找函数

使得

最小。

MATLAB函数:p=polyfit(x,y,n)

[p,s]=

polyfit(x,y,n)

说明:x,y为数据点,n为多项式阶数,返回p为幂次从高到低的多项式系数向量p。x必须是单调的。矩阵s用于生成预测值的误差估计。(见下一函数polyval)

多项式曲线求值函数:polyval( )

调用格式: y=polyval(p,x)

[y,DELTA]=polyval(p,x,s)

说明:y=polyval(p,x)为返回对应自变量x在给定系数P的多项式的值。

[y,DELTA]=polyval(p,x,s) 使用polyfit函数的选项输出s得出误差估计Y

DELTA。它假设polyfit函数数据输入的误差是独立正态的,并且方差为常数。则Y DELTA将至少包含50%的预测值。

=========================

练习:如下给定数据的拟合曲线,x=[0.5,1.0,1.5,2.0,2.5,3.0],

y=[1.75,2.45,3.81,4.80,7.00,8.60]。

解:MATLAB程序如下:

x=[0.5,1.0,1.5,2.0,2.5,3.0];

y=[1.75,2.45,3.81,4.80,7.00,8.60];

p=polyfit(x,y,2)

x1=0.5:0.05:3.0;

y1=polyval(p,x1);

plot(x,y,'*r',x1,y1,'-b')

计算结果为:

p =0.5614 0.8287

1.1560

即所得多项式为y=0.5614x^2+0.8287x+1.15560

另:matlab

polyval与polyvalm的区别是什么?

在坛子里发个网址真费劲,感谢原帖的几位大侠。

http://www.ilovematlab.cn/viewthread.php?tid=2538

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

polyval 只是计算一个多项式,这个你应该明白吧,help里的例子很清楚:

就是把x值带进去。

polyvalm比较特别的。但是明白这个之前,你需要明白什么是 characteristic

polynomial:

In linear algebra, one associates

a polynomial to

every square matrix,

its characteristic

polynomial. This polynomial encodes several important

properties of the matrix, most notably

its eigenvalues,

its determinant and

its trace.

Suppose we want to compute the characteristic polynomial of the

matrix

a4c26d1e5885305701be709a3d33442f.png We

have to compute the determinant of

a4c26d1e5885305701be709a3d33442f.png and

this determinant is

a4c26d1e5885305701be709a3d33442f.png The

latter is the characteristic polynomial

of A.

现在回到我们自己的问题, 对于Y = polyvalm(p,X), 他的计算方法是:

Y = P(1)*X^N + P(2)*X^(N-1) + ... + P(N)*X + P(N+1)*I

如果写出for循环是:

for i = 1:np %多项式长度,m是X的维数

Y = X * Y +

diag(p(i) * ones(m,1));

end

这个循环比较简单,不要我解释吧?

举个例子给你看,我把循环拆开:

复制内容到剪贴板代码:

p=[1 2 3]

X=[1 2; 3 4]

np = length(p);

[m,n] = size(X);

Y = zeros(m,m)

i = 1

Y = X * Y +

diag(p(i) * ones(m,1))

i = 2

Y = X * Y +

diag(p(i) * ones(m,1))

i = 3

Y = X * Y +

diag(p(i) * ones(m,1))

你再运行:

p=[1 2 3]

X=[1 2; 3 4]

polyvalm(p,X)

(By

math)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

总结一下:

polyval(p, t)是计算出来:

p(1)*t^n + p(2)*t^(n-1) + .... + p(n)

polyvalm(p, X)是计算出来:

p(1)*X^n + p(2)*X^(n-1) + .... + p(n)*I

这里要注意最后的I就是单位阵,不要忘了,否则会出错,比如:

>> p=[1 2 3];

>> X=[1 2; 3 4];

>> polyvalm(p, X)

ans =

12 14

21 33

>> p(1)*X*X+p(2)*X+p(3)

ans =

12 17

24 33

>> p(1)*X*X+p(2)*X+p(3)*eye(2,

2)

ans =

12 14

21 33

===============================

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

给个例子。

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)【范数】.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值