Search code examples
functionoctave

My function in Octave doesn't work


I'm working with Octave4.2.1 and I have written this function (in the file OctaveFunction.m):

    function y = squareNumber(x)
      y = x^2;
    endfunction

but if I call the function, I get this error:

error: 'squareNumber' undefined near line 1 column 1

and if I try to call the function in this way:

OctaveFunction squareNumber(4)

I get another error:

warning: function name 'squareNumber' does not agree with function filename 'C:\Users\HOME\Desktop\OctaveFunction.m' error: for x^A, A must be a square matrix. Use .^ for elementwise power. error: called from OctaveFunction at line 2 column 7

Where did I go wrong? Thank you!


Solution

  • I think the main problem is that your file name does not match the function name. If you were to match these, this should resolve your first error.


    Regarding the elementwise power error: If given the proper input (4) this should not lead to an error, as 4 is obviously a square matrix.

    Hence it seems that some undesired input is fed to your function, but again this problem is likely to disappear if you rename the file to match the functionname, and call the function as usual. (so without OctaveFunction).