1 可视化方法
在MATLAB中,用于场的可视化的函数有streamline、contour、contourf、quiver,如下表所示。除此之外,mesh、surf、imagesc等很多函数也可以用于场的可视化,这里主要有下面四种。
函数名称 作用 调用方式
streamline 根据向量数据绘制流线图 streamline(X,Y,Z,U,V,W,startx,starty,startz)
contour 创建一个包含矩阵 Z 的等值线的等高线图 contour(X,Y,Z)
contourf 创建一个包含矩阵 Z 的等值线的填充等高线图 contourf(X,Y,Z)
quiver 将向量显示为箭头,用于向量场的可视化 quiver(x,y,u,v)
以下为官方文档中的这三种函数的示例,熟悉函数的使用。
streamline
[x,y] = meshgrid(0:0.1:1,0:0.1:1);
u = x;
v = -y;
startx = 0.1:0.1:1;
starty = ones(size(startx));
streamline(x,y,u,v,startx,starty)
Figure1-1 streamline绘图
contour
注:contourf用法和contour类似。
[X,Y,Z] = peaks;
contour(X,Y,Z,20)
Figure1-2 contour绘图
quiver
[x,y] = meshgrid(0:0.2:2,0:0.2:2);
u = cos(x).*y;
v = sin(x).*y;
quiver(x,y,u,v)
以下是代码:
clc
clear
a = 0.4; E0 = 2;
k1 = 0.05; k0 = 0.01;
[X,Y] = meshgrid(0:0.01:1);
[theta,r] = cart2pol(Y,X);
rout = r; rin = r;
rin(rin>a) = NaN; % 圆内部(设置外部为NaN)
rout(rout Zout = -E0rout.cos(theta)+((k1-k0)/(k1+2k0))E0a^3cos(theta)./rout.^2;
Zin = -(3k0/(k1+2k0))E0rin.*cos(theta);
L1 = isnan(Zout); L2 = isnan(Zin);
Zout(L1==1) = 0; Zin(L2==1) = 0;
Z = Zout+Zin;
z = [-flip(Z);Z];
Z0 = [fliplr(z) z];
[PX,PY] = gradient(-Z0,0.05);
[x,y] = meshgrid(-1:0.01:1+0.01);
vy = 0; vx = 0.1:0.1:1;
[Vx,Vy] = meshgrid(vx,vy);
hold on
[C,h] = contourf(x,y,Z0,100);
set(h,'LineStyle','none')
M = 20;
quiver(x(M:M:end-M,M:M:end-M),y(M:M:end-M,M:M:end-M),PX(M:M:end-M,M:M:end-M),PY(M:M:end-M,M:M:end-M),1,'r');
contour(x,y,Z0,8,'b','ShowText','on');
rectangle('Position',[0-a,0-a,2a,2a],'Curvature',[1,1])
axis equal
colorbar
streamline(x,y,PX,PY,Vx,Vy);
streamline(-x,y,-PX,PY,-Vx,Vy);
streamline(x,-y,PX,-PY,Vx,-Vy);
streamline(-x,-y,-PX,-PY,-Vx,-Vy)