Page 25 - Applied Numerical Methods Using MATLAB
P. 25

14    MATLAB USAGE AND COMPUTATIONAL ERRORS
              (A5) It is just a set of function values [f1(x1) f1(x2) .. .] obtained at a time for several
                  values [x1 x2.. .] of x. In expectation of one-shot multi-operation, it is a good
                  practice to put a dot(.) just before the arithmetic operators *(multiplication),
                  /(division), and ^ (power) in the function definition so that the term-by-term
                  (termwise) operation can be done any time.
              Note that we can define a simple function not only in an independent M-file,
           but also inside a program by using the inline() command or just in a form of
           literal expression that can be evaluated by the command eval().

            >>f1 = inline(’1./(1+8*x.^2)’,’x’);
            >>f1([0 1]), feval(f1,[0 1])
              ans = 1.0000   0.1111
              ans = 1.0000   0.1111
            >>f1 = ’1./(1+8*x.^2)’; x = [0 1]; eval(f1)
              ans = 1.0000   0.1111

           As far as a polynomial function is concerned, it can simply be defined as its
           coefficient vector arranged in descending order. It may be called to yield its
           value for certain value(s) of its independent variable by using the command
           polyval().

                                                        3
            >>p = [1 0 -3 2]; %polynomial function  p(x) = x − 3x + 2
            >>polyval(p,[0 1])
              ans =  2.0000   0.0000
              The multiplication of two polynomials can be performed by taking the con-
           volution of their coefficient vectors representing the polynomials in MATLAB,
           since

                                      N
                N
            (a N x +· · · + a 1 x + a 0 )(b N x +· · · + b 1 x + b 0 ) = c 2N x 2N  +· · · + c 1 x + c 0
           where
                           min(k,N)

                    c k =          a k−m b m  for k = 2N, 2N − 1,..., 1, 0
                        m=max(0,k−N)

           This operation can be done by using the MATLAB built-in command conv() as
           illustrated below.

           >>a = [1 -1]; b=[1 1 1]; c = conv(a,b)
              c = 1  0  0  -1 %meaning that (x − 1)(x  2  + x + 1) = x  3  + 0 · x 2  + 0 · x − 1
                                                               n
           But, in case you want to multiply a polynomial by only x , you can simply
           append n zeros to the right end of the polynomial coefficient vector to extend
           its dimension.
           >>a=[123];c=[a00] %equivalently, c = conv(a,[1 0 0])
              c = 1  2  3  0  0 %meaning that (x  2  + 2x + 3)x  2  = x 4  + 2x 3  + 3x  2  + 0 · x + 0
   20   21   22   23   24   25   26   27   28   29   30