Page 75 - A Guide to MATLAB for Beginners and Experienced Users
P. 75

56        Chapter 4: Beyond the Basics


                     then the previous definition of h is substituted into the symbolic expression
                     h(t) before the integration is performed.


           Substitution
                     In Chapter 2 we described how to create an inline function from an expression.
                     You can then plug numbers into that function, to make a graph or table of
                     values for instance. But you can also substitute numerical values directly into
                     an expression with subs. For example,

                       >>symsaxy;
                       >> a = xˆ2 + yˆ2;
                       >> subs(a, x, 2)


                       ans =
                       4+y^2
                       >> subs(a, [x y], [3 4])

                       ans =
                             25

           More about M-Files


                     Files containing MATLAB statements are called M-files. There are two kinds
                     of M-files: function M-files, which accept arguments and produce output, and
                     script M-files, which execute a series of MATLAB statements. Earlier we cre-
                     ated and used bothtypes. In this section we present additional information
                     on M-files.

           Variables in Script M-Files

                     When you execute a script M-file, the variables you use and define belong
                     to your Workspace; that is, they take on any values you assigned earlier in
                     your MATLAB session, and they persist after the script finishes executing.
                     Consider the following script M-file, called scriptex1.m:

                       u=[1234];
                     Typing scriptex1 assigns the given vector to u but displays no output. Now
                     consider another script, called scriptex2.m:
                       n = length(u)
   70   71   72   73   74   75   76   77   78   79   80