plot a magnetic field around a wire with 1a of current in matlab

main.m
% Constants
I = 1; % Current in Amperes

% Create a grid
[X,Y] = meshgrid(-5:0.1:5, -5:0.1:5);

% Calculate the magnetic field components
Bx = (-I/2*pi) * (Y./((X.^2 + Y.^2))).*(10^7); % x-component of B-field
By = (I/2*pi) * (X./((X.^2 + Y.^2))).*(10^7); % y-component of B-field

% Plotting
quiver(X, Y, Bx, By); % Plotting the magnetic field
xlabel('x-axis');
ylabel('y-axis');
title('Magnetic Field around a Wire with 1A of Current');
439 chars
16 lines

Note: The quiver function is used to plot the magnetic field vectors.

related categories

gistlibby LogSnag