Professional Documents
Culture Documents
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.
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
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.
+ addition
- subtraction
* multiplication
/ division
^ exponentiation (to the power of)
>> pi
ans =
3.1416
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 =
3.14159265358979
>> 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
follows:
>> e=exp(1)
e =
2.7183
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 =
-1
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 =
1.6331e+16
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
ans =
Inf
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 =
1.4427
>> log(e)
ans =
1
>> log10(e)
ans =
0.4343
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 =
4.1544
>> angle(c)
ans =
0.7133
matlab often provides additional information about in-built functions. You can access this by using
the help command. For example
>> help abs
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.
2.5
1.5
0.5
0
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
soon.
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
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= .
2a
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
[ 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 =
cot(7/16*pi)
>> eval(I)
ans =
0.1989
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!