MATLAB: Mathematical functions and formatted output

 

As you read this material we strongly recommend that you activate your MATLAB window and try the commands right there and then

 

 

 We saw earlier that computer languages such as C++ , Visual Basic, Fortran etc. have a rich library of mathematical functions which make scientific programming very easy and pleasant.  The same is true for spreadsheets like Excel.  Of course, MATLAB has a good collection of mathematical functions too. 

You can see a list of the elementary math functions by typing help elfun at the command window ( in fact when you type help …topic.. , you get information about the topic ).  If you did that, you will see that there are the :

 

trigonometric functions

    sin         - Sine.

    sinh        - Hyperbolic sine.

    asin        - Inverse sine.

    asinh       - Inverse hyperbolic sine.

    cos         - Cosine.

    cosh        - Hyperbolic cosine.

    acos        - Inverse cosine.

    acosh       - Inverse hyperbolic cosine.

    tan         - Tangent.

    tanh        - Hyperbolic tangent.

    atan        - Inverse tangent.

    atan2       - Four quadrant inverse tangent.

    atanh       - Inverse hyperbolic tangent.

    sec         - Secant.

    sech        - Hyperbolic secant.

    asec        - Inverse secant.

    asech       - Inverse hyperbolic secant.

    csc         - Cosecant.

    csch        - Hyperbolic cosecant.

    acsc        - Inverse cosecant.

    acsch       - Inverse hyperbolic cosecant.

    cot         - Cotangent.

    coth        - Hyperbolic cotangent.

    acot        - Inverse cotangent.

    acoth       - Inverse hyperbolic cotangent.

  

Exponential type functions

    exp         - Exponential.

    log         - Natural logarithm.

    log10       - Common (base 10) logarithm.

    log2        - Base 2 logarithm and dissect floating point number.

    pow2        - Base 2 power and scale floating point number.

    sqrt        - Square root.

    nextpow2    - Next higher power of 2.

 

Rounding and remainder functions

    fix         - Round towards zero.

    floor       - Round towards minus infinity.

    ceil        - Round towards plus infinity.

    round       - Round towards nearest integer.

    mod         - Modulus (signed remainder after division).

    rem         - Remainder after division.

    sign        - Signum.  

 

Let us for example try to find some more about the function exp, we can type  >> help exp and we obtain:

 

 

 

 

Now suppose that we want to create a table of sin(x) and cos(x) as x varies from zero radians to p/2 radians.  First of all, is there a build in constant pi in MATLAB?  Absolutely!  See what the system has to say about  p .

 

 

I would also include PI = acos (-1.0) to the above list.  See what you get:

 

 

Notice the effect of format long.  Now let us proceed with the construction of the sin, cos table we mentioned earlier.  The following picture shows the necessary steps.  Note that you can insert comments to the statements by using the % sign.  Anything after % is treated as a comment.

 

 

There are some things missing from our table.  We have no heading for one, and also we seem to be without control of the output format. We will now see how we can write our output in our own format.

 

 

Formatting considerations:

 

In order to control the output of our results we use the function fprintf ( ‘  format & label string ‘, variable list );

As an example we can have:

 

fprintf ( ‘ \n  x = %5.2f   sin(x) = %10.3e   cos(x) = %10.3e  \n\n ‘ , x,  b,  c );

 

This will print out the labels x, sin(x), and cos(x) and next to each will insert the values of x, b and c.  The angle x will be displayed as float with two decimal digits, while b and c will be displayed in scientific notation with three decimal digits.  The width for x will be 5 but for b and c will be 10 places.  Let’ s see the example below and notice also what happens if we don’t use the width and precision specifiers :

 

 

Needless to say the above function can be used to create headings to tables.  For example I can accomplish this by writing:

 

fprintf ( ‘ \n\n        My table      \n   x …  cos(x) …   sin(x)  \n\n ‘ );

 

We can see the result in the following picture:

 

 

What happens if fprintf is used to print arrays?  The format string is cycled in column order through the elements of the array until all elements have been displayed.  For example let us print the sin and cosine table labels.  Here is what we do:

 

 

Note that the interactive character of the command window of MATLAB does not allow the introduction of a heading as a separate step from the printing of the actual table values. We will however see how to run MATLAB programs ( m-file scripts) where there is no such difficulty.