Mercurial > hg > octave-lyh
annotate scripts/polynomial/polyder.m @ 17199:85e55da61409
doc: Clarify description of plot format.
* scripts/plot/plot.m: Clarify description of plot format.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 06 Aug 2013 21:23:38 -0700 |
parents | 5d3a684236b0 |
children |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14104
diff
changeset
|
1 ## Copyright (C) 1994-2012 John W. Eaton |
2313 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
1597 | 18 |
3458 | 19 ## -*- texinfo -*- |
12803
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
20 ## @deftypefn {Function File} {} polyder (@var{p}) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
21 ## @deftypefnx {Function File} {[@var{k}] =} polyder (@var{a}, @var{b}) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
22 ## @deftypefnx {Function File} {[@var{q}, @var{d}] =} polyder (@var{b}, @var{a}) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
23 ## Return the coefficients of the derivative of the polynomial whose |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
24 ## coefficients are given by the vector @var{p}. If a pair of polynomials |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
25 ## is given, return the derivative of the product @math{@var{a}*@var{b}}. |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
26 ## If two inputs and two outputs are given, return the derivative of the |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
27 ## polynomial quotient @math{@var{b}/@var{a}}. The quotient numerator is |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
28 ## in @var{q} and the denominator in @var{d}. |
14104
614505385171
doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents:
12803
diff
changeset
|
29 ## @seealso{polyint, polyval, polyreduce} |
3458 | 30 ## @end deftypefn |
1597 | 31 |
12803
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
32 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
33 ## Created: June 1994 |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
34 ## Adapted-By: jwe |
2314 | 35 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
36 function [q, d] = polyder (p, a) |
1597 | 37 |
12803
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
38 if (nargin == 1 || nargin == 2) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
39 if (! isvector (p)) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
40 error ("polyder: argument must be a vector"); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
41 endif |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
42 if (nargin == 2) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
43 if (! isvector (a)) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
44 error ("polyder: argument must be a vector"); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
45 endif |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
46 if (nargout == 1) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
47 ## derivative of p*a returns a single polynomial |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
48 q = polyder (conv (p, a)); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
49 else |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
50 ## derivative of p/a returns numerator and denominator |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
51 d = conv (a, a); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
52 if (numel (p) == 1) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
53 q = -p * polyder (a); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
54 elseif (numel (a) == 1) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
55 q = a * polyder (p); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
56 else |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
57 q = conv (polyder (p), a) - conv (p, polyder (a)); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
58 q = polyreduce (q); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
59 endif |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
60 |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
61 ## remove common factors from numerator and denominator |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
62 x = polygcd (q, d); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
63 if (length (x) != 1) |
12803
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
64 q = deconv (q, x); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
65 d = deconv (d, x); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
66 endif |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
67 |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
68 ## move all the gain into the numerator |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
69 q = q/d(1); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
70 d = d/d(1); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
71 endif |
5216 | 72 else |
12803
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
73 lp = numel (p); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
74 if (lp == 1) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
75 q = 0; |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
76 return; |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
77 elseif (lp == 0) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
78 q = []; |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
79 return; |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
80 endif |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
81 |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
82 ## Force P to be a row vector. |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
83 p = p(:).'; |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
84 |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
85 q = p(1:(lp-1)) .* [(lp-1):-1:1]; |
5216 | 86 endif |
1597 | 87 else |
6046 | 88 print_usage (); |
1597 | 89 endif |
90 | |
91 endfunction | |
12803
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
92 |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
93 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
94 %!assert (polyder ([1, 2, 3], [2, 2])) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
95 %!assert (polyder (13), 0) |
12803
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
96 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
97 %!error polyder ([]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
98 %!error polyder (1,2,3) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
99 %!error <argument must be a vector> polyder ([1, 2; 3, 4]) |
12803
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
100 |