





Code chương trình Matlab
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 138 139 140 141 142 143 144 145 146 147 | function E_Field_2_Spheres % 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.08.03 clc; clear variables; close all; %% INPUT DATA FOR USER: L = 2; % distance between spheres R = 25e-2; % sphere radius a = L/4; % 2a=L - distance between spheres Vmax = 100; Vmin = -100; Nx = 100; Ny = 50; eps = (Vmax-Vmin)/1e5; contour_range_V = linspace(Vmin,Vmax,41); xmin = -2; xmax = 2; ymin = -1; ymax = 1; %% CALCULATION x = linspace(xmin,xmax,Nx); y = linspace(ymin,ymax,Ny); mpx = ceil(Nx/2); % Mid-point of x mpy = ceil(Ny/2); % Mid point of y hx = (xmax-xmin)/(Nx-1); hy = (ymax-ymin)/(Ny-1); A = 2/hx/hx+2/hy/hy; B = 1/hx/hx; C = 1/hy/hy; V = zeros(Nx,Ny); % Potential (Voltage) matrix V_const = zeros(Nx,Ny); % Initializing edges potentials V(1,:) = 0; V(Nx,:) = 0; V(:,1) = 0; V(:,Ny) = 0; % Initializing Corner potentials V(1,1) = 0.5*(V(1,2)+V(2,1)); V(Nx,1) = 0.5*(V(Nx-1,1)+V(Nx,2)); V(1,Ny) = 0.5*(V(1,Ny-1)+V(2,Ny)); V(Nx,Ny) = 0.5*(V(Nx,Ny-1)+V(Nx-1,Ny)); % Sphere position: dis = ceil(Nx*a/(xmax-xmin)); px1 = mpx-dis; px2 = mpx+dis; % Initializing Sphere Potentials: phi = linspace(0,2*pi,100); for i = 1:length(phi) x_sphere = R*cos(phi(i)); px_sphere = px1+ceil(Nx*x_sphere/(xmax-xmin)); y_sphere = R*sin(phi(i)); py_sphere = mpy+ceil(Ny*y_sphere/(ymax-ymin)); V(px_sphere,py_sphere) = Vmax; V_const(px_sphere,py_sphere) = 1; end for i = 1:length(phi) x_sphere = R*cos(phi(i)); px_sphere = px2+ceil(Nx*x_sphere/(xmax-xmin)); y_sphere = R*sin(phi(i)); py_sphere = mpy+ceil(Ny*y_sphere/(ymax-ymin)); V(px_sphere,py_sphere) = Vmin; V_const(px_sphere,py_sphere) = 1; end p = 1e100; V_old = V; while p>eps for i=2:Nx-1 for j=2:Ny-1 if V_const(i,j)==0 V(i,j)=1/A*(B*(V(i+1,j)+V(i-1,j))+C*(V(i,j+1)+V(i,j-1))); end end end Delta_V = abs(V-V_old); p = max(Delta_V(:)); V_old = V; error = p/(Vmax-Vmin); fprintf('error=%f\n',error); end % Take transpose for proper x-y orientation V = V'; [Ex,Ey]=gradient(V); Ex = -Ex; Ey = -Ey; E = sqrt(Ex.^2+Ey.^2); Emax = max(E(:)); %% FIGURES: figure('name','Electric Field Magnitude','color','w','numbertitle','off'); hold on pcolor(x,y,E) axis image shading interp; colormap jet quiver(x,y,Ex,Ey,2) colorbar('location','eastoutside','fontsize',14); xlabel('x [m]','fontsize',14); ylabel('y [m]','fontsize',14); title('Electric Field Magnitude'); set(gca,'fontsize',14); axis equal figure('name','Electric Field Magnitude','color','w','numbertitle','off'); hold on pcolor(x,y,E) axis image shading interp; colormap hot quiver(x,y,Ex,Ey,2); colorbar('location','eastoutside','fontsize',14); xlabel('x [m]','fontsize',14); ylabel('y [m]','fontsize',14); title('Electric Field Magnitude'); set(gca,'fontsize',14); axis equal figure('name','Electric Field and Potential distribution','color','w','numbertitle','off'); pcolor(x,y,V) axis image shading interp; colormap jet hold on, contour(x,y,V,contour_range_V,'linewidth',0.5,'linecolor','k'); quiver(x,y,Ex,Ey,2) colorbar('location','eastoutside','fontsize',14); xlabel('x [m]','fontsize',14); ylabel('y [m]','fontsize',14); title('Electric field and Potential distribution'); set(gca,'fontsize',14); axis equal |