# 1 MATLAB统计计算

``````最大值max
[y,l] = max(X,[],dim)

[y,l] = min(X,[],dim)

S = sum(X,dim)

S = prod(X,dim)

y = perms(x)

S = nchooosek(n,m)

S = mean(X,dim)

S = median(S,dim)

y =var(X,dim)

y =std(X,dim)

y =corrcoef(X)

S =cov(X)

Y = cumsum(X,dim)

Y =cumprod(X,dim)

Y = sort(X,dim)

Y = sortrows(X,dim)

Y = range(X)

Y =skewness(X)
``````

# 2 MATLAB绘图

（1）图形窗口的控制

• firgure或figure(N)
• clc
• close
• hold on(off)
• subplot(m,n,p)或subplot(nmp)
（2）二维图形的绘制
• plot命令：二维图形绘制

``````plot(y)
plot(x,y)
plot(x,y,'s')%s表示线的参数，设置颜色，形状等等
plot(x1,y1,'s',x2,y2,'s')
``````

• ezplot命令：快捷二维图形绘制

``````ezplot('f(x)',[a,b])
ezplot('f(x,y)',[s1,s2,y1,y2])
ezplot('x(t)','y(t)',[tmin,tmax])
``````

• ploty命令

``````ploty(x1,y1,x2,y2)
``````

• fplot命令：函数绘图

``````plot('f(x)',[a,b])
``````

• polar极坐标绘图

``````polar(theta,rho)
polar(theta,rho,s)
``````

• 对数坐标轴命令semilogx semilogy loglog

``````semilogx(...)
semilogy(...)
loglog(...)
``````

• 添加标题title

``````title('string')
``````

• 添加坐标轴标志函数xlabel ylabel zlabel

``````xlabel('text')
ylabel('text')
zlabel('text')
``````

• 设置网格线命令

``````grid on(off)
``````

• 图形标注函数legend

``````legend(string1,string2,string3)
legend off
``````

• 设置坐标轴函数axis

``````axis([xmin,xmax,ymin,ymax])
axis([xmin xmax ymin ymax zmin zmax])
axis off(on)
axis('manual')
``````

``````x = linspace(0,2*pi,30);
y =sin(x);
z = cos(x);
plot(x,y,'r',x,z,'go')
legend('sin(x)','cos(x)')
title('三角形函数示意图')
xlable('X')
ylable('Y')
``````

（3）三维图形的绘制

``````• 绘制三维曲线plot3
plot3(x,y,z,'s')

• 生成网课函数meshgrid
[X,Y] = meshgrid(x,y)

• 绘制空间曲面图surf
surf(X,Y,Z)

• 绘制空间网面图mesh
mesh(X,Y,Z)

• 绘制平面等值线contour
contour(X,Y,Z,n)

• 绘制平面流图quiver
quiver(X,Y,U,V)%X，Y是要画的点。UV分别是x和y的梯度，就是导数
``````

``````[X,Y] =meshgrid(-8:.5:8);
R = sqrt(X.^2+Y.^2)+eps;
Z = sin(R)./R;
figure
mesh(Z)
``````

（3）统计绘图

``````• 正整数的频率表tabulate
table = tabulate(X)

• 样本数据的盒图boxplot
boxplot(X)

• 附加有正态密度曲线的直方图histfit
histfit(data)

• 直方图hist
[S,T] = hist(X,n)

• 绘制饼图pie
pie(X)

• 散点图scatter
scatter(x,y)
``````

``````x = randn(1000,3);
hist(x)
``````

# 3 曲线拟合

（1）多项式拟合函数ployfit

``````[p,S] = ployfit(x,y,n)
``````

（2）线性最小二乘拟合函数lsqlin

``````x = lsqlin(C,d,A,b,Aeq,beq,lb,ub)
``````

（3）非线性最小二乘拟合函数lsqcurvefit

``````x = lsqcurvefit(fun,x0,xdata,ydata)
x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub)
x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub,options)
``````

（4）通用拟合函数fit

``````fitobject =fit(x,y,fitType)
fitobject =fit([x,y],z,fitType)
``````

``````t = [0.25 0.5 1 1.5 2 3 4 6 8]
c = [19.21 18.15 15.36 14.10 12.89 9.32 7.45 5.24 3.01]
plot(t,c,'*')
p = polyfit(t,c,2)
t1 = 0.25:0.01:8;
y = polyval(p,t1);
hold on
plot(t1,y)
hold off
``````

# 4 参数估计与假设检验

（1）Beta分布的最大似然估计betafit

``````[p,S] =betafit(x,alpha)
``````

（2）正态分布的参数估计normfit

``````[mu,sig,mci,sci] = normfit(S,alpha)
``````

（3）二项分布的最大似然估计binofit

``````[p,S] = binofit(x,n,alpha)
``````

（4）均匀分布的最大似然估计unfit

``````[a,b,aci,bci] = unfit(x,alpha)
``````

（5）单个正态总体的Z检验法ztest

``````[h,sig,ci] =ztest(x,m,sigma,alpha)
``````

（6）单个正态总体的t检验法ttest

``````[h,sig,ci] = ttest(s,m,alpha)
``````

# 5 课后习题

``````clc
close all;
x=randi(100,1,100);%随机产生100个整数，并生成1*100
% 画频率分布直方图
[counts,centers] = hist(x, 10);
plot(centers,counts,'*')
% 多项式拟合
p = polyfit(centers,counts,5)
t1 = 0:0.01:100;
y = polyval(p,t1);
hold on
plot(t1,y)
hold off
``````

``````x=randi(100,1,100);%随机产生100个整数，并生成1*100
% 画频率分布直方图
[counts,centers] = hist(x, 10);
figure
bar(centers, counts )
cftool
``````

（1）Xdata 为counts，Ydata为centers，Degree为3。欠拟合状态

（2）Xdata 为counts，Ydata为centers，Degree为5。可能拟合最佳

（3）Xdata 为counts，Ydata为centers，Degree为9。过拟合状态