Department of Engineering

IT Services

Basic use

First let's use Matlab just like a calculator. Type

 
1.2 * sin(3.4^2 + log10(5))

to see the value of 12 x sin(3.42 + log105). To keep this value for later use, calling it by the name `result' (for example) type

 
result = 1.2 * sin(3.4^2 + log10(5))

Now you can use this value in a new calculation:

 
new = 3+result/2

(Note that this gives 3 + (result/2) not (3 + (result)/2; you can always use round brackets to specify exactly what you mean:

newnew = (3+result)/2

Matlab knows about complex numbers. You get $\sqrt{-1}$ by typing sqrt(-1), but since most people use i to represent $\sqrt{-1}$ you can get it more easily by typing i. (To allow for the peccadilloes of electrical engineers you can also get it by typing j.) Try typing

 
exp(i*pi)

to check that Matlab also knows about the exponential function and π. (You have just evaluated e.) You can enter complex numbers either in terms of real and imaginary parts (z=x+iy), or in terms of modulus and argument (z=re). Try typing

 z = 3 + 4*i

and

 z = 5 * exp(i*0.9273)

Note that in both cases Matlab's response is in x+iy form, and that the argument (θ) must be specified in radians, not in degrees. You can recover the real part, imaginary part, modulus (absolute value), and argument by typing real(z) imag(z) abs(z) and angle(z) respectively. To find |z2|, for example, type abs(z^ 2).

Warning: If you should type something like i = 10 then the value of i will be redefined and will no longer be $\sqrt{-1}$.

One of the most useful features of Matlab is that functions like sin, log, abs and many others will work on whole lists of numbers simultaneously. Suppose, for example, that we wanted to evaluate the list

\begin{displaymath}\sin\left( -\frac{\pi}{2}\right) , ~\sin0,
~\sin\left( \frac{\pi}{2}\right), ~\sin\pi
\end{displaymath}

for some reason. We could do this by typing

 
sin([-pi/2, 0, pi/2, pi])

Notice that we used ( and ) to enclose the argument to the function sin, but [ and ] to enclose the list of numbers. If you make your list of numbers vertical instead of horizontal, then the answer comes out vertical too. Try:

 
vlist = [-pi/2; 0; pi/2; pi]
sin(vlist)

So, between [ and ] commas are used to put things beside each other, while semicolons put things on top of each other. (You can also use a space instead of a comma, and a line-break (`Return' key) instead of a semicolon -- try it.)

In fact functions like sin will work on two-dimensional arrays of numbers, and the result will have the same shape as the original array. To find

\begin{displaymath}\left[ \begin{array}{ccc}
\log_e1 & \log_e2 & \log_e3 \\
\log_e(-1) & \log_e(-2) & \log_e(-3)
\end{array} \right]
\end{displaymath}

type:

 
try1 = [1, 2, 3 ; -1, -2, -3]
log(try1)