180
6. Matrices
Hopefully this example makes it clear that the straightforward asterisk operation performs the mathematical operation of multiplication, with multiplication
by the identity matrix A leaving B unchanged, whereas multiplying using .*
perform

1.2 Scalar Quantities and Variables
3
> a = 3;
> b = a+1;
> x = a+b;
The rst line sets the variable a to be equal to 3, the semicolon instructing
MATLAB to execute the command but to suppress the output. The second
line sets b to be equal to a plus one, n

2
1. Simple Calculations with MATLAB
We shall start by introducing MATLAB commands which can be typed at
the MATLAB prompt; these will ultimately form part of our vocabulary of
MATLAB commands. MATLAB already has an extensive vocabulary: however
we will l

1.2 Scalar Quantities and Variables
5
with (whether it be a le containing MATLAB code, or data etc). We will
see more on this later in the section on script les.
2. Variable names in MATLAB are case sensitive, so that a and A are two
dierent objects.
3. I

6
1. Simple Calculations with MATLAB
of error, is in terms of division. We should note that in the syntax of MATLAB
a
a/b*c is not equal to bc but a c. In order to ensure that the denominator of
b
the fraction is calculated rst we would need to use a/(b*c

4
1. Simple Calculations with MATLAB
> z1-z3
> z2+z3-z1
Hopefully you should get the answers, 90, 2, 42 and 25.
Example 1.2 Here we give an example of the simple use of brackets:
>
>
>
>
>
>
>
format rat
a = 2; b = 3; c = 4;
a*(b+c)
a*b+c
a/b+c
a/(b+c)
fo

1.2 Scalar Quantities and Variables
11
message, in this case
? Undefined function or variable xsin.
In cases such as these MATLAB provides useful information as to where we
have gone wrong; information we can use to remedy the syntax error in our
piece of

14
1. Simple Calculations with MATLAB
gives r = [1 3 5] and s = [1 1.5 2 2.5 3 3.5]. We note that if the interval b-a is not exactly divisible by h, then the loop will run up until it exceeds
b, for instance t = 1:2:6 gives t = [1 3 5]. We can also initia

1.4 Vectors in MATLAB
13
There are other options for format which you can see by typing help format.
The default option is format short (which can be reverted back to by simply
typing format). The above options are
short 5 digits
long 15 digits
rat try to