


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 | function S_field_2_parallel % 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.05.03 clc; clear variables; close all; %% CONSTANTS ke = 9e9; mu0 = 4*pi*1e-7; %% PARAMETERS I = 1; % Ampere L = 10; lambda = 2e-9/L; y_wire_1 = linspace(-2,L,50); x_wire_1 = -0.5*ones(size(y_wire_1)); z_wire_1 = zeros(size(y_wire_1)); y_wire_2 = linspace(L,-2,50); x_wire_2 = 0.5*ones(size(y_wire_1)); z_wire_2 = zeros(size(y_wire_1)); Nx = 21; Ny = 30; Nz = 21; xmin = -1.5; xmax = 1.5; ymin = -1; ymax = L+1; zmin = -1; zmax = 1; %% CALCULATION N = length(x_wire_1)-1; x = linspace(xmin,xmax,Nx); y = linspace(ymin,ymax,Ny); z = linspace(zmin,zmax,Nz); [X,Y,Z] = meshgrid(x,y,z); Ex = zeros(size(X)); Ey = zeros(size(Y)); Ez = zeros(size(Z)); Bx = zeros(size(X)); By = zeros(size(Y)); Bz = zeros(size(Z)); kB = mu0/4/pi*I; cond = []; for i_wire = 1:N % wire 1: dx_wire = x_wire_1(i_wire+1)-x_wire_1(i_wire); dy_wire = y_wire_1(i_wire+1)-y_wire_1(i_wire); dz_wire = z_wire_1(i_wire+1)-z_wire_1(i_wire); Delta_x = X-x_wire_1(i_wire); Delta_y = Y-y_wire_1(i_wire); Delta_z = Z-z_wire_1(i_wire); r = sqrt(Delta_x.^2+Delta_y.^2+Delta_z.^2); dq = lambda*dy_wire; E = ke*dq./r./r; Ex = Ex+E./r.*Delta_x; % x-component of vector field Ey = Ey+E./r.*Delta_y; % y-component of vector field Ez = Ez+E./r.*Delta_z; % z-component of vector field Bx = Bx+kB*(dy_wire*Delta_z-dz_wire*Delta_y)./r.^3; % x-component of vector field By = By+kB*(dz_wire*Delta_x-dx_wire*Delta_z)./r.^3; % y-component of vector field Bz = Bz+kB*(dx_wire*Delta_y-dy_wire*Delta_x)./r.^3; % z-component of vector field cond = [cond;find(r<0.25)]; % wire 2: dx_wire = x_wire_2(i_wire+1)-x_wire_2(i_wire); dy_wire = y_wire_2(i_wire+1)-y_wire_2(i_wire); dz_wire = z_wire_2(i_wire+1)-z_wire_2(i_wire); Delta_x = X-x_wire_2(i_wire); Delta_y = Y-y_wire_2(i_wire); Delta_z = Z-z_wire_2(i_wire); r = sqrt(Delta_x.^2+Delta_y.^2+Delta_z.^2); dq = lambda*dy_wire; E = ke*dq./r./r; Ex = Ex+E./r.*Delta_x; % x-component of vector field Ey = Ey+E./r.*Delta_y; % y-component of vector field Ez = Ez+E./r.*Delta_z; % z-component of vector field Bx = Bx+kB*(dy_wire*Delta_z-dz_wire*Delta_y)./r.^3; % x-component of vector field By = By+kB*(dz_wire*Delta_x-dx_wire*Delta_z)./r.^3; % y-component of vector field Bz = Bz+kB*(dx_wire*Delta_y-dy_wire*Delta_x)./r.^3; % z-component of vector field cond = [cond;find(r<0.3)]; end Ex(cond) = 0; Ey(cond) = 0; Ez(cond) = 0; Bx(cond) = 0; By(cond) = 0; Bz(cond) = 0; Sx = (Ey.*Bz-Ez.*By)/mu0; % x-component of vector field Sy = (Ez.*Bx-Ex.*Bz)/mu0; % y-component of vector field Sz = (Ex.*By-Ey.*Bx)/mu0; % z-component of vector field %% DISPLAY RESULTS figure('name','Electric Field','color','k','numbertitle','off'); hold on quiver3(X,Y,Z,Ex,Ey,Ez,'AutoScaleFactor',1,'color','r'); axis equal axis([xmin xmax 0 L zmin zmax]); rotate3d on xlabel('X, m'); ylabel('Y, m'); zlabel('Z, m'); view(65,27); box off grid off set(gca,'color','k','xcolor','w','ycolor','w','zcolor','w') % Draw wires: plot3(x_wire_1,y_wire_1,z_wire_1,'linewidth',3,'color','cyan'); plot3(x_wire_2,y_wire_2,z_wire_2,'linewidth',3,'color','cyan'); figure('name','Magnetic Field','color','k','numbertitle','off'); hold on quiver3(X,Y,Z,Bx,By,Bz,'AutoScaleFactor',1,'color','b'); axis equal axis([xmin xmax 0 L zmin zmax]); rotate3d on xlabel('X, m'); ylabel('Y, m'); zlabel('Z, m'); view(65,27); box off grid off set(gca,'color','k','xcolor','w','ycolor','w','zcolor','w') % Draw wires: plot3(x_wire_1,y_wire_1,z_wire_1,'linewidth',3,'color','cyan'); plot3(x_wire_2,y_wire_2,z_wire_2,'linewidth',3,'color','cyan'); figure('name','Energy Flux','color','k','numbertitle','off'); hold on quiver3(X,Y,Z,Sx,Sy,Sz,'AutoScaleFactor',1,'color','g'); axis equal axis([xmin xmax 0 L zmin zmax]); rotate3d on xlabel('X, m'); ylabel('Y, m'); zlabel('Z, m'); view(65,27); box off grid off set(gca,'color','k','xcolor','w','ycolor','w','zcolor','w') % Draw wires: plot3(x_wire_1,y_wire_1,z_wire_1,'linewidth',3,'color','cyan'); plot3(x_wire_2,y_wire_2,z_wire_2,'linewidth',3,'color','cyan'); |