plot a 10 turn coil and the surrounding magnetic field when 1a current flows in matlab

main.m
% Define constants
current = 1; % current in amperes
radius = 0.1; % radius of the coil in meters
num_turns = 10; % number of turns in the coil
mu_0 = 4 * pi * 1e-7; % magnetic permeability of free space

% Create a meshgrid to represent the 2D space around the coil
[X, Y] = meshgrid(-0.5:0.01:0.5, -0.5:0.01:0.5);

% Calculate the magnetic field for each point in the meshgrid
Bx = zeros(size(X));
By = zeros(size(Y));
for i = 1:num_turns
    theta = linspace(0, 2*pi, 100); % angles from 0 to 2pi
    coil_x = radius * cos(theta) * cos(2*pi*i/num_turns);
    coil_y = radius * sin(theta) * sin(2*pi*i/num_turns);
    
    r_x = X - coil_x;
    r_y = Y - coil_y;
    
    r = sqrt(r_x.^2 + r_y.^2);
    
    dB = (mu_0 / (4 * pi)) * (2 * pi * radius^2 * current) ./ (r.^2);
    dBx = dB .* (r_y ./ r);
    DBy = dB .* (-r_x ./ r);
    
    Bx = Bx + dBx;
    By = By + DBy;
end

% Plot the coil
figure;
plot(radius * cos(theta), radius * sin(theta), 'k');
hold on;

% Plot the magnetic field vectors
quiver(X, Y, Bx, By);
title('Magnetic Field of a 10-Turn Coil');
xlabel('X-axis (m)');
ylabel('Y-axis (m)');
1111 chars
41 lines

This code will plot a 10-turn coil and the surrounding magnetic field when 1A current flows through it in MATLAB.

related categories

gistlibby LogSnag