Programming |
The Polynom subsref Method
Suppose the design of the polynom class specifies that a subscripted reference to a polynom
object causes the polynomial to be evaluated with the value of the independent variable equal to the subscript. That is, for a polynom object p
,
the following subscripted expression returns the value of the polynomial at x = 3
and x = 4
.
subsref Implementation Details
This implementation takes advantage of the char
method already defined in the polynom class to produce an expression that can then be evaluated.
function b = subsref(a,s) % SUBSREF switch s.type case '()' ind = s.subs{:}; for k = 1:length(ind) b(k) = eval(strrep(char(a),'x',num2str(ind(k)))); end otherwise error('Specify value for x as p(x)') end
Once the polynomial expression has been generated by the char
method, the strrep
function is used to swap the passed in value for the character x
. The eval
function then evaluates the expression and returns the value in the output argument.
The Polynom display Method | Overloading Arithmetic Operators for polynom |
© 1994-2005 The MathWorks, Inc.