A03.m - Calculating the Fine Structure Constant using Pre-Defined Variables
% loads the variables stored in the file constants.mat into memory load('constants'); % calculate the fine structure constant alpha = e^2 / (hbar * c * 4 * pi * eps0); % display the reciprical of the fine structure consant to 5 decimal places fprintf('fine structure constant = 1 / %.5f\n',1/alpha)
This program demonstrates how to read in a .mat file that has previously defined variables and use those variables in a simple calculation. The command load command will read in variables if the argument is a .mat file and will read in data values if it is an ASCII file (more on that later).
The constants.mat file contains values for the following constants all defined in MKS units:
- G = Gravitational constant
- c = speed of light
- e = fundamental charge
- eV = electron volt (in Joules)
- eps0 = permittivity of free space
- h = Planck's constant
- hbar = Planck's constant / 2 pi
- k = Boltzmann constant
- me = mass of electron
- mn = mass of neutron
- mp = mass of proton
- mu0 = permeability of free space
- u = atomic mass unit
Feel free to download this file and add to it or customize it to your needs. To save the variables defined in memory to a file, simply use the command save('filename'). Matlab will automatically add the .mat file extension.