[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Question re function in .m file

From: John B. Thoo
Subject: Re: Question re function in .m file
Date: Wed, 15 Dec 2004 07:54:42 -0800

Mirek and Miquel---

Hi. Thanks for the explanation. I see my misunderstanding, now, I think.

I thought that if I were to put something in a .m file, then I would have to run the file first (like a script). But, I see now, if I put a function in a .m file, then I can (and should) simply call on the function as if it were a built-in function.

And thanks for the explanation about "source" and the value of "pi", as well, and also the pointer to Ginac.


On Dec 15, 2004, at 1:50 AM, Miroslaw Kwasniak wrote:

On Tue, Dec 14, 2004 at 08:27:13PM -0800, John B. Thoo wrote:

Thanks!  Now, how do I understand why typing "source ..." gives no
errors when typing "quaderror" or "quaderror (x)" gives errors?

Paul described function-type m-file implications. "source" does nearly the
same as keyboard input.

In your case - you use Paul suggested modification - it isn't necessary to
do explicite "source"  or quaderror call. Simply the single:

  [v, ier, nfun, err] = quad ("quaderror", -1, 1)

must work because quad evaluating it first argument will call quaderror
function - if it isn't already definied octave search file "quaderror.m" and will signal a warnig in case function name mismatch (as in your case) or error if file "quaderror.m" is missing or it isn't function-type file but a
script or whatever else.

function-type m-file is simply a file that have first statement "function"


On Dec 15, 2004, at 1:18 AM, Miquel Cabanas wrote:


On Tue, Dec 14, 2004 at 08:15:49PM -0800, John B. Thoo wrote:

octave:1> quaderror
error: `x' undefined near line 2 column 11

as mentioned in a previous reply, since quaderror is a function,
you are expected to pass all required arguments to it. That is,
if you define a function (eg `quaderror()' ) with an argument
(eg `x') you are expected to call it with *one* argument, for

octave:1> quaderror (pi / 2)
ans = 0.25000

that works ok because you are using it right

Fewer errors when I type "quaderror (x)" in Octave, but still
errors, yet can still evaluate the function:

octave:2> quaderror (x)
error: `x' undefined near line 2 column 12
error: evaluating argument list element number 1
octave:2> quaderror (pi / 2)
ans = 0.25000

octave is for numerical calculations [1]. Since you have not
assigned a value to `x' before passing it to quaderror(), octave
warns you that `x' is not defined and that quaderror() can't do
anything with it. Try assigning a numerical value to x before calling
quaderror and you will see that it works,

octave:1> x = pi/2;
octave:2> quaderror(x)
ans = 0.25000

BTW, don't get fooled with `pi'. It is not a symbol, it's a
builtin numerical value,

octave:3> pi
pi = 3.1416

That seems weird to me (that I can evaluate the function even
with errors showing), and I am no less confused.

* there are no errors in your function, that's why it works; but
* you are making mistakes when calling it, and those are causing
  the error messages displayed by octave.

On the other side, the octave behaviour is perfectly coherent:

1. you define functions with all required arguments,
2. you must pass functions the required arguments,
3. since octave is meant for numerical calculation [1] your
   arguments must be numbers or numerical variables.

[1] actually, there's a package interfacing to Ginac, a symbolic
    computation program. Visit octave-forge website for further


Miquel E Cabanas ------------------------------------------------------
SeRMN, Universitat Autonoma de Barcelona (address@hidden)

Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:
How to fund new projects:
Subscription information:

reply via email to

[Prev in Thread] Current Thread [Next in Thread]