colloc.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include <string>
00028 
00029 #include "CollocWt.h"
00030 #include "lo-mappers.h"
00031 
00032 #include "defun-dld.h"
00033 #include "error.h"
00034 #include "oct-obj.h"
00035 #include "utils.h"
00036 
00037 DEFUN_DLD (colloc, args, ,
00038   "-*- texinfo -*-\n\
00039 @deftypefn {Loadable Function} {[@var{r}, @var{amat}, @var{bmat}, @var{q}] =} colloc (@var{n}, \"left\", \"right\")\n\
00040 Compute derivative and integral weight matrices for orthogonal\n\
00041 collocation using the subroutines given in J. Villadsen and\n\
00042 M. L. Michelsen, @cite{Solution of Differential Equation Models by\n\
00043 Polynomial Approximation}.\n\
00044 @end deftypefn")
00045 {
00046   octave_value_list retval;
00047 
00048   int nargin = args.length ();
00049 
00050   if (nargin < 1 || nargin > 3)
00051     {
00052       print_usage ();
00053       return retval;
00054     }
00055 
00056   if (! args(0).is_scalar_type ())
00057     {
00058       error ("colloc: N must be a scalar");
00059       return retval;
00060     }
00061 
00062   double tmp = args(0).double_value ();
00063 
00064   if (error_state)
00065     return retval;
00066 
00067   if (xisnan (tmp))
00068     {
00069       error ("colloc: N cannot be NaN");
00070       return retval;
00071     }
00072 
00073   octave_idx_type ncol = NINTbig (tmp);
00074   if (ncol < 0)
00075     {
00076       error ("colloc: N must be positive");
00077       return retval;
00078     }
00079 
00080   octave_idx_type ntot = ncol;
00081   octave_idx_type left = 0;
00082   octave_idx_type right = 0;
00083 
00084   for (int i = 1; i < nargin; i++)
00085     {
00086       if (args(i).is_defined ())
00087         {
00088           if (! args(i).is_string ())
00089             {
00090               error ("colloc: expecting string argument \"left\" or \"right\"");
00091               return retval;
00092             }
00093 
00094           std::string s = args(i).string_value ();
00095 
00096           if ((s.length () == 1 && (s[0] == 'R' || s[0] == 'r'))
00097               || s == "right")
00098             {
00099               right = 1;
00100             }
00101           else if ((s.length () == 1 && (s[0] == 'L' || s[0] == 'l'))
00102                    || s == "left")
00103             {
00104               left = 1;
00105             }
00106           else
00107             {
00108               error ("colloc: unrecognized argument");
00109               return retval;
00110             }
00111         }
00112       else
00113         {
00114           error ("colloc: unexpected empty argument");
00115           return retval;
00116         }
00117     }
00118 
00119   ntot += left + right;
00120   if (ntot < 1)
00121     {
00122       error ("colloc: the total number of roots must be positive");
00123       return retval;
00124     }
00125 
00126   CollocWt wts (ncol, left, right);
00127 
00128   ColumnVector r = wts.roots ();
00129   Matrix A = wts.first ();
00130   Matrix B = wts.second ();
00131   ColumnVector q = wts.quad_weights ();
00132 
00133   retval(3) = q;
00134   retval(2) = B;
00135   retval(1) = A;
00136   retval(0) = r;
00137 
00138   return retval;
00139 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines