Function definitons are not permitted in this context.

4 ビュー (過去 30 日間)
Mackenzie Pandaleon
Mackenzie Pandaleon 2021 年 9 月 1 日
Hello,
I want to calculate a far field pressure for multiple surface displacements.
I am trying to use the following code :
function P = pression(u_r,xi)
% I have to define the global parameters but it doesn't change the error
s = 0 ;
cpt = 0;
for theta = 0:pas_a:2*pi
s = u_r(1)*xi(1)*besselj(0,k*pi*sin(theta));
for j = 2:1:55
s = u_r(j)*( xi(j)-xi(j-1) )*besselj(0,k*pi*sin(theta));
end
cpt = cpt+1;
P(cpt) = abs(rho*c*k/r*s);
end
end
u_r is the displacement(1x55), xi is the lenght along which the displecment is (1x55), P is the far field pressure (361,1)
As far as I know this calculation works well for a single u_r outside of a function. But I have a table of displacements (13x55) named U where the different dispplacements are; I want to calculate all the far field pressures and store them in a single table (361x13) so as to be able to plot all of them on a single graph.
I initially programmed this with 3 for loops; but this resulted in a very false pressure and most of the results where equal to 0 for some reason; so I quickly abandonned that idea.
So I thought of implementing a function to calculate the pressure but I can't seem to program it.
PS : I will then call this function in a for loop :
RES = zeros(361,13);
for k = 1:13
RES(:,k) = pression(U(:,k),xi);
end
Any help is appreciated!!
Thanks

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 9 月 1 日
"Function definitions are not permitted in this context." occurs in any of the following circumstances:
  • You try to use the function keyword at the command line. function cannot be used at the command line; the only kind of functions that can be defined at the command line are anonymous functions
  • In R2015a or earlier, you try to use the function keyword in the same file as a script file, after the script. It did not become permitted to define a script in a function file until R2015b.
And really, that is it.
You indicate you are using R2017a, so the second of those should not apply. The implication, by elimination, is that you tried to define a function at the command line.
  1 件のコメント
Mackenzie Pandaleon
Mackenzie Pandaleon 2021 年 9 月 1 日
Thank you for that awnser, I ended up putting it in a new file, apparently that was the issue all though I am not sure as to why.
However it may not have been the right solution because I now get an outpu that i always equal to 0.
I am not sure why that is

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

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by