


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 159 160 161 162 163 164 | function E_Field_Complex_Body % 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: a = 0.5; b = a/2; Vmax = 100; Nx = 500; Ny = 250; eps = Vmax/1e5; contour_range_V = linspace(0,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)); % Initializing potentials: ap = ceil(Nx*a/(xmax-xmin)); bp = ceil(Ny*b/(ymax-ymin)); V(mpx-ap:mpx+ap,mpy+bp) = Vmax; V(mpx-ap:mpx+ap,mpy-bp) = Vmax; V_const(mpx-ap:mpx+ap,mpy+bp) = 1; V_const(mpx-ap:mpx+ap,mpy-bp) = 1; tan_alpha = b/a; x1 = linspace(-a,0,100); y1 = b-(a+x1)*tan_alpha; for i = 1:length(x1) px = ceil(Nx*(x1(i)-xmin)/(xmax-xmin)); py = ceil(Ny*(y1(i)-ymin)/(ymax-ymin)); V(px,py) = Vmax; V_const(px,py) = 1; end y1 = -b+(a+x1)*tan_alpha; for i = 1:length(x1) px = ceil(Nx*(x1(i)-xmin)/(xmax-xmin)); py = ceil(Ny*(y1(i)-ymin)/(ymax-ymin)); V(px,py) = Vmax; V_const(px,py) = 1; end x1 = linspace(a,2*a,100); y1 = (2*a-x1)*tan_alpha; for i = 1:length(x1) px = ceil(Nx*(x1(i)-xmin)/(xmax-xmin)); py = ceil(Ny*(y1(i)-ymin)/(ymax-ymin)); V(px,py) = Vmax; V_const(px,py) = 1; end y1 = -(2*a-x1)*tan_alpha; for i = 1:length(x1) px = ceil(Nx*(x1(i)-xmin)/(xmax-xmin)); py = ceil(Ny*(y1(i)-ymin)/(ymax-ymin)); V(px,py) = Vmax; V_const(px,py) = 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; clc 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'); 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 |