Introduction to Matlab

Mathematics University of Queensland Mathematics

2  Simple Calculations

2.1  Basic Arithmetic.

The simplest way to start with Matlab is to use it for simple calculations. Matlab has a wide range of functions and is able to use complex numbers as well as reals. The following simple commands have obvious meanings. Type them in and see what happens. (In the examples below, all the text after the  % is just a comment or an explanation. You do not have to type it in. It would just be ignored by Matlab.)

                 % The words after `%' are comments
                 % and explanations.  Do not type them in.
 
(-1+2+3)*5 - 2/3        % The four arithmetic operations
2^3                     % Means 2 to the power 3
exp( sin( pi/2 ) )        % The usual functions are provided
                          %  log, log10, cos, tan, asin, ...

Like a calculator, Matlab does all calculations correct only to about 16 significant decimal digits. This is enough accuracy for most purposes. For convenience, usually only the first 5 significant digits are displayed on the screen. Some more examples.

pi
22/7                  % pi is not the same as 22/7!
11*(15/11) - 15       % This shows there is roundoff error
                      %   when Matlab uses fractions.
cos( pi/3 )           % These are familiar trig functions.
sin( pi/6 )           %   Note Matlab always uses radians.

Matlab also uses variables to store intermediate answers. A variable can have almost any name, but it must begin with a letter. Matlab distinguishes between upper and lower case letters.

x = 2+3
y = 4+5
result1 = x/y

Sometimes the values of intermediate calculations are not needed and we do not want them to be displayed. If a semicolon (;) is placed after a command, the results are not displayed.

p = 2+3 ;                   % The semicolons supress
q = 3+5 ;                   %   unwanted output.
ratio = p/q

A number of commands can be placed on the one line, provided they are separated by a comma (,) or a semicolon (;). (Use a semicolon, unless you want the answer displayed.)

p = 2+3 ; q2 = x + 4 , ratio2 = p/q2       % All on one line

Parentheses can be used to make expressions clearer. Thus the last calculation could have been written as

ratio  =  (2+3)/(x+4)

Exercise 2

In this exercise, save retyping by using the up arrow to edit the previous commands! Remember that multiplication is done with a `*', it is not enough to put numbers next to each other.

  1. Use Matlab to evaluate log(s2-2 s cos(p/5)+1) where s = .5 .

  2. Now use Matlab to evaluate log(s2-2 s cos(p/5)+1) where s = .95 .

  3. Finally, evaluate log(s2-2 s sin(p/5)+1) when s = 1.

2.2  Complex Numbers.

Matlab handles complex numbers as easily as real numbers. When you start Matlab the variable i stands for Ö(-1).

x  =  2 + 3*i  ,  y  = 1 - 1*i
z1 = x - y ,  z2 = x * y ,  z3 = x / y
abs( x )
angle( y )
                       % Matlab quickly calculates the
sin(x)                 %    sin  of a complex number.

Matlab's facility with complex numbers is handy, as using complex numbers often involves complicated arithmetic. Indeed, as the previous example shows, Matlab will effortlessly work out functions of complex numbers that are difficult to do from first principles. But be careful about the name used for Ö(-1). When Matlab starts, the variable i contains Ö(-1). Often the user overrides this and uses i to stand for another number (in coding åi = 1n for example). In this case the Matlab command i = (-1)^.5 should be used to reset i to Ö(-1) before it is used to represent complex numbers.

i = 2 ; j = 3 ; i+j    % i is used in another calculation.
z = 2 + 3*i            % Complex arithmetic no langer works!.
i = (-1)^.5            % reset i
z = 2 + 3*i            % Complex numbers can now be used.

Here are two more complex calculations. Matlab can be used to demonstrate one of the most important relations in Mathematics: that epi = -1. (At least to within round off error!).

exp( pi*i ) + 1
i^i

Exercise 3

   

(a.)    The last command showed that according to Matlab, ii is a real number. Can you explain mathematically why this is this so? (Hint: Use i = epi/2.)

(b.)    If y = ii, what are iy and yi? Can you work this out without Matlab?