



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 148 149 150 151 152 153 154 155 156 157 158 | function E_Field_Plate_Sphere % 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.01 clc; clear variables; close all; %% INPUT DATA FOR USER: L = 2; % plate length R = 5e-2; % sphere radius a = 0.5; % 2a - distance between plates Vmax = 100; Vmin = -100; Nx = 100; Ny = 50; eps = (Vmax-Vmin)/1e5; contour_range_V = linspace(Vmin,Vmax,41); xmin = -2; xmax = 2; ymin = -0.5; ymax = 1.5; %% CALCULATION x = linspace(xmin,xmax,Nx); y = linspace(ymin,ymax,Ny); mpx = ceil(Nx/2); % Mid-point of x mpy = ceil(Ny/4); % 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)); % Length of plate in terms of number of grids: length_plate = floor(Nx*L/(xmax-xmin)); lp = floor(length_plate/2); % Position of plate on y axis position_sphere = floor(Ny*a/(ymax-ymin)); pp1 = mpy; pp2 = mpy+position_sphere; % Initializing Sphere Potentials: phi = linspace(0,2*pi,120); for i = 1:length(phi) x_sphere = R*cos(phi(i)); px_sphere = ceil(Nx*(x_sphere-xmin)/(xmax-xmin)); y_sphere = a+R*sin(phi(i)); py_sphere = ceil(Ny*(y_sphere-ymin)/(ymax-ymin)); V(px_sphere,py_sphere) = Vmax; V_const(px_sphere,py_sphere) = 1; end % Initializing Plate Potentials: V(mpx-lp:mpx+lp,pp1) = Vmin; V(mpx,pp2) = Vmax; V_const(mpx-lp:mpx+lp,pp1) = 1; V_const(mpx,pp2) = 1; 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 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 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'); contour(x,y,V,contour_range_V,'linewidth',0.5); hold on, quiver(x,y,Ex,Ey,2) axis([min(x) max(x) min(y) max(y)]); 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 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) axis([min(x) max(x) min(y) max(y)]); 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 |