Josh Thompson
Postdoc: Colorado State University
Home  >  Teaching

Basic Arithmetics and Array Operations

Contents

1. Basic Arithmetics

2. Simple Arrays


1. Basic Arithmetics

1.1 Algebraic Operations. Running MATLAB creates the Command Window on your computer screen. The Command Window displays a prompt, >>, and a cursor waiting for your action. You can now type in a command and execute it by pressing the “Enter” key:

» 7.9*(17.75-4/5)*1.2^0.3+0.1
 

ans =

  141.5331

»

If you don’t want to see the result, end the command with a semicolon:

» 7.9*(17.75-4/5)*1.2^0.3+0.1;
»

You still have access to the result by typing

» ans

ans =

  141.5331

but after the next computation,

» 8*7.2+0.4;
» ans

ans =

    58

ans is reserved to to the new result. If you want to save the result of the first computation, type

» a=7.9*(17.75-4/5)*1.2^0.3+0.1;

Here you have introduced a variable, a, and assigned to it the value of the consecutive computation. You can use an assigned variable in any subsequent computation, assign another value to the variable, or clear it:

» b=a*0.1-0.4;a,b

a =

  141.5331
 

b =

   13.7533

>> a=5

a =

     5

>> clear a;a
??? Reference to a cleared variable a.

Observe that in the first command line three consecutive commands were executed. Ending a particular command in such a sequence with a comma/semicolon does/does not cause MATLAB to display the answer. If a command – or a sequence of commands – is to be continued over more than one line, type three dots and press the “enter” key before switching to the next line:

» 1.5*0.3;b,...
b*0.9+...
8,

b =

  13.7533
 

ans =

   20.3780

The symbols for the operations multiplication, addition, exponentiation, subtraction should be familiar to you as well as the orders in which the are carried out. However,  MATLAB has two different (inverse) types of divisions, namely left and right division:

» 4/5,4\5

ans =

    0.8000
 

ans =

    1.2500

The arithmetic operations can be directly extended to complex numbers. A nice feature of MATLAB is that it does not require any special treatment of complex numbers as shown by the following examples:

» c1=1-2i %the appended i signifies the imaginary part

c1 =

   1.0000 - 2.0000i

» c1=1-2j %j also works

c1 =

   1.0000 - 2.0000i

» c2=3*(2-sqrt(-1)*3)

c2 =

   6.0000 - 9.0000i

» c3=sqrt(-2)

c3 =

        0 + 1.4142i

» c4=(c1+c2)/c3

c4 =

  -7.7782 - 4.9497i

» i^2

ans =

    -1

You can also exploit the Euler identity for complex numbers. Recall that, given a complex number a+ib, the magnitude M and the phase angle q  are related by

a+ib=Meiq,  eiq=cosq+isinq,  M=Ö(a2+b2),

i.e.  a=Mcosq, b=Msinq.  Let’s find M, q  as well as the real and imaginary parts of c1:
 

» abs(c1),angle(c1),real(c1),imag(c1)  %default measure of angle is radians

ans =

    2.2361
 

ans =

   -1.1071
 

ans =

     1
 

ans =

    -2

MATLAB also knws the value of  p:

» pi

ans =

    3.1416
 

1.2 Mathematical Functions. There is a long list of mathematical functions buildt into MATLAB. Included are all of the functions that are tought in standard calculus courses:

sin(x), cos(x), tan(x), cot(x), sec(x), csec(x),
asin(x), acos(x), atan(x), acot(x),    (inverse trig-functions)
log(x), log10(x),   (natural and base 10 log)
exp(x), sinh(x), cosh(x), tanh(x), coth(x), sech(x), csech(x),
asinh(x), acosh(x), atanh(x), acoth(x), asech(x), acsech(x)

As with a pocket calculator you can include all of these functions in a calculation:
 

» 5*cos(7)-acosh(1)

ans =

    3.7695

but the command cos(x) works only if x is assigned to a value:

» x=7;cos(x)

ans =

    0.7539
 

1.3 Output Format. If you use MATLAB to compute cos(p) , you get

» cos(pi)

ans =

    -1

In this case MATLAB realizes that the answer is an integer and displays the result in that form. However, cos(1.57) is not an integer and you obtain

» cos(1.57)

ans =

   7.9633e-004

a floating point number with an accuracy of five significant digits. If you want a more precise answer, type

» format long
» cos(1.57)

ans =

    7.963267107332634e-004

to obtain the answer with an accuracy of sixteen significant figures. After the long format command has been executed all answers will displayed in that format. If you want to return to short format (default), type

» format short

It is important to realize that, although only five significant digits are displayed in the short format, MATLAB is computing the answer to an accuracy of sixteen significant figures.

Another useful format is the rational format. Here the answers will be shown as rational numbers. If the numbers are actually irrational, MATLAB will find a close rational approximation.

» format rat
» 0.45,pi

ans =

     9/20
 

ans =

   355/113


2. Simple Arrays

2.1 Arrays. All computations considered so far involved single numbers, called scalars. Oerations involving scalars are the basis of mathematics. However, when yor wish to to perform the same operation on several numbers at a time, repeated scalar operations become cumbersome. To solve this problem, MATLAB offers operations on data arrays.

Suppose you want to compute sin(x) at the values x = 0, 0.2p, 0.4p, 0.6p, 0.8p, p. To do this, you can create first an array containing these numbers by typing (the numbers can be separated by empty space or by commas)

» x=[0 0.2*pi 0.4*pi 0.6*pi 0.8*pi pi]

x =

     0    3.1416    0.6283    1.2566    1.8850    2.5133    3.1416

Then the command
 

» y=sin(x)

y =

         0    0.5878    0.9511    0.9511    0.5878    0.0000

generates another array, called y, in which the sine of each value of the array x is place at the corresponding position.

2.2 Array Operations. Besides evaluating each number in an array by functions, you can also perform standard arithmetic operations, all at a time. One distinguishes between scalar-array and array-array operations. The command

» 0.5*x-2

ans =

   -2.0000   -1.6858   -1.3717   -1.0575   -0.7434   -0.4292

is a scalar-array operation: each element of x is multiplied by 0.5 and then 2 is subtracted. In case of division you must be careful with the order: the command x/0.5 generates an array in which each element of x is divided by 0.5, but the command 0.5/x will lead to an error message. You can also raise each element to some power. For this the standard exponentation symbol must be supplemented by a period:

» x.^1.4

ans =

         0    0.5217    1.3769    2.4290    3.6336    4.9660

Adding the elements of two arrays can be done with the standard symbol for addition:

» x+y

ans =

         0    1.2161    2.2077    2.8360    3.1011    3.1416

but for multiplying the elements of x and y to each other we need again to supplement the multiplication symbol by a period:

» x.*y

ans =

         0    0.3693    1.1951    1.7927    1.4773    0.0000

Analogously for division (left and right, but make sure that you don’t divide by zero):

» [4 2]./[2 2], [2 2].\[4 2]

ans =

     2     1
 

ans =

     2     1

Similarly you can exponentiate each element of an array by the corresponding element of another array:

» [2,3].*[2 2]

ans =

4 6

and the same symbol can be used to create an array in which a single number (scalar) is raised to the powers taken from a given array:

» 2.^[1 2]

ans =

2  4

2.3 Array Generation. The standard plot command plots two arrays versus each other. For example, if you want to plot a sine, you will define an array of x-values and the corresponding array of sines evaluated at these values and then use the plot command (type help plot). A small array, e.g. 5 values, will not be sufficient - we need sufficiently large arrays to get a nice plot. Typing many numbers into an array is certainly too cumbersome. You can construct  arrays appropriate for plotting in the following ways:

x=linspace(0,pi,6);

x =

         0    0.6283    1.2566    1.8850    2.5133    3.1416

generates an array of 6 uniformly distributed values in the interval 0 £ x £p . If you replace 6 by 20 you have a sufficiently large number for plotting a sine function. A similar command is

» x=0:0.5:pi

x =

         0    0.5000    1.0000    1.5000    2.0000    2.5000    3.0000

which generates an array that starts at the first value (0), adds successively the increment (0.5) and ends at or before the last value (p). Note that if the last value would be 3 instead of p you would get the same array.

2.4 Length of an Array. If you have constructed an array by some means and don’t know its length,
the command

» length(x)

ans =

     7

provides this information.

2.5 Array Addressing and Indexing. In MATLAB, individual array elements are labelled and accessed by integers: x(1) is the first element in x, x(2) the second, x(end)the last, and so on. Besides accessing a single element you can also access a block of elements.

» x(3),x(2:4),x(3:end)

ans =

     1
 

ans =

    0.5000    1.0000    1.5000
 

ans =

    1.0000    1.5000    2.0000    2.5000    3.0000