0

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. 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

1 Answer 1

5

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

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.

Sign up to request clarification or add additional context in comments.

3 Comments

It is already in my current working directory. When I call the function normally it works; however, I want to call the function inside the function declaration file and the error I got is parse error near line 31 where the line number is the corresponding line.
Ah sorry, I just misread your answer at the beginning you are right about that I should write a separate script file or use the CLI.
that is not true. You can define functions in your script files no problem at all. The only thing you need to be careful is that the first thing in your script can't be a function definition. If you really want to start with a function, just add a 1; on the first line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.