[matlab]基础与应用笔记3

来源:互联网 发布:mac版flashplayer 编辑:程序博客网 时间:2024/04/30 03:47


其他绘图命令

  ezplot(f,[xmin,xmax]) %默认横坐标范围[-2*pi,2*pi]

% Easy to use function plotter

>> ezplot('tan(x)')

>> syms x y
>> F=x^4 + y^4 - 8 * x^2 - 10 * y ^2 + 16;
>> ezplot(F)



FPLOT(FUN,LIMS) plots the function FUN between the x-axis limits
    specified by LIMS = [XMIN XMAX]. Using LIMS = [XMIN XMAX YMIN YMAX]
    also controls the y-axis limits. FUN(x) must return a row vector for
    each element of vector x.

FUN必须为M文件的函数名或对变量x的可执行字符串,下例为匿名函数

f(x) = {  x+1,     x< 1 

              1+1/x, x>=1


>> fun1=@(x)(x+1)*(x<1)+(1+1/x)*(x>=1);
>> fplot(fun1,[-3,3])


多项式回归: 

例如,对两组数据用多项式曲线进行拟合。

>> x= [...]; y=[...]; %输入x与y的数据

>> polyfit(x,y,n)   %n为多项式的次数

如:某司每年收入(单位: 百万元)

    t: 2.4     t+1  : 5                t +2 :8              t+3:12                  t+4:17.4

尝试拟合

>> t=[0,1,2,3,4];
>> R=[2.4,5,8,12,17.4];

>> plot(t,R,'*')  % 散点图



>> polyfit(t,R,3)


ans =


    0.0833   -0.0286    2.5310    2.4029


即 拟合函数:y(t) = 0.0833t³ -0.0286t² + 2.5310t + 2.4029


>>hold on 

>> y = 0.0833 .* t.^3 -0.0286 .* t.^2 + 2.5310 .* t + 2.4029               %回归验证


y =


    2.4029    4.9886    8.0169   11.9876   17.4005                


>> plot(t,y,'r')   %拟合曲线



或者单独回归

>> plot(t,y,'r')
>> subs(y,t,[0,1,2,3,4])


ans =


    2.4029    4.9886    8.0169   11.9876   17.4005

0 0
原创粉丝点击