Library polynom implements the constructors and methods of two classes: polynom for polynomials, and ratfun for rational functions. Basic arithmetic operators and functions are overloaded to support expressions with the same syntax as for numbers and matrices.
The following statement makes available functions defined in polynom:
use polynom
Methods for conversion to MathML are defined in library polynom_mathml. Both libraries can be loaded with a single statement:
use polynom, polynom_mathml
Polynom object constructor.
use polynom p = polynom p = polynom(coef)
polynom(coef) creates a polynom object initialized with the coefficients in vector coef, given in descending powers of the variable. Without argument, polynom returns a polynom object initialized to 0.
The following operators and functions may be used with polynom arguments, with results analog to the corresponding functions of LME. Function roots ignores leading zero coefficients.
- | minus | + | plus |
^ | mpower | rem | |
\ | mldivide | roots | |
/ | mrdivide | - | uminus |
* | mtimes | + | uplus |
use polynom p = polynom([3,0,1,-4,2]) p = 3x^4+x^2-4x+2 q = 3 * p^2 + 8 q = 27x^8+18x^6-72x^5+39x^4-24x^3+60x^2-48x+20
polynom::disp, polynom::double, polynom::subst, polynom::diff, polynom::int, polynom::inline, polynom::feval, ratfun::ratfun
Display a polynom object.
use polynom disp(p)
disp(p) displays polynomial p. It is also executed implicitly when LME displays the polynom result of an expression which does not end with a semicolon.
use polynom p = polynom([3,0,1,-4,2]) p = 3x^4+x^2-4x+2
Convert a polynom object to a vector of coefficients.
use polynom coef = double(p)
double(p) converts polynomial p to a row vector of descending-power coefficients.
use polynom p = polynom([3,0,1,-4,2]); double(p) 3 0 1 -4 2
Substitute the variable of a polynom object with another polynomial.
use polynom subst(a, b)
subst(a,b) substitutes the variable of polynom a with polynom b.
use polynom p = polynom([1,2,3]) p = x^2+3x+9 q = polynom([2,0]) q = 2x r = subst(p, q) r = 4x^2+6x+9
polynom::polynom, polynom::feval
Polynom derivative.
use polynom diff(p)
diff(p) differentiates polynomial p.
use polynom p = polynom([3,0,1,-4,2]); q = diff(p) q = 12x^3+2x-4
polynom::polynom, polynom::int, polyder
Polynom integral.
use polynom int(p)
int(p) integrates polynomial p.
use polynom p = polynom([3,0,1,-4,2]); q = int(p) q = 0.6x^5+0.3333x^3-2x^2+2x
polynom::polynom, polynom::diff, polyint
Conversion from polynom object to inline function.
use polynom fun = inline(p)
inline(p) converts polynomial p to an inline function which can then be used with functions such as feval and ode45.
use polynom p = polynom([3,0,1,-4,2]); fun = inline(p) fun = <inline function> dumpvar('fun', fun); fun = inline('function y=f(x);y=polyval([3,0,1,-4,2],x);');
polynom::polynom, polynom::feval, ode45
Evaluate a polynom object.
use polynom y = feval(p, x)
feval(p,x) evaluates polynomial p for the value of x. If x is a vector or a matrix, the evaluation is performed separately on each element and the result has the same size as x.
use polynom p = polynom([3,0,1,-4,2]); y = feval(p, 1:5) y = 2 46 242 770 1882
polynom::polynom, polynom::inline, feval
Conversion to MathML.
use polynom, polynom_mathml str = mathml(p) str = mathml(p, false)
mathml(p) converts its argument p to MathML presentation, returned as a string.
By default, the MathML top-level element is <math>. If the result is to be used as a MathML subelement of a larger equation, a last input argument equal to the logical value false can be specified to suppress <math>.
use polynom, polynom_mathml p = polynom([3,0,1,-4,2]); m = mathml(p); math(0, 0, m);
Ratfun object constructor.
use polynom r = ratfun r = ratfun(coefnum) r = ratfun(coefnum, coefden)
ratfun(coefnum,coefden) creates a ratfun object initialized with the coefficients in vectors coefnum and coefden, given in descending powers of the variable. Without argument, ratfun returns a ratfun object initialized to 0. If omitted, coefden defaults to 1.
The following operators and functions may be used with ratfun arguments, with results analog to the corresponding functions of LME.
inv | * | mtimes | |
- | minus | + | plus |
\ | mldivide | - | uminus |
^ | mpower | + | uplus |
/ | mrdivide |
use polynom r = ratfun([3,0,1,-4,2], [2,5,0,1]) r = (3x^4+x^2-4x+2)/(2x^3+5x^2+1)
ratfun::disp, ratfun::inline, ratfun::feval, polynom::polynom
Display a ratfun object.
use polynom disp(r)
disp(r) displays rational function r. It is also executed implicitly when LME displays the ratfun result of an expression which does not end with a semicolon.
Get the numerator of a ratfun as a vector of coefficients.
use polynom coef = num(r)
num(r) gets the numerator of r as a row vector of descending-power coefficients.
Get the denominator of a ratfun as a vector of coefficients.
use polynom coef = den(a)
den(a) gets the denominator of a as a row vector of descending-power coefficients.
Ratfun derivative.
use polynom diff(r)
diff(r) differentiates ratfun r.
use polynom r = ratfun([1,3,0,1],[2,5]); q = diff(r) q = (4x^3+21x^2+30x-2)/(4x^2+20x+25)
Conversion from ratfun to inline function.
use polynom fun = inline(r)
inline(r) converts ratfun r to an inline function which can then be used with functions such as feval and ode45.
ratfun::ratfun, ratfun::feval, ode45
Evaluate a ratfun object.
use polynom y = feval(r, x)
feval(r,x) evaluates ratfun r for the value of x. If x is a vector or a matrix, the evaluation is performed separately on each element and the result has the same size as x.
use polynom r = ratfun([1,3,0,1],[2,5]); y = feval(r, 1:5) y = 0.7143 2.3333 5.0000 8.6923 13.4000
ratfun::ratfun, ratfun::inline, feval
Conversion to MathML.
use polynom, polynom_mathml str = mathml(r) str = mathml(r, false)
mathml(r) converts its argument r to MathML presentation, returned as a string.
By default, the MathML top-level element is <math>. If the result is to be used as a MathML subelement of a larger equation, a last input argument equal to the logical value false can be specified to suppress <math>.
use polynom, polynom_mathml r = ratfun([1,3,0,1],[2,5]); m = mathml(r); math(0, 0, m);