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

main.m
% Define constants
I = 1; % Current in Amperes
N = 10; % Number of turns in the coil
radius = 1; % Radius of the coil

% Create a grid for 3D plotting
[x, y, z] = meshgrid(-2:0.1:2, -2:0.1:2, -2:0.1:2);

% Calculate magnetic field components
Bx = (mu_0 * N * I * radius^2 * (x - radius)) ./ ((x - radius).^2 + y^2 + z^2)^(3/2);
By = (mu_0 * N * I * radius^2 * y) ./ ((x - radius).^2 + y^2 + z^2)^(3/2);
Bz = 0;

% Plot the 3D magnetic field
quiver3(x, y, z, Bx, By, Bz, 'b');
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('3D Magnetic Field Generated by 1A Current Through a 10 Turns Coil');
606 chars
20 lines

This code will create a 3D plot of the magnetic field generated by the coil in Matlab. Make sure to define the constant mu_0 which is the permeability of free space.

gistlibby LogSnag