MATLAB Object-Oriented Programming
DocPolynom Class Properties Name Class Default Description coef double [] Vector of polynomial coefficients [highest order ... l ...
p2 = 2*x^4 + 3*x^2 + 2*x - 7 Find the roots of the polynomial by passing the coefficients to the roots function. roots(p1.coef) ...
Example Code Discussion function obj = DocPolynom(c) if nargin > 0 if isa(c,'DocPolynom') obj.coef = c.coef; else obj.coef = ...
Example Code Discussion function str = char(obj) if all(obj.coef == 0) s = '0'; str = s; return else d = length(obj.coef)-1; s = ...
Example Code Discussion d = d - 1; end end str = [s{:}]; end function disp(obj) c = char(obj); if iscell(c) disp([' ' c{:}]) els ...
Example Code Discussion function b = subsref(a,s) switch s(1).type case '()' ind = s.subs{:}; b = polyval(a.coef,ind); case '.' ...
Example Code Discussion function r = plus(obj1,obj2) obj1 = DocPolynom(obj1); obj2 = DocPolynom(obj2); k = length(obj2.coef) - l ...
if nargin > 0 if isa(c,'DocPolynom') obj.coef = c.coef; else obj.coef = c(:).'; end end end % DocPolynom function obj = set.c ...
a = -a; %#ok ind = ind + 1; end end if a ~= 1 || d == 0 if a == -1 s(ind) = {'-'}; ind = ind + 1; else s(ind) = {num2str(a)}; in ...
e = @(x)eval(p); y = zeros(length(x)); disp(['y = ',p]) for k = 1:length(x) y(k) = e(x(k)); disp([' ',num2str(y(k)),... ' = f(x ...
k = length(obj2.coef) - length(obj1.coef); zp = zeros(1,k); zm = zeros(1,-k); r = DocPolynom([zp,obj1.coef] + [zm,obj2.coef]); e ...
Input argument is a DocPolynom object — If you call the constructor function with an input argument that is already a DocPolyno ...
obj.coef = val(ind(1):end); else obj.coef = val; end end end Convert DocPolynom Objects to Other Types The DocPolynom class defi ...
class(c) ans = double The Character Converter The char method produces a char vector that represents the polynomial displayed as ...
if d > 0 s(ind) = {'*'}; ind = ind + 1; end end end if d >= 2 s(ind) = {['x^' int2str(d)]}; ind = ind + 1; elseif d == 1 s ...
creates a DocPolynom object. Because the statement is not terminated with a semicolon, the resulting output is displayed on the ...
Redefine Indexed Reference The DocPolynom class redefines indexed reference to support the use of objects representing polynomia ...
obj = p.method — Use dot notation to call methods without arguments and return a modified object. subsref Implementation Detai ...
case 'disp' disp(a) otherwise if length(s)>1 b = a.(s(1).subs)(s(2).subs{:}); else b = a.(s.subs); end end otherwise error('S ...
obj1 = DocPolynom(obj1); obj2 = DocPolynom(obj2); k = length(obj2.coef) - length(obj1.coef); zp = zeros(1,k); zm = zeros(1,-k); ...
«
34
35
36
37
38
39
40
41
42
43
»
Free download pdf