12.07.2015 Views

GNU Octave - Local Sector 7 web page

GNU Octave - Local Sector 7 web page

GNU Octave - Local Sector 7 web page

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 13: Functions and Script Files 101declares the function. The macro DEFUN_DLD and the macros that it depends on are definedin the files ‘defun-dld.h’, ‘defun.h’, and ‘defun-int.h’ (these files are included in theheader file ‘octave/oct.h’).Note that the third parameter to DEFUN_DLD (nargout) is not used, so it is omittedfrom the list of arguments in order to avoid the warning from gcc about an unused functionparameter.The next line,ColumnVector dx (3);simply declares an object to store the right hand sides of the differential equation, and thestatementColumnVector x (args(0).vector_value ());extracts a vector from the first input argument. The vector_value method is used sothat the user of the function can pass either a row or column vector. The ColumnVectorconstructor is needed because the ODE class requires a column vector. The variable argsis passed to functions defined with DEFUN_DLD as an octave_value_list object, whichincludes methods for getting the length of the list and extracting individual elements.In this example, we don’t check for errors, but that is not difficult. All of the <strong>Octave</strong>’sbuilt-in functions do some form of checking on their arguments, so you can check the sourcecode for those functions for examples of various strategies for verifying that the correctnumber and types of arguments have been supplied.The next statementsdx(0) = 77.27 * (x(1) - x(0)*x(1) + x(0)- 8.375e-06*pow (x(0), 2));dx(1) = (x(2) - x(0)*x(1) - x(1)) / 77.27;dx(2) = 0.161*(x(0) - x(2));define the right-hand side of the differential equation. Finally, we can return dx:return octave_value (dx);The actual return type is octave_value_list, but it is only necessary to convert the returntype to an octave_value because there is a default constructor that can automaticallycreate an object of that type from an octave_value object, so we can just use that instead.To use this file, your version of <strong>Octave</strong> must support dynamic linking. To find out ifit does, type the command octave_config_info ("dld") at the <strong>Octave</strong> prompt. Supportfor dynamic linking is included if this command returns 1.To compile the example file, type the command ‘mkoctfile oregonator.cc’ at the shellprompt. The script mkoctfile should have been installed along with <strong>Octave</strong>. Runningit will create a file called ‘oregonator.oct’ that can be loaded by <strong>Octave</strong>. To test the‘oregonator.oct’ file, start <strong>Octave</strong> and type the commandoregonator ([1, 2, 3], 0)at the <strong>Octave</strong> prompt. <strong>Octave</strong> should respond by printing

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!