MATLAB Programming
MATLAB Programming MATLAB Programming
9 Classes and Objects Example — A Polynomial Class This example implements a MATLAB data type for polynomials by defining a new class called polynom. The class definition specifies a structure for data storage and defines a directory (@polynom) of methods that operate on polynom objects. This section covers • “Polynom Data Structure” on page 9-26 • “Polynom Methods” on page 9-26 • “The Polynom Constructor Method” on page 9-27 • “Converter Methods for the Polynom Class” on page 9-28 • “The Polynom display Method” on page 9-30 • “The Polynom subsref Method” on page 9-31 • “Overloading Arithmetic Operators for polynom” on page 9-32 • “Overloading Functions for the Polynom Class” on page 9-34 • “Listing Class Methods” on page 9-36 Polynom Data Structure The polynom class represents a polynomial with a row vector containing the coefficients of powers of the variable, in decreasing order. Therefore, a polynom object p is a structure with a single field, p.c, containingthe coefficients. This field is accessible only within the methods in the @polynom directory. Polynom Methods To create a class that is well behaved within the MATLAB environment and provides useful functionality for a polynomial data type, the polynom class implements the following methods: • A constructor method polynom.m • A polynom to double converter 9-26
Example — A Polynomial Class • A polynom to char converter • A display method • A subsref method • Overloaded +, -,and* operators • Overloaded roots, polyval, plot, anddiff functions The Polynom Constructor Method Here is the polynom class constructor, @polynom/polynom.m. function p = polynom(a) %POLYNOM Polynomial class constructor. % p = POLYNOM(v) creates a polynomial object from the vector v, % containing the coefficients of descending powers of x. if nargin == 0 p.c = []; p = class(p,'polynom'); elseif isa(a,'polynom') p = a; else p.c = a(:).'; p = class(p,'polynom'); end Constructor Calling Syntax You can call the polynom constructor method with one of three different arguments: • No input argument — If you call the constructor function with no arguments, it returns a polynom object with empty fields. • Input argument is an object — If you call the constructor function with an input argument that is already a polynom object, MATLAB returns the input argument. The isa function (pronounced “is a”) checks for this situation. • Input argument is a coefficient vector — If the input argument is a variable thatisnotapolynomobject,reshapeittobearowvectorandassignit 9-27
- Page 636 and 637: 8 Error Handling Checking for Error
- Page 638 and 639: 8 Error Handling Handling and Recov
- Page 640 and 641: 8 Error Handling X = A * B catch di
- Page 642 and 643: 8 Error Handling matrixMultiply(A,
- Page 644 and 645: 8 Error Handling Message Identifier
- Page 646 and 647: 8 Error Handling error('msg_id', 'e
- Page 648 and 649: 8 Error Handling Warnings Like erro
- Page 650 and 651: 8 Error Handling Warning Control Th
- Page 652 and 653: 8 Error Handling warnings by issuin
- Page 654 and 655: 8 Error Handling You must type the
- Page 656 and 657: 8 Error Handling s(2) ans = identif
- Page 658 and 659: 8 Error Handling end function f2(x)
- Page 660 and 661: 8 Error Handling Debugging Errors a
- Page 662 and 663: 9 Classes and Objects Classes and O
- Page 664 and 665: 9 Classes and Objects The diagram s
- Page 666 and 667: 9 Classes and Objects functions do
- Page 668 and 669: 9 Classes and Objects visible only
- Page 670 and 671: 9 Classes and Objects Class Method
- Page 672 and 673: 9 Classes and Objects Examples of C
- Page 674 and 675: 9 Classes and Objects not support p
- Page 676 and 677: 9 Classes and Objects overloaded me
- Page 678 and 679: 9 Classes and Objects A(1,2).name(3
- Page 680 and 681: 9 Classes and Objects Object Indexi
- Page 682 and 683: 9 Classes and Objects Converter Met
- Page 684 and 685: 9 Classes and Objects Operation M-F
- Page 688 and 689: 9 Classes and Objects to the .c fie
- Page 690 and 691: 9 Classes and Objects end if d >= 2
- Page 692 and 693: 9 Classes and Objects % SUBSREF swi
- Page 694 and 695: 9 Classes and Objects q = polynom(q
- Page 696 and 697: 9 Classes and Objects % POLYNOM/DIF
- Page 698 and 699: 9 Classes and Objects Building on O
- Page 700 and 701: 9 Classes and Objects Multiple Inhe
- Page 702 and 703: 9 Classes and Objects Inheritance M
- Page 704 and 705: 9 Classes and Objects a.currentValu
- Page 706 and 707: 9 Classes and Objects error('Asset
- Page 708 and 709: 9 Classes and Objects case 'date' a
- Page 710 and 711: 9 Classes and Objects Note that the
- Page 712 and 713: 9 Classes and Objects For example,
- Page 714 and 715: 9 Classes and Objects The Stock sub
- Page 716 and 717: 9 Classes and Objects case '.' swit
- Page 718 and 719: 9 Classes and Objects Example — T
- Page 720 and 721: 9 Classes and Objects if isa(name,'
- Page 722 and 723: 9 Classes and Objects end if saving
- Page 724 and 725: 9 Classes and Objects Saving and Lo
- Page 726 and 727: 9 Classes and Objects • subsref
- Page 728 and 729: 9 Classes and Objects function n =
- Page 730 and 731: 9 Classes and Objects Object Preced
- Page 732 and 733: 9 Classes and Objects How MATLAB De
- Page 734 and 735: 9 Classes and Objects Private funct
Example — A Polynomial Class<br />
• A polynom to char converter<br />
• A display method<br />
• A subsref method<br />
• Overloaded +, -,and* operators<br />
• Overloaded roots, polyval, plot, anddiff functions<br />
The Polynom Constructor Method<br />
Here is the polynom class constructor, @polynom/polynom.m.<br />
function p = polynom(a)<br />
%POLYNOM Polynomial class constructor.<br />
% p = POLYNOM(v) creates a polynomial object from the vector v,<br />
% containing the coefficients of descending powers of x.<br />
if nargin == 0<br />
p.c = [];<br />
p = class(p,'polynom');<br />
elseif isa(a,'polynom')<br />
p = a;<br />
else<br />
p.c = a(:).';<br />
p = class(p,'polynom');<br />
end<br />
Constructor Calling Syntax<br />
You can call the polynom constructor method with one of three different<br />
arguments:<br />
• No input argument — If you call the constructor function with no<br />
arguments, it returns a polynom object with empty fields.<br />
• Input argument is an object — If you call the constructor function with<br />
an input argument that is already a polynom object, <strong>MATLAB</strong> returns<br />
the input argument. The isa function (pronounced “is a”) checks for this<br />
situation.<br />
• Input argument is a coefficient vector — If the input argument is a variable<br />
thatisnotapolynomobject,reshapeittobearowvectorandassignit<br />
9-27