Laser Beam Profile: Knife Edge Method Matlab Function for Error Function Fitting

Here is the Matlab function to calculate the beam profile of a Gaussian beam using error function fit.

Fitting function:

Transmission(x-x0) = 0.5(1+erf(sqrt(2).(x-x0)./w))

where x is the position of the knife edge (measured in mm usually), x0 is the beam center, w is the beam waist that we will calculate, erf is the error function.


Here is the Matlab Function:

function [w, x0, fitresult, gof] = knifEdge(x, TransmittedPower)
% knifEdge(x,NormalizedPower)
% Knife edge method: Calculation of beam waist and center of the beam
% Input :
% X Input : Knife position (eg. in mm)
% Y Output: Power for each of the x position.
% Output:
% w = beam waist
% x0 = center of the beam
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.

%% Fit: ‘Error Function Fitting’.
NormalizedPower = TransmittedPower./max(TransmittedPower); % Normalizing Power
x = x-min(x);
lincoeff = polyfit(x, NormalizedPower, 1);
a = lincoeff(1);

[xData, yData] = prepareCurveData( x, NormalizedPower );

% Fit Type
if a<0
ft = fittype( ‘0.5*(1-erf(sqrt(2).*(x-x0)./w))’, ‘independent’, ‘x’, ‘dependent’, ‘y’ );
ft = fittype( ‘0.5*(1+erf(sqrt(2).*(x-x0)./w))’, ‘independent’, ‘x’, ‘dependent’, ‘y’ );
opts = fitoptions( ‘Method’, ‘NonlinearLeastSquares’ );
opts.Display = ‘Off’;
opts.StartPoint = [0.913375856139019 0.63235924622541];

% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );

% Plot fit with data.
figure( ‘Name’, ‘Error Function Fitting’ );
h = plot( fitresult, xData, yData );
legend( h, ‘x vs Normalized Power ‘, ‘Error Function Fitting’, ‘Location’, ‘NorthEast’ );
w = fitresult.w;
x0 = fitresult.x0;
% Label axes
xlabel x
ylabel NormalizedPower
grid on


2 thoughts on “Laser Beam Profile: Knife Edge Method Matlab Function for Error Function Fitting

  1. Hello,

    Thank you for providing the script to find the Knife’s edge beam radius. What beam radius are you actually calculating from this? Is it the 1/e, 1/e2, FWHM…?

    1. Hi,
      It is 1/e^2 radius. However, you have to use curve fitting and just fit a custom function:
      Matlab does not fit directly from the code. The initial parameters need to be set first based on the fitted function.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s