您的位置:首页 > 编程语言 > MATLAB

MATLAB-曲线拟合

2015-08-20 03:32 531 查看

曲线拟合

曲线拟合
函数列表
polyfit

polyval

subplot

plot

代码

X0.10.20.150.0-0.20.3
Y0.950.840.861.061.500.72

函数列表

polyfit

函数功能

多项式的拟合运算

调用方法

polyfit(x,y,n)。

x为横坐标,y为纵坐标,n为拟合阶数。

例子

x = (0: 0.1: 2.5) ;1

y = erf(x);

p = polyfit(x,y,6)

p =

0.0084 -0.0983 0.4217 -0.7435 0.1471 1.1064 0.0004

则y=0.0084x^6-0.0983x^5+0.4217x^4-0.7435x^3+0.1471x^2+1.1064x+0.0004

polyval

函数功能

多项式的估值运算

调用方法

polyval(p,x).

返回n次多项式p在x处的值。
输入变量p是一个长度为n+1的向量,其元素为按降幂排列的多项式系数。


例子

对多项式p(x)=3*x^2+2*x+1,计算在x=5,7,9的值。

p = [3 2 1];

x=[5,7,9];

polyval(p,x)

%结果为
ans =
86 162 262


subplot

函数功能

分割figure,创建子坐标系

调用方法

h = subplot(m,n,p) or subplot(mnp);

subplot(m,n,p,’replace’);

subplot(m,n,P);

subplot(h);

subplot(‘Position’,[left bottom width height]);

subplot(…, prop1, value1, prop2, value2, …);

h = subplot(…) ;

subplot(m,n,P)此时p为向量,表示将P中指定的小块合并成一个大块创建坐标系,P中指定的小块可以不连续,甚至不相连。


例子

t=0:0.001:1;

y1=sin(10*t);

y2=sin(15*t);

subplot(211)

plot(t,y1)

subplot(212)

plot(t,y2)1.7432 -1.6959 1.0850

plot

函数功能

绘制线性二维图

调用方法

plot(X,Y)

当X,Y均为实数向量,且为同维向量


例子

x=0:pi/100:2*pi;

y=sin(x);

plot(x,y)

结果得到的是正弦函数曲线


代码

>> x=[0.1 0.2 0.15 0.0 -0.2 0.3];
>> y=[0.95 0.84 0.86 1.06 1.50 0.72];
>> p=polyfit(x,y,2)

p =

1.7432   -1.6959    1.0850

>> xi=-0.2:0.01:0.3;
>> yi=polyval(p,xi);
>> subplot(2,2,1);
>> plot(x,y,'o',xi,yi,'k');
>> title('polyfit');


从0至2.5,每隔0.1取一个数。

如:0,0.1,0.2,…,2.3,2.4,2.5。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  matlab 编辑器 函数 plot x