Giới thiệu
Chương trình viết trên Matlab này mô phỏng điện trường bao quanh hệ nhiều điện tích điểm. 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. Tại mỗi điểm như thế, vector cường độ điện trường được tính toán theo định luật Coulomb dành cho điện tích điểm, kết hợp nguyên lý chồng chất dành cho hệ nhiều điện tích điểm.
Cách sử dụng
Ta có thể thêm bớt số điện tích, cũng như thay đổi vị trí của chúng tại phần INPUT DATA như bên dưới. Việc lựa chọn tham số “streak_arrow” sẽ cho phép minh hoạ vector thường hoặc “vector cong”, giúp việc biểu diễn thêm sinh động.
1 2 3 4 5 6 7 8 9 10 | %% INPUT DATA Q = [2 -2]*1e-9; % charges xQ = [-1 1]; % coordinates of charges yQ = [0 0]; zQ = [0 0]; streak_arrow = 1; % 1-MUI TEN CONG, 0-MUI TEN THANG xmin = -2; xmax = 2; ymin = -1; ymax = 1; zmin = -1; zmax = 1; |
Dưới đây là một vài kết quả mô phỏng thu được.




Nguyên lý mô phỏng
Khi điện tích điểm \(q_i\) nằm ở vị trí \(\vec{r_i}=\{x_i,y_i,z_i\}\) trong không gian, nó tạo ra tại điểm có toạ độ \(\vec{R}=\{X,Y,Z\}\) một điện trường:
\(\vec{E_i}=\frac{1}{4\pi\varepsilon_0}\cdot\frac{q_i}{\lvert\vec{R}-\vec{r_i}\rvert^3}\left(\vec{R}-\vec{r_i}\right).\)Nhằm đơn giản hoá tính toán, ta tính độ lớn của điện trường \(\vec{E_i}\) nói trên, tỉ lệ nghịch với bình phương khoảng cách \(\lvert\vec{R}-\vec{r_i}\rvert\) theo định luật Coulomb:
\(E_i=\frac{1}{4\pi\varepsilon_0}\cdot\frac{q_i}{\Delta r^2},\)trong đó:
\(\Delta r=\lvert\vec{R}-\vec{r_i}\rvert=\sqrt{(X-x_i)^2+(Y-y_i)^2+(Z-z_i)^2}\)Sau đó lấy hình chiếu của vector \(\) lên các trục toạ độ Descartes:
\(\begin{aligned}E_{ix}&=E_i\cos(\widehat{\vec{E_i},Ox}),\\E_{iy}&=E_i\cos(\widehat{\vec{E_i},Oy}),\\E_{iz}&=E_i\cos(\widehat{\vec{E_i},Oz}),\end{aligned}\)hay:
\(\begin{aligned}E_{ix}&=\frac{1}{4\pi\varepsilon_0}\cdot\frac{q_i}{\Delta r^3}(X-x_i),\\E_{iy}&=\frac{1}{4\pi\varepsilon_0}\cdot\frac{q_i}{\Delta r^3}(Y-y_i),\\E_{iz}&=\frac{1}{4\pi\varepsilon_0}\cdot\frac{q_i}{\Delta r^3}(Z-z_i).\end{aligned}\tag{1}\)Nếu hệ được cấu thành từ nhiều điện tích điểm có giá trị từ \(q_1\rightarrow q_N\) thì phép tính (1) viết lại dưới dạng chồng chất:
\(\begin{aligned}E_{x}&=\frac{1}{4\pi\varepsilon_0}\sum\limits_{i=1}^N\frac{q_i}{\Delta r^3}(X-x_i),\\E_{y}&=\frac{1}{4\pi\varepsilon_0}\sum\limits_{i=1}^N\frac{q_i}{\Delta r^3}(Y-y_i),\\E_{z}&=\frac{1}{4\pi\varepsilon_0}\sum\limits_{i=1}^N\frac{q_i}{\Delta r^3}(Z-z_i).\end{aligned}\)Sau cùng, nhờ chức năng của Matlab, ta có thể biểu diễn vector \(\vec{E}=\{Ex,Ey,Ez\}\) trong không gian. Thực tế, cần khai báo tập hợp các vị trí cần tính điện trường:
1 2 3 4 5 6 7 |
Theo code trên, chương trình sẽ tính điện trường cho 20 hàng, 10 cột và 10 lớp, tổng cộng 2000 điểm. Chỉ vài giây tính toán, máy tính có thể làm được việc mà con người phải mất nhiều phút chỉ để làm xong một phần nghìn việc ấy.
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 | function Electric_field_vector_Particles % Author: Tran Hai Cat % Lecturer in Physics, HCM University of Technology and Education % - Dai hoc Su pham Ky thuat Tp. Ho Chi Minh % Created: 2019.02.01 clc; clear all; close all; %% INPUT DATA Q = [2 -2]*1e-9; % charges xQ = [-1 1]; % coordinates of charges yQ = [0 0]; zQ = [0 0]; streak_arrow = 1; % 1-MUI TEN CONG, 0-MUI TEN THANG xmin = -2; xmax = 2; ymin = -1; ymax = 1; zmin = -1; zmax = 1; %% CALCULATION N = length(Q); Nx = 20; Ny = 10; Nz = 10; 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)); for iN = 1:N r = sqrt((X-xQ(iN)).^2+(Y-yQ(iN)).^2+(Z-zQ(iN)).^2); E = ke*Q(iN)./r./r; Ex = Ex+E./r.*(X-xQ(iN)); % x-component of vector field Ey = Ey+E./r.*(Y-yQ(iN)); % y-component of vector field Ez = Ez+E./r.*(Z-zQ(iN)); % z-component of vector field condition = find(r<0.3); Ex(condition) = 0; Ey(condition) = 0; Ez(condition) = 0; end %% FIGURES figure('name','Electric Field Vector 3D','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 charged particles: for iN = 1:N [x1, y1, z1] = sphere(24); radius = 0.1; x1 = x1(:)*radius+xQ(iN); y1 = y1(:)*radius+yQ(iN); z1 = z1(:)*radius+zQ(iN); P = [x1 y1 z1]; P = unique(P,'rows'); shp = alphaShape(P,1.5); plot(shp) end %% 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 |