annotate scripts/polynomial/polyderiv.m @ 11469:c776f063fefe

Overhaul m-script files to use common variable name between code and documentation.
author Rik <octave@nomad.inbox5.com>
date Sun, 09 Jan 2011 12:41:21 -0800
parents a8ce6bdecce5
children fd0a3ac60b0e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2004,
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 7411
diff changeset
2 ## 2005, 2006, 2007, 2008 John W. Eaton
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
3 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
4 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
5 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6850
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6850
diff changeset
9 ## your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
10 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
14 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
15 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6850
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6850
diff changeset
18 ## <http://www.gnu.org/licenses/>.
904
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 561
diff changeset
19
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3202
diff changeset
20 ## -*- texinfo -*-
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
21 ## @deftypefn {Function File} {} polyderiv (@var{p})
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
22 ## @deftypefnx {Function File} {[@var{k}] =} polyderiv (@var{a}, @var{b})
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
23 ## @deftypefnx {Function File} {[@var{q}, @var{d}] =} polyderiv (@var{b}, @var{a})
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3202
diff changeset
24 ## Return the coefficients of the derivative of the polynomial whose
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
25 ## coefficients are given by the vector @var{p}. If a pair of polynomials
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
26 ## is given, return the derivative of the product @math{@var{a}*@var{b}}.
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
27 ## If two inputs and two outputs are given, return the derivative of the
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
28 ## polynomial quotient @math{@var{b}/@var{a}}. The quotient numerator is
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
29 ## in @var{q} and the denominator in @var{d}.
10224
f6e0404421f4 point to polyint in @seealso, not polyinteg
John W. Eaton <jwe@octave.org>
parents: 8920
diff changeset
30 ## @seealso{poly, polyint, polyreduce, roots, conv, deconv, residue,
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5307
diff changeset
31 ## filter, polygcd, polyval, polyvalm}
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3202
diff changeset
32 ## @end deftypefn
1025
f558749713f1 [project @ 1995-01-11 20:52:10 by jwe]
jwe
parents: 904
diff changeset
33
3202
44d82b369c78 [project @ 1998-10-29 20:27:57 by jwe]
jwe
parents: 3085
diff changeset
34 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
2312
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
35 ## Created: June 1994
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
36 ## Adapted-By: jwe
561
e79ff1f4df3c [project @ 1994-07-25 22:32:08 by jwe]
jwe
parents:
diff changeset
37
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
38 function [q, d] = polyderiv (p, a)
5216
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents: 5135
diff changeset
39
5217
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
40 if (nargin == 1 || nargin == 2)
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
41 if (! isvector (p))
5216
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents: 5135
diff changeset
42 error ("polyderiv: argument must be a vector");
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents: 5135
diff changeset
43 endif
5217
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
44 if (nargin == 2)
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
45 if (! isvector (a))
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
46 error ("polyderiv: argument must be a vector");
5217
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
47 endif
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
48 if (nargout == 1)
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
49 ## derivative of p*a returns a single polynomial
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
50 q = polyderiv (conv (p, a));
5217
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
51 else
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
52 ## derivative of p/a returns numerator and denominator
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
53 d = conv (a, a);
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
54 if (numel (p) == 1)
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
55 q = -p * polyderiv (a);
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
56 elseif (numel (a) == 1)
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
57 q = a * polyderiv (p);
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
58 else
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
59 q = conv (polyderiv (p), a) - conv (p, polyderiv (a));
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
60 q = polyreduce (q);
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
61 endif
5217
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
62
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
63 ## remove common factors from numerator and denominator
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
64 x = polygcd (q, d);
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
65 if (length(x) != 1)
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
66 q = deconv (q, x);
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
67 d = deconv (d, x);
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
68 endif
5217
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
69
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
70 ## move all the gain into the numerator
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
71 q = q/d(1);
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
72 d = d/d(1);
5217
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
73 endif
5216
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents: 5135
diff changeset
74 else
5217
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
75 lp = numel (p);
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
76 if (lp == 1)
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
77 q = 0;
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
78 return;
5217
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
79 elseif (lp == 0)
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
80 q = [];
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10224
diff changeset
81 return;
5216
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents: 5135
diff changeset
82 endif
561
e79ff1f4df3c [project @ 1994-07-25 22:32:08 by jwe]
jwe
parents:
diff changeset
83
5217
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
84 ## Force P to be a row vector.
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
85 p = p(:).';
5135
8eaef366ab43 [project @ 2005-02-09 07:59:37 by jwe]
jwe
parents: 5053
diff changeset
86
5217
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
87 q = p(1:(lp-1)) .* [(lp-1):-1:1];
5216
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents: 5135
diff changeset
88 endif
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents: 5135
diff changeset
89 else
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
90 print_usage ();
5216
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents: 5135
diff changeset
91 endif
561
e79ff1f4df3c [project @ 1994-07-25 22:32:08 by jwe]
jwe
parents:
diff changeset
92
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
93 endfunction
5217
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
94
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
95 %!assert(all (all (polyderiv ([1, 2, 3]) == [2, 2])));
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
96
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
97 %!assert(polyderiv (13) == 0);
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
98
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
99 %!error polyderiv ([]);
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
100
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
101 %!error polyderiv ([1, 2; 3, 4]);
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
102