% Biot-Savart demo for straight wire % assumes that current is set to 4pi/mu_o % test two different length wires (finite) L = [1,10]; dz = 0.1; % two separate meshes for the various length wires z1 = -L(1):dz:L(1); z2 = -L(2):dz:L(2); % let's compare various distances from the wire x = 0.1:0.05:1; xe = x(1):.001:x(end); % fine mesh for exact case % re-allocate B results for each length & each distance B1 = zeros(length(x),1); B2 = B1; % loop over distances, & then also over each element dz for h = 1:length(x) for k = 1:length(z1) B1(h) = B1(h) + dz * x(h) ./ (x(h)^2 + z1(k)^2).^(3/2); end for k = 1:length(z2) B2(h) = B2(h) + dz * x(h) ./ (x(h)^2 + z2(k)^2).^(3/2); end end % exact result is a simple step B = 2./xe; % plot the results clf plot(xe,B,'b-',x,B1,'ko',x,B2,'rd') xlabel('x') ylabel('B_y') title('B-field from a straight wire') legend('L = \infty',['L = ' num2str(L(1))],['L = ' num2str(L(2))]) return