Programming |
Overloading Functions for the Polynom Class
MATLAB already has several functions for working with polynomials represented by coefficient vectors. They should be overloaded to also work with the new polynom object. In many cases, the overloading methods can simply apply the original function to the coefficient field.
Overloading roots for the Polynom Class
The method @polynom/roots.m
finds the roots of polynom objects.
function r = roots(p) % POLYNOM/ROOTS. ROOTS(p) is a vector containing the roots of p. r = roots(p.c);
Overloading polyval for the Polynom Class
The function polyval
evaluates a polynomial at a given set of points. @polynom/polyval.m
uses nested multiplication, or Horner's method to reduce the number of multiplication operations used to compute the various powers of x.
function y = polyval(p,x) % POLYNOM/POLYVAL POLYVAL(p,x) evaluates p at the points x. y = 0; for a = p.c y = y.*x + a; end
Overloading plot for the Polynom Class
The overloaded plot
function uses both root
and polyval
. The function selects the domain of the independent variable to be slightly larger than an interval containing all real roots. Then polyval
is used to evaluate the polynomial at a few hundred points in the domain.
function plot(p) % POLYNOM/PLOT PLOT(p) plots the polynom p. r = max(abs(roots(p))); x = (-1.1:0.01:1.1)*r; y = polyval(p,x); plot(x,y); title(char(p)) grid on
Overloading diff for the Polynom Class
The method @polynom/diff.m
differentiates a polynomial by reducing the degree by 1 and multiplying each coefficient by its original degree.
function q = diff(p) % POLYNOM/DIFF DIFF(p) is the derivative of the polynom p. c = p.c; d = length(c) - 1; % degree q = polynom(p.c(1:d).*(d:-1:1));
Overloading Arithmetic Operators for polynom | Listing Class Methods |
© 1994-2005 The MathWorks, Inc.