[英]Octave: Calling a function in its function file
Is calling a function in the file functions definition file possible? 是否可以在文件函数定义文件中调用函数? I am pretty curious about it. 我对此很好奇。 Thanks for your answers. 感谢您的回答。
%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
This works fine, yet when I append the line fleas(500) for example at the end of the file I get a parse error. 效果很好,但是当我在文件末尾附加行fleas(500)时,出现解析错误。 When I add it at the beginning of the file I get the following error: 当我在文件的开头添加它时,出现以下错误:
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
In octave you can either create a function file or a script file. 在八度中,您可以创建功能文件或脚本文件。 I will simplify a little bit: 我将简化一下:
For every function create a file with the same name. 为每个函数创建一个具有相同名称的文件。 begins with function ...
ends with endfunction
始于function ...
结尾endfunction
If you want to call functions, you can either use the command line or crate a script file. 如果要调用函数,则可以使用命令行或创建脚本文件。 Script files do not contain any function definition, simply write the commands you want to execute. 脚本文件不包含任何函数定义,只需编写要执行的命令即可。
Thus you have to create a second file containing fleas(500)
or call it from command line. 因此,您必须创建另一个包含fleas(500)
文件或从命令行调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.