x=-4:0.5:8 yx=polyval(t,x) plot(x,yx)
070数据的拟合与绘图 x=0:0.1:2*pi; y=sin(x); p=polyfit(x,y,5); y1=polyval(p,x) plot(x,y,'b',x,y1,'r')
071求代数式的极限: syms x
f=sym('log(1+2*x)/sin(3*x)'); b=limit(f,x,0)
072求导数与微分 syms x
f=sym('x/(cos(x))^2'); y1=diff(f) y2=int(f,0,1)
078划分网格函数
[x,y]=meshgrid(-2:0.01:2,-3:0.01:5); t=x.*exp(-x.^2-y.^2);
[px,py]=gradient(t,0.05,0.1); td=sqrt(px.^2+py.^2); subplot(221) imagesc(t) subplot(222) imagesc(td) colormap('gray')
079求多次多项方程组的解:
syms x1 x2 a ;
eq1=sym('x1^2+x2=a')
31
eq2=sym('x1-a*x2=0') [x1 x2]=solve(eq1,eq2,x1,x2) v=solve(eq1,eq2) v.x1 v.x2
an1=x1(1),an2=x1(2) an3=x2(1),an4=x2(2)
080求解微分方程:
[y]=dsolve('Dy=-y^2+6*y','y(0)=1','x') s=dsolve('Dy=-y^2+6*y','y(0)=1','x') [u]=dsolve('Du=-u^2+6*u','u(0)=1') w=dsolve('Du=-u^2+6*u','z')
[u,w]=dsolve('Du=-w^2+6*w,Dw=sin(z)','u(0)=1,w(0)=0','z') v=dsolve('Du=-w^2+6*w,Dw=sin(z)','u(0)=1,w(0)=0','z')
081各种显现隐含函数绘图: f=sym('x^2+1') subplot(221) ezplot(f,[-2,2]) subplot(222)
ezplot('y^2-x^6-1',[-2,2],[0,10]) x=sym('cos(t)') y=sym('sin(t)') subplot(223) ezplot(x,y) z=sym('t^2') subplot(224) ezplot3(x,y,z,[0,8*pi])
082极坐标图:
r=sym('4*sin(3*x)') ezpolar(r,[0,6*pi])
083多函数在一个坐标系内:
x=0:0.1:8; y1=sin(x);
32
y2=cos(x); subplot(221) plot(x,y1) subplot(222) plot(x,y1,x,y2) w=[2 3;3 1;4 6] subplot(223) plot(w)
q=[4 6:3 5:1 2] subplot(224) plot(w,q)
084调整刻度图像: x=0:0.1:10; y1=sin(x); y2=exp(x);
y3=exp(x).*sin(x); subplot(221) plot(x,y2) subplot(222) loglog(x,y2) subplot(223) plotyy(x,y1,x,y2)
085等高线等图形,三维图:t=0:pi/50:10*pi; subplot(2,3,1)
plot3(t.*sin(t),t.*cos(t),t.^2) grid on
[x,y]=meshgrid([-2:0.1:2]) z=x.*exp(-x.^2-y.^2) subplot(2,3,2) plot3(x,y,z) box off
subplot(2,3,3) meshz(x,y,z)
subplot(2,3,4) surf(x,y,z)
33
subplot(2,3,5) contour(x,y,z)
subplot(2,3,6) surf(x,y,z) subplot(2,3,5) contour(x,y,z) box off
subplot(2,3,6) contour3(x,y,z) axis off
086统计图
Y=[5 2 1;8 7 3;9 8 6;5 5 5;4 3 2] subplot(221) bar(Y) box off
subplot(222) bar3(Y)
subplot(223) barh(Y)
subplot(224) bar3h(Y)
087面积图
Y=[5 1 2;8 3 7;9 6 8;5 5 5;4 2 3]; subplot(221) area(Y)
grid on
set(gca,'Layer','top','XTick',1:5) sales=[51.6 82.4 90.8 59.1 47.0]; x=90:94;
profits=[19.3 34.2 61.4 50.5 29.4]; subplot(222)
area(x,sales,'facecolor',[0.5 0.9 0.6], 'edgecolor','b','linewidth',2) hold on
area(x,profits,'facecolor',[0.9 0.85 0.7], 'edgecolor','y','linewidth',2) hold off
34
set(gca,'Xtick',[90:94]) set(gca,'layer','top')
gtext('\\leftarrow 销售量') gtext('利润') gtext('费用')
xlabel('年','fontsize',14)
088函数的插值: x=0:2*pi; y=sin(x); xi=0:0.1:8;
yi1=interp1(x,y,xi,'linear') yi2=interp1(x,y,xi,'nearest') yi3=interp1(x,y,xi,'spline') yi4=interp1(x,y,xi,'cublic') p=polyfit(x,y,3) yy=polyval(p,xi) subplot(3,2,1) plot(x,y,'o') subplot(3,2,2) plot(x,y,'o',xi,yy) subplot(3,2,3) plot(x,y,'o',xi,yi1) subplot(3,2,4) plot(x,y,'o',xi,yi2) subplot(3,2,5) plot(x,y,'o',xi,yi3) subplot(3,2,6) plot(x,y,'o',xi,yi4)
089二维插值计算:
[x,y]=meshgrid(-3:0.5:3); z=peaks(x,y);
[xi,yi]=meshgrid(-3:0.1:3); zi=interp2(x,y,z,xi,yi,'spline') plot3(x,y,z) hold on
mesh(xi,yi,zi+15) hold off axis tight
35