plot spectral radiance of a blackbody in matlab

To plot the spectral radiance of a blackbody in MATLAB, we need to use Planck's law and plot the function for a given temperature.

main.m
% Define constants
h = 6.62607004e-34; % Planck's constant [J s]
c = 299792458; % Speed of light [m/s]
k = 1.38064852e-23; % Boltzmann constant [J/K]

% Define blackbody temperature range over which to plot
T = linspace(300, 5000, 1000); % [K]

% Define range of wavelengths for which to calculate spectral radiance
lambda = linspace(1e-9, 3e-6, 1000); % [m]

% Calculate spectral radiance for each temperature and wavelength using Planck's law
for i = 1:length(T)
    B_lambda(i,:) = (2*h*c^2)./((lambda.^5).*(exp((h*c)./(lambda*k.*T(i)))-1)); % [W/(sr m^2 m)]
end

% Plot results
figure
loglog(lambda, B_lambda)
xlabel('Wavelength [m]')
ylabel('Spectral Radiance [W/(sr m^2 m)]')
title('Blackbody Spectral Radiance')
legend(string(T),'Location','northeast')
760 chars
24 lines

In this code, we first define the constants we will need: Planck's constant h, the speed of light c, and the Boltzmann constant k. We then define a temperature range over which to plot the spectral radiance using linspace(). We also define a range of wavelengths over which to calculate the spectral radiance.

Using Planck's law, we calculate the spectral radiance for each temperature and wavelength, storing the results in a matrix B_lambda where rows correspond to temperatures and columns correspond to wavelengths.

Finally, we plot the results using loglog(), with wavelength on the x-axis and spectral radiance on the y-axis. We also add a legend showing the temperature corresponding to each curve.

gistlibby LogSnag