23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Example — A Polynomial Class<br />

Having implemented the double method, you can use it to call <strong>MATLAB</strong><br />

functions on polynom objects that require double values as inputs. For<br />

example,<br />

size(double(p))<br />

ans =<br />

1 4<br />

The Polynom to Char Converter<br />

The converter to char is a key method because it produces a character string<br />

involving the powers of an independent variable, x. Therefore, once you have<br />

specified x, the string returned is a syntactically correct <strong>MATLAB</strong> expression,<br />

whichyoucanthenevaluate.<br />

Here is @polynom/char.m.<br />

function s = char(p)<br />

% POLYNOM/CHAR<br />

% CHAR(p) is the string representation of p.c<br />

if all(p.c == 0)<br />

s = '0';<br />

else<br />

d = length(p.c) - 1;<br />

s = [];<br />

for a = p.c;<br />

if a ~= 0;<br />

if ~isempty(s)<br />

if a > 0<br />

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

else<br />

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

a = -a;<br />

end<br />

end<br />

if a ~= 1 | d == 0<br />

s = [s num2str(a)];<br />

if d > 0<br />

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

end<br />

9-29

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

Saved successfully!

Ooh no, something went wrong!