Function 八度:在函数文件中调用函数

Function 八度:在函数文件中调用函数,function,octave,Function,Octave,调用文件函数定义文件中的函数是否可行?我对此很好奇。谢谢你的回答 %It would prove efficient to write a function since we are going %to do the same thing twice. function fleas(N); clear totalflea; %The below vector is for plotting purposes only. Nvector = linspace(0, N, N + 1); %Defi

调用文件函数定义文件中的函数是否可行?我对此很好奇。谢谢你的回答

%It would prove efficient to write a function since we are going
%to do the same thing twice.
function fleas(N);
clear totalflea;
%The below vector is for plotting purposes only.
Nvector = linspace(0, N, N + 1);
%Define the flea vector as follows:
%The value 0 correspond to a fleas residing on dog B 
%(Burnside);thus initially all fleas are on Burnside.
totalflea(1) = 0;
%Since initially we do not have any fleas on Burnside.
fv = zeros(1,50);
for n = 1 : N;
k = randi(50);
%The above code generates a random integer between 1 and 50.
%The code has been implemented in Octave 3.4.
switch fv(k)
    case 0
    fv(k) = 1;
    case 1
    fv(k) = 0;
end
%The above statement changes the values of fv(k) depending
%on its initial value. The possible values are 0 or 1.
totalflea(n + 1) = sum(fv);
endfor
%The following lines are there to depict two standard deviations away
%from the mean value of 25. The standard deviation of a discrete binomial
%variable is found in "Introduction to Probability" by Bertsekas and
%Tsitsiklis. The 2 SD barrier is as follows:
sdp = ones(1, N + 1)*(25 + 2*sqrt(50)/2);
sdm = ones(1, N + 1)*(25 - 2*sqrt(50)/2);
plot (Nvector, totalflea, Nvector, sdp , "1", Nvector, sdm, "1");
% "1" is supplied as an optional argument to determine the color 
%of the graph.
xlabel('Time Steps')
ylabel('Fleas on Anik')
xrange 
endfunction
这很好,但是当我在文件末尾添加行跳蚤(500)时,我得到了一个解析错误。当我将其添加到文件开头时,会出现以下错误:

warning: function 'fleas' defined within script file '/home/ongun/Desktop/Dropbox/Computational Physics/Codes/fleas.m'
error: invalid use of script /home/ongun/Desktop/Dropbox/Computational Physics/Codes/fleas.m in index expression

在octave中,您可以创建函数文件或脚本文件。我将简化一点:

为每个函数创建一个同名文件。以
function开始…
endfunction结束

如果要调用函数,可以使用命令行或装入脚本文件。脚本文件不包含任何函数定义,只需编写要执行的命令即可


因此,您必须创建包含跳蚤(500)
的第二个文件,或者从命令行调用它。

在octave中,您可以创建函数文件或脚本文件。我将简化一点:

为每个函数创建一个同名文件。以
function开始…
endfunction结束

如果要调用函数,可以使用命令行或装入脚本文件。脚本文件不包含任何函数定义,只需编写要执行的命令即可


因此,您必须创建包含跳蚤(500)
的第二个文件,或者从命令行调用它。

它已经在我当前的工作目录中。当我正常调用函数时,它工作;但是,我想调用函数声明文件中的函数,我得到的错误是第31行附近的parse error,其中行号是对应的行。啊,对不起,我只是在开始时误解了您的答案,您是对的,我应该编写一个单独的脚本文件或使用CLI。这不是真的。您可以在脚本文件中定义函数,一点问题也没有。唯一需要注意的是,脚本中的第一件事不能是函数定义。如果你真的想从一个函数开始,只需添加一个
1在第一行。它已经在我当前的工作目录中。当我正常调用函数时,它工作;但是,我想调用函数声明文件中的函数,我得到的错误是第31行附近的parse error,其中行号是对应的行。啊,对不起,我只是在开始时误解了您的答案,您是对的,我应该编写一个单独的脚本文件或使用CLI。这不是真的。您可以在脚本文件中定义函数,一点问题也没有。唯一需要注意的是,脚本中的第一件事不能是函数定义。如果你真的想从一个函数开始,只需添加一个
1在第一行。