plot magnetic filed generated by a 1a current through a 10 turns coil in matlab

main.m
% Constants
I = 1; % current in Amperes
N = 10; % number of turns
R = 0.5; % radius of the coil in meters

% Create a grid of points where we want to calculate the magnetic field
x = -2*R:0.1:2*R;
y = -2*R:0.1:2*R;
z = 0;

[X, Y] = meshgrid(x, y);
Z = z * ones(size(X));

% Calculate the magnetic field components (Bx, By, Bz) at each point
mu0 = 4*pi*1e-7; % permeability of free space
Bx = (mu0 * N * I * R^2) ./ (2 * ((X.^2 + Y.^2 + R^2).^(3/2))) .* X;
By = (mu0 * N * I * R^2) ./ (2 * ((X.^2 + Y.^2 + R^2).^(3/2))) .* Y;
Bz = (mu0 * N * I * R^2) ./ (2 * ((X.^2 + Y.^2 + R^2).^(3/2))) .* R;

% Plot the magnetic field using quiver3
figure;
quiver3(X, Y, Z, Bx, By, Bz);
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Magnetic Field Generated by a 1A Current Through a 10-Turn Coil');
grid on;
795 chars
28 lines

This MATLAB code calculates and plots the magnetic field generated by a 1A current through a coil with 10 turns. The coil is assumed to have a radius of 0.5 meters and the magnetic field is visualized using quiver3.

gistlibby LogSnag