GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
colloc.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <algorithm>
31 #include <string>
32 
33 #include "CollocWt.h"
34 #include "lo-mappers.h"
35 
36 #include "defun.h"
37 #include "error.h"
38 #include "ovl.h"
39 #include "utils.h"
40 
41 DEFUN (colloc, args, ,
42  doc: /* -*- texinfo -*-
43 @deftypefn {} {[@var{r}, @var{amat}, @var{bmat}, @var{q}] =} colloc (@var{n}, "left", "right")
44 Compute derivative and integral weight matrices for orthogonal collocation.
45 
46 Reference: @nospell{J. Villadsen}, @nospell{M. L. Michelsen},
47 @cite{Solution of Differential Equation Models by Polynomial Approximation}.
48 @end deftypefn */)
49 {
50  int nargin = args.length ();
51 
52  if (nargin < 1 || nargin > 3)
53  print_usage ();
54 
55  if (! args(0).is_scalar_type ())
56  error ("colloc: N must be a scalar");
57 
58  double tmp = args(0).double_value ();
59  if (octave::math::isnan (tmp))
60  error ("colloc: N cannot be NaN");
61 
63  if (ncol < 0)
64  error ("colloc: N must be positive");
65 
66  octave_idx_type ntot = ncol;
68  octave_idx_type right = 0;
69 
70  for (int i = 1; i < nargin; i++)
71  {
72  std::string s = args(i).xstring_value ("colloc: optional arguments must be strings");
73 
74  std::transform (s.begin (), s.end (), s.begin (), ::tolower);
75 
76  if (s == "r" || s == "right")
77  right = 1;
78  else if (s == "l" || s == "left")
79  left = 1;
80  else
81  error (R"(colloc: string argument must be "left" or "right")");
82  }
83 
84  ntot += left + right;
85  if (ntot < 1)
86  error (R"("colloc: the total number of roots (N + "left" + "right") must be >= 1)");
87 
88  CollocWt wts (ncol, left, right);
89 
90  ColumnVector r = wts.roots ();
91  Matrix A = wts.first ();
92  Matrix B = wts.second ();
93  ColumnVector q = wts.quad_weights ();
94 
95  return ovl (r, A, B, q);
96 }
97 
98 /*
99 
100 %!assert (colloc (1), 0.5)
101 %!assert (colloc (1, "left"), [0; 0.5])
102 %!assert (colloc (1, "right"), [0.5; 1])
103 %!assert (colloc (1, "left", "right"), [0; 0.5; 1])
104 
105 ## Test input validation
106 %!error colloc ()
107 %!error colloc (1,2,3,4)
108 %!error <N must be a scalar> colloc (ones (2,2))
109 %!error <N cannot be NaN> colloc (NaN)
110 %!error <N must be positive> colloc (-1)
111 %!error <optional arguments must be strings> colloc (1, 1)
112 %!error <string argument must be "left" or "right"> colloc (1, "foobar")
113 %!error <total number of roots .* must be .= 1> colloc (0)
114 
115 */
ColumnVector quad_weights(void)
Definition: CollocWt.h:161
Matrix first(void)
Definition: CollocWt.h:163
Matrix second(void)
Definition: CollocWt.h:165
ColumnVector roots(void)
Definition: CollocWt.h:158
Definition: dMatrix.h:42
OCTINTERP_API void print_usage(void)
Definition: defun.cc:53
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
void error(const char *fmt,...)
Definition: error.cc:968
ColumnVector transform(const Matrix &m, double x, double y, double z)
Definition: graphics.cc:5814
F77_RET_T const F77_INT F77_CMPLX const F77_INT F77_CMPLX * B
F77_RET_T const F77_INT F77_CMPLX * A
T * r
Definition: mx-inlines.cc:773
bool isnan(bool)
Definition: lo-mappers.h:178
octave_idx_type nint_big(double x)
Definition: lo-mappers.cc:184
static int left
Definition: randmtzig.cc:191
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:211