MATLAB Object-Oriented Programming

(Joyce) #1
creates a DocPolynom object. Because the statement is not terminated with a semicolon,
the resulting output is displayed on the command line:

p =
x^3 - 2*x - 5

Display Evaluated Expression


The char converter method forms a MATLAB expression for the polynomial represented
by a DocPolynom object. The dispPoly method evaluates the expression returned by
the char method with a specified value for x.

methods
function dispPoly(obj,x)
p = char(obj);
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 = ',...
num2str(x(k)),')'])
end
end
end

Create a DocPolynom object p:

p = DocPolynom([1 0 -2 -5])

p =

x^3 - 2*x - 5

Evaluate the polynomial at x equal to three values, [3 5 9]:

dispPoly(p,[3 5 9])

y = x^3 - 2*x - 5
16 = f(x = 3)
110 = f(x = 5)
706 = f(x = 9)

19 Defining Custom Data Types

Free download pdf