why am i recieving a "parse error" at m=2? how do i fix it?

1 view (last 30 days)
function [E, x, y] = generate_hermite_gauss_beam(m, n, w0, lambda, gridSize, gridLength)
% Generate Hermite-Gauss beam
% Inputs:
% m, n: Hermite polynomial indices (m in x-direction, n in y-direction)
% w0: waist width of the beam
% lambda: wavelength of the beam
% gridSize: number of points in each dimension of the grid
% gridLength: total length of the grid in both x and y directions
% Create coordinate vectors for x and y
x = linspace(-gridLength/2, gridLength/2, gridSize);
y = linspace(-gridLength/2, gridLength/2, gridSize);
% Create a grid for x and y coordinates
[X, Y] = meshgrid(x, y);
% Calculate beam parameters
zR = pi * w0^2 / lambda; % Rayleigh range
w_z = w0 * sqrt(1 + (gridLength / zR)^2); % Beam waist at distance z
R_z = gridLength * (1 + (zR / gridLength)^2); % Radius of curvature
% Calculate Hermite-Gauss beam profile
u = X / w_z;
v = Y / w_z;
Hm = hermitePoly(round(m), u);
Hn = hermitePoly(round(n), v);
amplitude = 1 / (sqrt(2^round(m) * factorial(round(m)) * 2^round(n) * factorial(round(n)))) * ...
exp(-(u.^2 + v.^2) / 2) .* Hm .* Hn;
% Calculate Gaussian phase profile
phase = exp(1i * pi * (X.^2 + Y.^2) / lambda / R_z);
% Calculate the final Hermite-Gauss beam
E = amplitude .* phase;
% Normalize the amplitude to 1
E = E / max(abs(E(:)));
end
function H = hermitePoly(n, x)
% Calculate the Hermite polynomial of order n at x
% using the recursive relation
if n == 0
H = ones(size(x));
elseif n == 1
H = 2 * x;
else
H = 2 * x .* hermitePoly(n - 1, x) - 2 * (n - 1) * hermitePoly(n - 2, x);
end
end
% Define the parameters for the Hermite-Gauss beam
m = 2; % Hermite polynomial index in x-direction
n = 3; % Hermite polynomial index in y-direction
w0 = 1e-3; % Waist width in meters
lambda = 632.8e-9;% Wavelength in meters (HeNe laser)
gridSize = 256; % Number of points in each dimension of the grid
gridLength = 0.2; % Grid size in meters (total length in both x and y directions)
% Generate the Hermite-Gauss beam
[E, x, y] = generate_hermite_gauss_beam(m, n, w0, lambda, gridSize, gridLength);
% Display the amplitude and phase of the beam
figure;
subplot(1, 2, 1);
imagesc(x, y, abs(E).^2);
axis square;
title('Amplitude');
xlabel('x (m)');
ylabel('y (m)');
subplot(1, 2, 2);
imagesc(x, y, angle(E));
axis square;
title('Phase');
xlabel('x (m)');
ylabel('y (m)');
colormap(gca, 'hsv');
colorbar;

Answers (1)

Voss
Voss on 23 Jul 2023
Function definitions in a script must appear at the end, like this:
% Define the parameters for the Hermite-Gauss beam
m = 2; % Hermite polynomial index in x-direction
n = 3; % Hermite polynomial index in y-direction
w0 = 1e-3; % Waist width in meters
lambda = 632.8e-9;% Wavelength in meters (HeNe laser)
gridSize = 256; % Number of points in each dimension of the grid
gridLength = 0.2; % Grid size in meters (total length in both x and y directions)
% Generate the Hermite-Gauss beam
[E, x, y] = generate_hermite_gauss_beam(m, n, w0, lambda, gridSize, gridLength);
% Display the amplitude and phase of the beam
figure;
subplot(1, 2, 1);
imagesc(x, y, abs(E).^2);
axis square;
title('Amplitude');
xlabel('x (m)');
ylabel('y (m)');
subplot(1, 2, 2);
imagesc(x, y, angle(E));
axis square;
title('Phase');
xlabel('x (m)');
ylabel('y (m)');
colormap(gca, 'hsv');
colorbar;
function [E, x, y] = generate_hermite_gauss_beam(m, n, w0, lambda, gridSize, gridLength)
% Generate Hermite-Gauss beam
% Inputs:
% m, n: Hermite polynomial indices (m in x-direction, n in y-direction)
% w0: waist width of the beam
% lambda: wavelength of the beam
% gridSize: number of points in each dimension of the grid
% gridLength: total length of the grid in both x and y directions
% Create coordinate vectors for x and y
x = linspace(-gridLength/2, gridLength/2, gridSize);
y = linspace(-gridLength/2, gridLength/2, gridSize);
% Create a grid for x and y coordinates
[X, Y] = meshgrid(x, y);
% Calculate beam parameters
zR = pi * w0^2 / lambda; % Rayleigh range
w_z = w0 * sqrt(1 + (gridLength / zR)^2); % Beam waist at distance z
R_z = gridLength * (1 + (zR / gridLength)^2); % Radius of curvature
% Calculate Hermite-Gauss beam profile
u = X / w_z;
v = Y / w_z;
Hm = hermitePoly(round(m), u);
Hn = hermitePoly(round(n), v);
amplitude = 1 / (sqrt(2^round(m) * factorial(round(m)) * 2^round(n) * factorial(round(n)))) * ...
exp(-(u.^2 + v.^2) / 2) .* Hm .* Hn;
% Calculate Gaussian phase profile
phase = exp(1i * pi * (X.^2 + Y.^2) / lambda / R_z);
% Calculate the final Hermite-Gauss beam
E = amplitude .* phase;
% Normalize the amplitude to 1
E = E / max(abs(E(:)));
end
function H = hermitePoly(n, x)
% Calculate the Hermite polynomial of order n at x
% using the recursive relation
if n == 0
H = ones(size(x));
elseif n == 1
H = 2 * x;
else
H = 2 * x .* hermitePoly(n - 1, x) - 2 * (n - 1) * hermitePoly(n - 2, x);
end
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!