Mercurial > hg > octave-lyh
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 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2004, |
8920 | 2 ## 2005, 2006, 2007, 2008 John W. Eaton |
2313 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
2313 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
904 | 19 |
3368 | 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 | 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 | 31 ## filter, polygcd, polyval, polyvalm} |
3368 | 32 ## @end deftypefn |
1025 | 33 |
3202 | 34 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
2312 | 35 ## Created: June 1994 |
36 ## Adapted-By: jwe | |
561 | 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 | 39 |
5217 | 40 if (nargin == 1 || nargin == 2) |
41 if (! isvector (p)) | |
5216 | 42 error ("polyderiv: argument must be a vector"); |
43 endif | |
5217 | 44 if (nargin == 2) |
45 if (! isvector (a)) | |
10549 | 46 error ("polyderiv: argument must be a vector"); |
5217 | 47 endif |
48 if (nargout == 1) | |
10549 | 49 ## derivative of p*a returns a single polynomial |
50 q = polyderiv (conv (p, a)); | |
5217 | 51 else |
10549 | 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 | 54 if (numel (p) == 1) |
55 q = -p * polyderiv (a); | |
56 elseif (numel (a) == 1) | |
57 q = a * polyderiv (p); | |
58 else | |
59 q = conv (polyderiv (p), a) - conv (p, polyderiv (a)); | |
60 q = polyreduce (q); | |
61 endif | |
5217 | 62 |
10549 | 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 | 65 if (length(x) != 1) |
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 | 68 endif |
5217 | 69 |
10549 | 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 | 73 endif |
5216 | 74 else |
5217 | 75 lp = numel (p); |
76 if (lp == 1) | |
10549 | 77 q = 0; |
78 return; | |
5217 | 79 elseif (lp == 0) |
10549 | 80 q = []; |
81 return; | |
5216 | 82 endif |
561 | 83 |
5217 | 84 ## Force P to be a row vector. |
85 p = p(:).'; | |
5135 | 86 |
5217 | 87 q = p(1:(lp-1)) .* [(lp-1):-1:1]; |
5216 | 88 endif |
89 else | |
6046 | 90 print_usage (); |
5216 | 91 endif |
561 | 92 |
7411 | 93 endfunction |
5217 | 94 |
7411 | 95 %!assert(all (all (polyderiv ([1, 2, 3]) == [2, 2]))); |
96 | |
97 %!assert(polyderiv (13) == 0); | |
98 | |
99 %!error polyderiv ([]); | |
100 | |
101 %!error polyderiv ([1, 2; 3, 4]); | |
102 |