user3460758
user3460758

Reputation: 987

Parse Error compiling Octave script on Ubuntu

I'm not sure what this error is in relation to (that's why I included Ubuntu in the title, I don't know if this is related to the error).

I have a simple Octave script to practice numerical integration that looks like:

function y = f(x)
     y = x.*sin(1./x) .* sqrt(abs(1-x));
endfunction 

[q, ier, nfun, err] = quad("f", 0, 3)

and the error I'm getting is:

parse error near line 11 of file /home/ariel/Desktop/Ariel/Programming/Octave_Programming/SimpsonsRule.m

  syntax error

>>> This is GNU Emacs 23.3.1 (x86_64-pc-linux-gnu, GTK+ Version 2.24.10)
                                                               ^

I'm confused about this error as there is no line 11 in the code?

Please note: I'm very new at programming and trying to learn a few languages at once so forgive what is probably a very simple problem, I wasn't able to find a solution after searching online.

Thanks in advance!

Upvotes: 1

Views: 2305

Answers (2)

hoc_age
hoc_age

Reputation: 1995

Looks like this is a common "Hello, World!" type of exercise; identical code and arguments here.

You'll get an "ABNORMAL RETURN..." statement because your lower limit (x=0) is in the denominator of an expression in your function (...sin(1./x)...). Set the lower limit to something very small like .001 or something as in link above to avoid the discontinuity.

Putting exactly that code (even without the 1; as suggested by @Andy) in a file (call it anything you want; your file name seemed to be SimpsonsRule.m), then running it from the command line,

octave SimpsonsRule.m

Gives the output that @Andy reports. Changing the function call to

quad("f", 0.001, 3)

yields

q =  1.9819
ier = 0
nfun =  3297
err =  2.8021e-08

You're almost there!

If you're interested in learning another language, take a look at the Fortran source that underpins the implementation of Octave's quad function! I like Fortran, but I'm mostly kidding.


Edits:

You have options here.

  1. If you are on the Octave command line, copy and paste exactly those lines into the command line all at once (function ... through quad("f",0,3)) and it will work.
  2. Put exactly the same content (function ... through quad("f",0,3)) into a file of any name (e.g., SimpsonsRule.m) and invoke it from the shell (bash or whatever, not octave prompt) as octave SimpsonsRule.m and it will work.
  3. Or the more complicated solution: put the function definition (only) in a file by itself, then call the function separately. More on this below.

Octave (and MATLAB) has conventions about file names and functions. Just like in other programming languages, Octave knows to search certain directories and certain file names to find a function that you call. When you call a function that Octave doesn't know (e.g., if you call foo(1), it will search through a set of directories for a file name called foo.m (i.e., same name as function with .m extension).

So your function is called f. Create a file called f.m and put exactly the function definition of f:

function y = f(x)
   y = x.*sin(1./x) .* sqrt(abs(1-x));
endfunction 

Then start octave session from your shell (prompt represented by $),

$ octave

Then call your shiny new f function:

octave:1> f(2)

and Octave will report

ans =  0.95885

Now run the quad command, which calls your function f:

octave:2> [q, ier, nfun, err] = quad("f", 0.001, 1)

And see what it does!

Keep in mind that f is probably not a good function name -- it doesn't describe at all what the function does and is likely to collide with some other function, although it's not harmful in this case.

If you're serious about learning Octave, I'd recommend starting with some tutorials.

Upvotes: 1

Andy
Andy

Reputation: 8091

Do you want to use GNU Octave from inside Emacs? This could lead some problems because the plots with fltk toolkit are redrawn from readline (which is disabled in Emacs mode).

Another point: If your script file is SimpsonsRule.m, it has to begin with the function declaration of SimpsonsRule or you add some dummy statement like 1;:

1;
function y = f(x)
     y = x.*sin(1./x) .* sqrt(abs(1-x));
endfunction 

[q, ier, nfun, err] = quad("f", 0, 3)

when running this script octave returns

octave:1> SimpsonsRule
 ABNORMAL RETURN FROM DQAGP
q =  1.9819
ier =  1
nfun =  5061
err =    1.1522e-07

and q looks fine for me

Upvotes: 0

Related Questions