MATLAB Object-Oriented Programming

(Joyce) #1
obj1 = DocPolynom(obj1);
obj2 = DocPolynom(obj2);
k = length(obj2.coef) - length(obj1.coef);
zp = zeros(1,k);
zm = zeros(1,-k);
r = DocPolynom([zp,obj1.coef] + [zm,obj2.coef]);
end
end

Here is how the function works:


  • Ensure that both input arguments are DocPolynom objects so that expressions such
    as


p + 1

that involve both a DocPolynom and a double, work correctly.


  • Access the two coefficient vectors and, if necessary, pad one of them with zeros to
    make both the same length. The actual addition is simply the vector sum of the two
    coefficient vectors.

  • Call the DocPolynom constructor to create a properly typed object that is the result of
    adding the polynomials.


Define - Operator

Implement the minus operator (-) using the same approach as the plus (+) operator.

The minus method computes p - q. The dominant argument must be a DocPolynom
object.
methods
function r = minus(obj1,obj2)
obj1 = DocPolynom(obj1);
obj2 = DocPolynom(obj2);
k = length(obj2.coef) - length(obj1.coef);
zp = zeros(1,k);
zm = zeros(1,-k);
r = DocPolynom([zp,obj1.coef] - [zm,obj2.coef]);
end
end

Define the * Operator

Implement the mtimes method to compute the product p*q. The mtimes method
implements matrix multiplication since the multiplication of two polynomials is the
convolution (conv) of their coefficient vectors:

19 Defining Custom Data Types

Free download pdf