用matlab绘制三维空间球体的电场线

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<a) = NaN; % 圆外部(设置内部为NaN)
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)

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Matlab绘制三维空间下的光线反射图,你可以使用以下步骤: 1. 创建一个三维坐标系: ```matlab figure; hold on; grid on; axis equal; xlabel('X'); ylabel('Y'); zlabel('Z'); ``` 2. 定义反射面的几何形状: ```matlab % 定义反射面的顶点坐标 vertices = [x1, y1, z1; x2, y2, z2; x3, y3, z3; ...]; % 绘制反射面 patch('Vertices', vertices, 'Faces', [1 2 3], 'FaceColor', 'blue', 'FaceAlpha', 0.5); ``` 3. 定义光源位置、入射光线方向和反射光线方向: ```matlab % 定义光源位置 light_pos = [x_light, y_light, z_light]; % 定义入射光线方向 incident_dir = [dir_x, dir_y, dir_z]; % 定义反射光线方向 reflection_dir = [refl_x, refl_y, refl_z]; ``` 4. 绘制入射光线和反射光线: ```matlab % 绘制入射光线 plot3([light_pos(1), light_pos(1)+incident_dir(1)], [light_pos(2), light_pos(2)+incident_dir(2)], [light_pos(3), light_pos(3)+incident_dir(3)], 'r', 'LineWidth', 2); % 绘制反射光线 plot3([light_pos(1), light_pos(1)+reflection_dir(1)], [light_pos(2), light_pos(2)+reflection_dir(2)], [light_pos(3), light_pos(3)+reflection_dir(3)], 'g', 'LineWidth', 2); ``` 5. 添加光源和观察者的位置标记: ```matlab % 绘制光源位置标记 scatter3(light_pos(1), light_pos(2), light_pos(3), 'filled', 'MarkerFaceColor', 'yellow', 'MarkerEdgeColor', 'black'); % 绘制观察者位置标记 observer_pos = [x_observer, y_observer, z_observer]; scatter3(observer_pos(1), observer_pos(2), observer_pos(3), 'filled', 'MarkerFaceColor', 'magenta', 'MarkerEdgeColor', 'black'); ``` 完整的代码示例如下所示,你可以根据实际情况修改参数: ```matlab % 创建三维坐标系 figure; hold on; grid on; axis equal; xlabel('X'); ylabel('Y'); zlabel('Z'); % 定义反射面的顶点坐标 vertices = [0, 0, 0; 1, 0, 0; 0, 1, 0]; % 绘制反射面 patch('Vertices', vertices, 'Faces', [1 2 3], 'FaceColor', 'blue', 'FaceAlpha', 0.5); % 定义光源位置 light_pos = [0.5, 0.5, 2]; % 定义入射光线方向 incident_dir = [0, 0, -1]; % 定义反射光线方向 reflection_dir = [0, 0, 1]; % 绘制入射光线 plot3([light_pos(1), light_pos(1)+incident_dir(1)], [light_pos(2), light_pos(2)+incident_dir(2)], [light_pos(3), light_pos(3)+incident_dir(3)], 'r', 'LineWidth', 2); % 绘制反射光线 plot3([light_pos(1), light_pos(1)+reflection_dir(1)], [light_pos(2), light_pos(2)+reflection_dir(2)], [light_pos(3), light_pos(3)+reflection_dir(3)], 'g', 'LineWidth', 2); % 绘制光源位置标记 scatter3(light_pos(1), light_pos(2), light_pos(3), 'filled', 'MarkerFaceColor', 'yellow', 'MarkerEdgeColor', 'black'); % 绘制观察者位置标记 observer_pos = [0.5, 0.5, 0]; scatter3(observer_pos(1), observer_pos(2), observer_pos(3), 'filled', 'MarkerFaceColor', 'magenta', 'MarkerEdgeColor', 'black'); ``` 运行以上代码将在Matlab绘制出一个三维空间下的光线反射图。请根据你的实际需求修改参数和形状。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值