simulate and plot in 3d earths magnetic field in matlab

To simulate and plot the Earth's magnetic field in 3D using Matlab, you need to follow these steps:

  1. Download the World Magnetic Model (WMM) coefficients from the National Geophysical Data Center (NGDC) website:
main.m
url = 'https://www.ngdc.noaa.gov/geomag/WMM/DoDWMM.cgi?model=WMM2020&model_out=1&minLat=-90&maxLat=90&minLon=-180&maxLon=180&yr=2021&mean=IGRF&nDigits=-1&format=coef';
filename = 'WMM2020.COF';
websave(filename, url);
218 chars
4 lines
  1. Define the simulation parameters, such as the latitude, longitude and altitude range:
main.m
lat = linspace(-90,90,50);
lon = linspace(-180,180,100);
alt = linspace(0,50000,100);
[lat_mesh, lon_mesh, alt_mesh] = meshgrid(lat, lon, alt);
144 chars
5 lines
  1. Compute the magnetic field at each point on the grid, using the igrfmagm function from the Geomagnetic Toolbox:
main.m
[X,Y,Z,F] = igrfmagm(2021,lat_mesh,lon_mesh,alt_mesh);
55 chars
2 lines
  1. Visualize the magnetic field as a vector field using the quiver3 function:
main.m
quiver3(lat_mesh, lon_mesh, alt_mesh, X, Y, Z);
xlabel('Latitude');
ylabel('Longitude');
zlabel('Altitude');
title('Earth Magnetic Field');
axis equal;
152 chars
7 lines

This will create a 3D plot of the Earth's magnetic field, where the vectors represent the direction and magnitude of the magnetic field at each point in the grid.

gistlibby LogSnag