MATLAB Answers

### Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

## Gaussian Equation function inside a for loop function.

Michael Valero

### Michael Valero (view profile)

さんによって質問されました 2019 年 1 月 20 日

### Walter Roberson (view profile)

さんによって コメントされました 2019 年 1 月 21 日
So I'm trying to write up code that shows the numerical disperswion upon a Gaussian pulse propagating in free space for different Courant stability factors, but I'm not sure how to implement the Gaussian function within my code to make it work. And for some reason, I think I'm missing a detail when it comes to using the 'function' word for the variable 'mat'. Could someone please help me with this?
clear;
clc;
close all;
%-------------------------------------------------------------------------
% a_ INITIALIZED VALUES --------------------------------------------------
c = 3.*10.^8; % speed of light.
f = 1.*10.^9; % Hz.
omega = 2.*pi.*f; % rotational speed. f is not defined.
j = sqrt(-1); % imaginary value defined.
% k = (2.*pi)/lambda; % propagating wave number. NOTE: 17jan19 at 1111T. Not needed?
k1 = omega/c;
k2 = - omega/c;
j = sqrt(-1); % imaginary value defined.
vpg1 = c; % positive phase velocity.
vpg2 = -c; % negative phase velocity.
%-------------------------------------------------------------------------
% b_ DEFINED VARIABLES ---------------------------------------------------
imax = 200;
nmax = 100;
u = zeros(1, imax);
uhold1 = zeros(1, imax); % array of change across time. S x S matrix.
uhold2 = zeros(1, imax); % array of change across space.
%-------------------------------------------------------------------------
% c_ TIME-STEPPING LOOP --------------------------------------------------
% c_001_ calculation of S.
S1 = 0.99; % 18jan19. Following directions to only set this to 1.
S2 = 0.99;
% c_002_ calculation of N.
%N = lambda/delta_x; % 17jan19. Do I need to define N this way?
% c_003_ for loop generation of values.
for n = 1:nmax
% space calculation
u(1) = 1; % source 1.
for i = 2:imax-1
% time calculation
% equation 2.16 pg 25 Taflove.
u(i) = (S1.^2).*(uhold1(i+1) - 2.*uhold1(i) + uhold1(i-1)) + 2.*uhold1(i) - uhold2(i);
function mat = gauss2d(mat, sigma, center)
gsize = size(mat);
[R,C] = ndgrid(1:gsize(1), 1:gsize(2));
mat = gaussC(R,C, sigma, center);
end
uhold2 = uhold1;
uhold1 = u;
%pause(0.05)
figure
plot(u);
pause(0.05);
xlabel('Grid i Coordinate', 'fontsize', 15);
ylabel('Wave Function u(i)', 'fontsize', 15);
title('Pulse Propagation', 'fontsize', 15);
end

#### 0 件のコメント

サインイン to comment.

## 1 件の回答

### Walter Roberson (view profile)

2019 年 1 月 20 日

functions cannot be defined inside of loops or other control structures. You need to move the function definition to the end of the script. Also you will need to add an "end" after it.

Walter Roberson

### Walter Roberson (view profile)

2019 年 1 月 21 日
clear;
clc;
close all;
%-------------------------------------------------------------------------
% a_ INITIALIZED VALUES --------------------------------------------------
c = 3.*10.^8; % speed of light.
f = 1.*10.^9; % Hz.
omega = 2.*pi.*f; % rotational speed. f is not defined.
j = sqrt(-1); % imaginary value defined.
% k = (2.*pi)/lambda; % propagating wave number. NOTE: 17jan19 at 1111T. Not needed?
k1 = omega/c;
k2 = - omega/c;
j = sqrt(-1); % imaginary value defined.
vpg1 = c; % positive phase velocity.
vpg2 = -c; % negative phase velocity.
%-------------------------------------------------------------------------
% b_ DEFINED VARIABLES ---------------------------------------------------
imax = 200;
nmax = 100;
u = zeros(1, imax);
uhold1 = zeros(1, imax); % array of change across time. S x S matrix.
uhold2 = zeros(1, imax); % array of change across space.
%-------------------------------------------------------------------------
% c_ TIME-STEPPING LOOP --------------------------------------------------
% c_001_ calculation of S.
S1 = 0.99; % 18jan19. Following directions to only set this to 1.
S2 = 0.99;
% c_002_ calculation of N.
%N = lambda/delta_x; % 17jan19. Do I need to define N this way?
% c_003_ for loop generation of values.
for n = 1:nmax
% space calculation
u(1) = 1; % source 1.
for i = 2:imax-1
% time calculation
% equation 2.16 pg 25 Taflove.
u(i) = (S1.^2).*(uhold1(i+1) - 2.*uhold1(i) + uhold1(i-1)) + 2.*uhold1(i) - uhold2(i);
end
uhold2 = uhold1;
uhold1 = u;
%pause(0.05)
figure
plot(u);
pause(0.05);
xlabel('Grid i Coordinate', 'fontsize', 15);
ylabel('Wave Function u(i)', 'fontsize', 15);
title('Pulse Propagation', 'fontsize', 15);
end
function mat = gauss2d(mat, sigma, center)
gsize = size(mat);
[R,C] = ndgrid(1:gsize(1), 1:gsize(2));
mat = gaussC(R,C, sigma, center);
end
Michael Valero

### Michael Valero (view profile)

2019 年 1 月 21 日
Didn't work. I'm starting to think that to define the function definition, I need to define it else where and not in the present code. MATLAB error prompt explains code added at specific line cannot be used in that context.
Walter Roberson

### Walter Roberson (view profile)

2019 年 1 月 21 日
One possibility is that you are using R2016a or earlier. In R2016a or earlier, functions can never be defined within scripts.
However, if you had been using a version as old as that implies, then you would have filled in the version number field or you would have clearly stated it, so I think you might need to ask your instructor to open a technical support case on your behalf.

サインイン to comment.

Translated by