Symbolic function Gaussian Beam

Actually I want to plot the intensity profile I(x,y,z,lambda,wo) of a gaussian beam. Some how I get confused with handle @(x,y..) and syms. May some one help.
r = @(x,y) sqrt(x^2+y^2); % Distance
k = @(lambda) 2*pi*lambda; % Wavenumber
w = @(w0,z,z0) w0*sqrt(1+(z/z0)^2); % Beamradius
R = @(z,z0) z*(1+(z0/z)^2); %
w0= @(lambda,z0) sqrt(lambda*z0/pi);
z0= @(w0, lambda) pi*w0^2/lambda;
xi= @(z, z0) atan(z/z0);
E = @(x,y,z,E0) ...
E0*w0(1,1) / w(1,z,z0(1,1))...
* exp(-(r(x,y)/w(w0,z,z0(1,1)))^2) ...
* exp(-1i*k(1)*z-xi(z, z0(1,1))) ...
* exp(-1i*k(1)*r(x,y)^2/2/R(z,z0(1,1)));
I=@(x,z) E(x,1,z,1)^2;
figure()
fcontour(I)
Warning: Error updating FunctionContour.

Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To operate on each element of the matrix individually, use POWER (.^) for elementwise power.

回答 (2 件)

Star Strider
Star Strider 2023 年 11 月 9 日
編集済み: Star Strider 2023 年 11 月 9 日

0 投票

You need to use element-wise operations. See the documentation on Array vs. Matrix Operations for details.
Also, you can use the hypot function for ‘r’.
For example, ‘R’ becomes:
R = @(z,z0) z.*(1+(z0./z).^2);
Addition, subtraction, and multiplication of an array by a scalar are element-wise by definition.
.

1 件のコメント

De Le
De Le 2023 年 11 月 10 日
I am not shure this is the issue here. These ment to smybolic linear function, not arrays. Anyways using .* and .^ did not solve the issue. I guess, as Torsten mentionend, my problem here is that i dont know how to use variables and funtion handelns properly.

サインインしてコメントする。

Torsten
Torsten 2023 年 11 月 9 日
編集済み: Torsten 2023 年 11 月 9 日

0 投票

w0 and z0 are function handles. You can't use them like variables in your definitions.
Further, E is a complex-valued function. fcontour can only plot real-valued functions.

2 件のコメント

De Le
De Le 2023 年 11 月 10 日
Is this approched better in case of using variables and function handels properly?
% Variables
syms x y z
% Coeffitents
% lambda w0 E0
% Handels
% r k w R xi z0
lambda = 1;w0 =1;E0 =1;
r(x,y) = sqrt(x^2+y^2); % Distance
k = 2*pi*lambda; % Wavenumber
z0 = pi*w0^2/lambda;
w(z) = w0*sqrt(1+(z/z0)^2); % Beamradius
R(z) = z*(1+(z0/z)^2); %
xi(z) = atan(z/z0);
E(x,y,z) =...
E0*w0 / w(z)...
* exp(-(r(x,y)/w(z))^2) ...
* exp(-1i*k*z-xi(z) ...
* exp(-1i*k*r(x,y)^2/2/R(z)));
I(x,z)= E(x,1,z)^2;
figure()
fcontour(I)
I(1,1)
Further E is complex, but since I=E^2, fcontour has to plot a real.
I(x,z)= E(x,1,z)^2;
figure()
fcontour(I)
Anayways some how it is still not working.
Torsten
Torsten 2023 年 11 月 10 日
編集済み: Torsten 2023 年 11 月 10 日
lambda = 1;
w0 =1;
E0 =1;
z0 = pi*w0^2/lambda;
k = 2*pi*lambda; % Wavenumber
r = @(x,y)sqrt(x.^2+y.^2); % Distance
w = @(z)w0*sqrt(1+(z/z0).^2); % Beamradius
R = @(z) z.*(1+(z0./z).^2); %
xi = @(z)atan(z/z0);
E = @(x,y,z)...
E0*w0 ./ w(z)...
.* exp(-(r(x,y)./w(z)).^2) ...
.* exp(-1i*k*z-xi(z)) ...
.* exp(-1i*k*r(x,y).^2./2./R(z));
I = @(x,z)E(x,1,z).*conj(E(x,1,z));
fcontour(I,[-4 4 -30 1],'Fill','on' )
colorbar

サインインしてコメントする。

質問済み:

2023 年 11 月 9 日

編集済み:

2023 年 11 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by