MATLAB Programming
MATLAB Programming MATLAB Programming
9 Classes and Objects end if d >= 2 s = [s 'x^' int2str(d)]; elseif d == 1 s = [s 'x']; end end d = d - 1; end end Evaluating the Output If you create the polynom object p p = polynom([1 0 -2 -5]); and then call the char method on p char(p) MATLAB produces the result ans = x^3 - 2*x - 5 The value returned by char is a string that you can pass to eval once you have defined a scalar value for x. Forexample, x = 3; eval(char(p)) ans = 16 See “The Polynom subsref Method” on page 9-31 for a better method to evaluate the polynomial. The Polynom display Method Here is @polynom/display.m. This method relies on the char method to produce a string representation of the polynomial, which is then displayed 9-30
Example — A Polynomial Class on the screen. This method produces output that is the same as standard MATLABoutput.Thatis,thevariablenameisdisplayedfollowedbyanequal sign, then a blank line, then a new line with the value. function display(p) % POLYNOM/DISPLAY Command window display of a polynom disp(' '); disp([inputname(1),' = ']) disp(' '); disp([' ' char(p)]) disp(' '); The statement p = polynom([1 0 -2 -5]) creates a polynom object. Since the statement is not terminated with a semicolon, the resulting output is p = x^3 - 2*x - 5 The Polynom subsref Method Suppose the design of the polynom class specifies that a subscripted reference to a polynom objectcausesthepolynomialtobeevaluatedwiththevalueofthe independent variable equal to the subscript. That is, for a polynom object p, p = polynom([1 0 -2 -5]); the following subscripted expression returns the value of the polynomial at x = 3 and x = 4. p([3 4]) ans = 16 51 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) 9-31
- 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 686 and 687: 9 Classes and Objects Example — A
- Page 688 and 689: 9 Classes and Objects to the .c fie
- 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
- Page 736 and 737: 9 Classes and Objects 9-76
- Page 738 and 739: 10 Scheduling Program Execution wit
Example — A Polynomial Class<br />
on the screen. This method produces output that is the same as standard<br />
<strong>MATLAB</strong>output.Thatis,thevariablenameisdisplayedfollowedbyanequal<br />
sign, then a blank line, then a new line with the value.<br />
function display(p)<br />
% POLYNOM/DISPLAY Command window display of a polynom<br />
disp(' ');<br />
disp([inputname(1),' = '])<br />
disp(' ');<br />
disp([' ' char(p)])<br />
disp(' ');<br />
The statement<br />
p = polynom([1 0 -2 -5])<br />
creates a polynom object. Since the statement is not terminated with a<br />
semicolon, the resulting output is<br />
p =<br />
x^3 - 2*x - 5<br />
The Polynom subsref Method<br />
Suppose the design of the polynom class specifies that a subscripted reference<br />
to a polynom objectcausesthepolynomialtobeevaluatedwiththevalueofthe<br />
independent variable equal to the subscript. That is, for a polynom object p,<br />
p = polynom([1 0 -2 -5]);<br />
the following subscripted expression returns the value of the polynomial at x<br />
= 3 and x = 4.<br />
p([3 4])<br />
ans =<br />
16 51<br />
subsref Implementation Details<br />
This implementation takes advantage of the char method already defined in<br />
the polynom class to produce an expression that can then be evaluated.<br />
function b = subsref(a,s)<br />
9-31