Anda di halaman 1dari 6

MT1242: Introduction to Matlab 1

1 Introducing matlab
matlab is short for Matrix Laboratory, and was designed in the first instance for the purposes
of numerical linear algebra. Since its conception it has developed many advanced features for the
manipulation of vectors and matrices. It can solve large systems of equations efficiently and is
therefore useful for solving differential equations and optimization problems. It also provides excellent
means for data visualization and has symbolic capabilities. Whilst simple problems can be solved
interactively with matlab, its real power shows when given calculations that are cumbersome or
extremely repetitive to do by hand.

1.1 Calling Matlab

The easiest way to start Matlab is by clicking the Start menu and then selecting Programs core
followed by scientific software, Matlab and finally Matlab R12, i.e. the sequence

Start--Programs core--Scientific software--Matlab--Matlab R12},

After a short initiation sequence Matlab will appear. Using the default settings Matlab appears
with 3 active sub-windows, other tools can be started from the view menu. You can have up to six
active windows with the following features

Command Window This is where you type commands in at the >>

Command History gives a list of recent commands.
Current Directory allows you to access files in a different directory
Workspace Allows you to access all information about the the vari-
ables you have defined. NB: you can view them by
double-clicking the yellow symbols to activate the array
Launch Pad allows you to launch demos and the help utility
Help Allows you to navigate the online help facility

To save space you can overlap windows by dragging the titlebar of one window over to the title bar
of another, and tabs allow you switch between the windows.

1.2 matlab as a calculator

The basic mathematical operators are defined as follows:

+ addition
- subtraction
* multiplication
/ division
^ exponentiation (to the power of)

Although employing matlab as a calculator is under-using its computational capabilities, it can

serve as an introduction to the wide range of functions available. For example, matlab stores the
value of π directly:
MT1242: Introduction to Matlab 2

>> pi
ans =
This might not have been the answer, that you were expecting. However, matlab has two formats
for displaying the value of a variable, short and long. The default is short, but you can toggle
between them, using the format command:
>> format long
>> pi
ans =
>> format short
matlab always stores variables in long form. Unlike π, matlab doesn’t store the value of e in the
same way. Instead, it has a function exp to compute exponentials and you can assign variables as
>> e=exp(1)
e =
It is possible to suppress the output of a function from the command bar. For example, if wished
to assign the value π to a variable a, we would use a semi-colon after the instruction in order to
suppress the output:
>> a=pi;
matlab also has the standard range of trigonometric functions, such as sin, cos, tan
>> cos(a)
ans =
The command lookfor instructs matlab to scan through its standard functions, looking for refer-
ences to the word specified. For example,
>> lookfor sine
ACOS Inverse cosine.
ACOSH Inverse hyperbolic cosine.
ASIN Inverse sine.
ASINH Inverse hyperbolic sine.
COS Cosine.
COSH Hyperbolic cosine.
SIN Sine.
SINH Hyperbolic sine.
TFFUNC time and frequency domain versions of a cosine modulated Gaussian pulse.
Sometimes, computer arithmetic does not always produce the right answer, for example ∞. We know
that tan π2 = ∞, however will matlab get the right answer?
>> tan(pi/2)
ans =
MT1242: Introduction to Matlab 3

Unfortunately not! This is due to the floating point capabilities of the machine being used. matlab
is however aware of the existence of the value ∞, for example
>> 1/0

Warning: Divide by zero.

ans =

matlab is also familiar with logarithms! You may recall that the logarithm of a number x, with
base n, is defined to be the value a such that na = x. There are many types of logarithm available
in matlab, for example base 2, natural and base 10:
>> log2(e)
ans =
>> log(e)
ans =
>> log10(e)
ans =
matlab also has a range of commands for evaluating complex numbers. matlab uses i to represent

−1. For example:
>> c=a+i*e
c =
3.1416 + 2.7183i
>> abs(c)
ans =
>> angle(c)
ans =
matlab often provides additional information about in-built functions. You can access this by using
the help command. For example
>> help abs

ABS Absolute value.

ABS(X) is the absolute value of the elements of X. When
X is complex, ABS(X) is the complex modulus (magnitude) of
the elements of X.


You are often pointed towards other in-built functions that are similar to the function of interest.
There is also an on-line version of the help facility, called helpdesk. To access this simply type
MT1242: Introduction to Matlab 4

helpdesk in the command bar. helpdesk contains all the information that can be bought up via
help in addition to useful features such as hyper-links to related commands and helpful illustrations
of how various commands can be used. helpdesk can prove a powerful ally when using matlab and
you should familiarise yourself with it!
When you have been using matlab for a while, it can often become quite tricky to remember all
the different variables that you have introduced. Fortunately matlab can remind you of them:
>> who
Your variables are:
a ans c e
al b
Note that pi and i are not listed, as they are built in to matlab. The command whos, gives a more
descriptive version of the above, including the size of each variable.

1.3 Visualization using matlab

One of the many nice features of matlab, is it’s ability to produce information graphically.

1.3.1 Argand diagrams

Consider a complex number a + bi, where a and b are both real numbers. This can be represented
by the vector (a, b) on an Argand diagram, which represents the real numbers on the x-axis and
imaginary numbers on the y-axis. A visual representation of a + bi can be made by drawing a line
between the origin (0, 0) and the point (a, b).
We may use matlab to produce such a diagram, using the plot command. Recall that we created
a complex number c in the previous section. matlab can think of the number c as simply a point
in two dimensional space and in order to tell it to join this point to the origin, we use the following
>> plot([0,c])
This produces the following graph:




0 0.5 1 1.5 2 2.5 3 3.5

In order to produce the graph we had to vectorize the two points. We did this by creating an array
using the square brackets [ ]. We will learn more about arrays and in particular vectors and matrices
MT1242: Introduction to Matlab 5

After a time you may wish to remove some or all the variables that you have stored in the computer’s
memorybank. The command clear is used for this purpose:

>> clear e

clears the individual variable e (watch it disappear from the workspace window), whereas

>> clear

removes all variables from the memorybank.

1.4 matlab as an equation solver

matlab is particularly good at solving systems of equations. There are a number of ways in which
we can ask matlab to do this.

1.4.1 The quadratic formula

All of you should be familiar with the formula for finding the roots of a quadratic equation. We shall
first briefly discuss this formula, before seeing how matlab handles such equations.

A quadratic equation is one of the form

ax2 + bx + c = 0

and often a question of interest is given values of a, b and c what are the values of x which satisfy this
equation? These values are often called roots and there are the same number of roots as the highest
power of the equation. Consequently for a quadratic equation, we seek two roots. By re-arranging
the equation in the correct manner we discover that the roots of the problem are given by the formula

−b ± b2 − 4ac
x= .
It is important to note that even if all the coefficients (i.e. a, b and c) are real, the roots of the
equation may not be! matlab has symbolic capabilities which can manipulate equations such as
those above. We shall briefly discuss how:
We must first inform matlab that we wish to make use of its symbolic capabilities. It is essential to
inform matlab of the variables that we want it to consider as symbols – the command syms exists
for this purpose. Note that it is not a standard feature of matlab and therefore not all versions
have symbolic capabilities.

>> syms a b c x
>> whos
Name Size Bytes Class

a 1x1 126 sym object

b 1x1 126 sym object
c 1x1 126 sym object
x 1x1 126 sym object

Grand total is 8 elements using 504 bytes

MT1242: Introduction to Matlab 6

>> y = solve(a*x^2 + b*x + c)

y =
[ 1/2/a*(-b+(b^2-4*a*c)^(1/2))]
[ 1/2/a*(-b-(b^2-4*a*c)^(1/2))]
>> pretty(y)

[ 2 1/2]
[ -b + (b - 4 a c) ]
[1/2 --------------------]
[ a ]
[ ]
[ 2 1/2]
[ -b - (b - 4 a c) ]
[1/2 --------------------]
[ a ]
matlab has some basic “artificial intelligence” built in. An example of this is the fact that unless we
specify otherwise, it solves the equation with respect to the symbolic variable x, or alternatively the
symbolic variable with the letter which lies closest alphabetically to x. We may however, specifically
instruct matlab to solve the equation with respect to a different symbolic variable, for example b:
>> y = solve(a*x^2 + b*x + c, b)
y = -(a*x^2+c)/x
You can evaluate symbolic expressions when the parameters take specific numerical values using the
subs command:
>> subs([y],{a,b,c},{1,1,1})
ans =
-(x^2 + 1)/x

You can also get Matlab to symbolically solve integrals and evaluate answers
>> syms I x
>> I=int(1/(1+cos(x)),0,pi/8)

I =


>> eval(I)

ans =

When you’ve decided enough is enough use either quit or exit:
>> exit

Remember the best way to learn matlab is by trying it yourself, so make sure you make use of the
support classes and other facilities available to you!