Page 38 - Computational Colour Science Using MATLAB
P. 38

USING FUNCTIONS IN MATLAB                       25
             could be predicted in advance because the system is well conditioned. The
             condition number of a matrix is given by the MATLAB command cond, thus:


                  M = [1 -1; 1 1; 6 1];
                  cond(M)
                  ans =
                        4.4159


             The condition number of a matrix M is defined as the product of the norm of M
             and the norm of the inverse matrix M  1  (Borse, 1997). When the condition
             number of a matrix is high it is especially important to use the backslash
             operator rather than pinv.





             3.3   M-files

             A powerful property of MATLAB is that it offers the user the ability to write
             scripts, known as M-files. Any simple text editor such as Notepad can be used to
             write an M-file, but in later versions of MATLAB an Integrated Development
             Environment (IDE) is provided with a special MATLAB editor. Commands can
             be entered as a script in the same way that they would be entered into the
             Command Window. If the script is saved with the .m extension, then the
             commands can be executed by simply typing the name of the script. For example,
             an M-file called test.m can be executed by typing test in the Command
             Window. For some scripts it can be useful to place the command clear as the first
             line in the script so that MATLAB script is started from a clean environment. Of
             course, it is important to be careful to avoid using names for M-files that clash
             with any of MATLAB’s built-in functions or M-file functions. Comments may
             be placed in M-files by starting the line with the % symbol.




             3.4   Using functions in MATLAB

             Although it is possible to create quite complicated programs using combinations
             of M-files (since one M-file can call another) most users will at some stage wish
             to create their own functions. This can also be achieved using M-files. In fact,
             many of the toolbox functions in MATLAB that perform some action on an
             arbitrary input are in fact scripts stored as M-files. In order to see how a script
             can be used to generate a function the following example illustrates a function
             called treble that takes a single variable as input and produces three times that
             variable as the output:
   33   34   35   36   37   38   39   40   41   42   43