Điện trường thanh dài tích điện

Đây là chương trình viết trên Matlab mô phỏng điện trường bao quanh thanh dài tích điện đều. Vector cường độ điện trường biểu diễn trên mạng lưới 3D các điểm trong không gian. Cường độ điện trường được tính toán bằng cách chia nhỏ thanh dài ra thành nhiều đoạn nhỏ, mỗi đoạn có thể xem như một điện tích điểm, tạo ra điện trường theo định luật Coulomb. Tại mỗi vị trí trong không gian, điện trường được tích phân lại theo các “điện trường Coulomb” từ các phần nhỏ ấy.

Kết quả tính toán thể hiện như hình dưới.

Code chương trình

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
function Electric_field_vector_Rod
% Author: Tran Hai Cat
% Created: 2019.02.01
clc;
clear all;
close all;
%% INPUT DATA
Q = 2e-9; % charges
L = 2;
N = 50;

streak_arrow = 1; % 1-MUI TEN CONG, 0-MUI TEN THANG

xmin = -1; xmax = 3; ymin = -1; ymax = 1; zmin = -1; zmax = 1;
%% CALCULATION
lambda = Q/L;
dx = L/N;
dq = lambda*dx;

Nx = 20;
Ny = 11;
Nz = 11;
x = linspace(xmin,xmax,Nx);
y = linspace(ymin,ymax,Ny);
z = linspace(zmin,zmax,Nz);
[X,Y,Z] = meshgrid(x,y,z);

ke = 9e9;
Ex = zeros(size(X));
Ey = zeros(size(Y));
Ez = zeros(size(Z));

r1 = sqrt(Y.^2+Z.^2);
x_rod = 0;
for iN = 1:N
r = sqrt((X-x_rod).^2+Y.^2+Z.^2);
E = ke*dq./r./r;
Ex = Ex+E./r.*(X-x_rod); % x-component of vector field
Ey = Ey+E./r.*Y; % y-component of vector field
Ez = Ez+E./r.*Z; % z-component of vector field

condition = find((r1<0.3)&(X>0)&(X<2));
Ex(condition) = 0;
Ey(condition) = 0;
Ez(condition) = 0;

x_rod = x_rod+dx;
end

%% FIGURES
figure('name','Electric Field Rod','color','w','numbertitle','off');
if streak_arrow
streakarrow3d(X,Y,Z,Ex,Ey,Ez,2,1);
else
quiver3(X,Y,Z,Ex,Ey,Ez,'AutoScaleFactor',3);
end
hold on

% Draw the rod:
x1 = linspace(0,2,2);
phi = linspace(0,2*pi,20);
y1 = 0.1*cos(phi);
z1 = 0.1*sin(phi);
[X1,Y1] = meshgrid(x1,y1);
Z1 = zeros(size(Y1));
for i = 1:length(x1)
Z1(:,i,:) = z1;
end
mesh(X1,Y1,Z1,'FaceColor','interp');

%% Axis properties
axis equal
axis([xmin xmax ymin ymax zmin zmax]);
rotate3d on

xlabel('X, m');
ylabel('Y, m');
zlabel('Z, m');
view(-20,30);
box on
grid off
set(gca,'color','w')

function hh=streakarrow3d(X,Y,Z,U,V,W,np,AC)
%% Bertrand Dano 03-09
% Copyright 1984-2009 The MathWorks, Inc.
DX=abs(X(1,1,1)-X(1,2,1)); DY=abs(Y(1,1,1)-Y(2,1,1)); DZ=abs(Z(1,1,1)-Z(1,1,2));
DD=min([DX DY DZ]);
ks=DD/100; % Size of the "dot" for the tuft graphs
np=np*10;
alpha = 3; % Size of arrow head relative to the length of the vector
beta = .15; % Width of the base of the arrow head relative to the length
XYZ=stream3(X,Y,Z,U,V,W,X,Y,Z);
Vmag=sqrt(U.^2+V.^2+W.^2);
Vmin=min(Vmag(:));Vmax=max(Vmag(:));
Vmag=Vmag(:);
cmap=colormap;
for k=1:length(XYZ)
F=XYZ(k); [L M]=size(F{1});
if L<np
F0{1}=F{1}(1:L,:);
if L==1
F1{1}=F{1}(L,:);
else
F1{1}=F{1}(L-1:L,:);
end

else
F0{1}=F{1}(1:np,:);
F1{1}=F{1}(np-1:np,:);
end
P=F1{1};
if AC==1
vcol=floor((Vmag(k)-Vmin)./(Vmax-Vmin)*64); if vcol==0; vcol=1; end
COL=[cmap(vcol,1) cmap(vcol,2) cmap(vcol,3)];
else
COL='k';
end
hh=streamline(F0);
set(hh,'color',COL,'linewidth',.5);

if L>1
x1=P(1,1); y1=P(1,2); z1=P(1,3);
x2=P(2,1); y2=P(2,2); z2=P(2,3);
u=x2-x1; v=y2-y1; w=z2-z1; uv=sqrt(u.*u + v.*v);

xa1=x2+u-alpha*(u+beta*(v+eps)); xa2=x2+u-alpha*(u-beta*(v+eps)); xa3=x2+u-alpha*u;
ya1=y2+v-alpha*(v-beta.*(u+eps)); ya2=y2+v-alpha*(v+beta.*(u+eps)); ya3=y2+v-alpha*v;
za1=z2+w-alpha*w; za2=z2+w-alpha*(w+beta.*(uv+eps)); za3=z2+w-alpha*(w-beta.*(uv+eps));

plot3([x2 xa1 xa2 x2 xa3 xa3 x2],[y2 ya1 ya2 y2 ya3 ya3 y2],[z2 za1 za1 z2 za2 za3 z2],'color',COL); hold on
end



end
axis image