plot 10 loop coil and the surrounding magnetic field when a !a current flows in matlab

main.m
% Parameters
current = 5; % Amperes
radius = 1; % meters
loops = 10; % number of loops
segments = 100; % number of segments to divide each loop into

% Create coil
theta = linspace(0, 2*pi, segments+1);
x = radius * cos(theta);
y = radius * sin(theta);

% Plot the coil
figure;
hold on;
for i = 1:loops
    plot(x + 2*i, y, 'k', 'LineWidth', 2); % Add separation between loops for better visualization
end
axis equal;
xlabel('X');
ylabel('Y');
title('Current Loop Coil');

% Calculate magnetic field
[X,Y] = meshgrid(-5:0.1:5, -5:0.1:5);
Bx = zeros(size(X));
By = zeros(size(Y));

for i = 1:loops
    R = sqrt((X - 2*i).^2 + Y.^2);
    B = (current * radius^2) ./ (2 * R.^3);
    theta = atan2(Y, X - 2*i);
    Bx = Bx + B .* cos(theta);
    By = By + B .* sin(theta);
end

% Plot the magnetic field
figure;
quiver(X, Y, Bx, By);
xlabel('X');
ylabel('Y');
title('Magnetic Field around Coil');
893 chars
42 lines

related categories

gistlibby LogSnag