23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

9 Classes and Objects<br />

end<br />

if d >= 2<br />

s = [s 'x^' int2str(d)];<br />

elseif d == 1<br />

s = [s 'x'];<br />

end<br />

end<br />

d = d - 1;<br />

end<br />

end<br />

Evaluating the Output<br />

If you create the polynom object p<br />

p = polynom([1 0 -2 -5]);<br />

and then call the char method on p<br />

char(p)<br />

<strong>MATLAB</strong> produces the result<br />

ans =<br />

x^3 - 2*x - 5<br />

The value returned by char is a string that you can pass to eval once you<br />

have defined a scalar value for x. Forexample,<br />

x = 3;<br />

eval(char(p))<br />

ans =<br />

16<br />

See “The Polynom subsref Method” on page 9-31 for a better method to<br />

evaluate the polynomial.<br />

The Polynom display Method<br />

Here is @polynom/display.m. This method relies on the char method to<br />

produce a string representation of the polynomial, which is then displayed<br />

9-30

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!