You are on page 1of 6

MAB760, september 2006

Introduction to MATLAB
What is MATLAB?
MATLAB is an interactive program package for numerical calculations, graphics and
programming. The name is a shortening of Matrix laboratory and is especially
constructed for matrix calculations.
Environment
MATLAB is used through some interactive windows. In the Command window you give
calculation instructions and receive results. The recommended way of working is to write
the instructions in a text file. These instructions are executed if you write the name of the
file in the Command window.
How to start MATLAB
How to start MATLAB depends on the operating system. On a Unix computer you write
matlab in the terminal window. If you never have been working with MATLAB before I
recommend you to try the commands that I have given in this introduction (after >>) and
run some of the demos included in the help, especially the demo Basic Matrix Operators
that can be found under the topic matlab and matrices.
Help
If help is needed the easiest way to find answers is to open the Help window. You open
the Help window with the blue question mark. In the Help window you can find for
example demos, a search function and much more.
Another way is to use the command help. If you just write
>> help
in the Command window you get a list of all primary help topics. You can choose the
topic you have questions about and write for example
>> help ops
where ops is the topic including operators and special characters. Here you will find the
operator times, if you want to know more about this command write
>> help times
and so on.

MAB760, september 2006


Variables
In MATLAB you do not have to declare variables, you create them when they are
needed. To allot a scalar 7.5, to the variable a, you write
>> a=7.5
When you create a matrix rows are separated by semicolons.
>> A=[1 2 3; 4 5 6; 7 8 9]
A=
1
4
7

2
5
8

3
6
9

There are a feew different ways to allot a sequence of numbers to a matrix or vector
>> x=1:5
x=

>> x=1:0.5:5
x = 1.0

1.5

2.0

2.5 3.0

3.5

4.0

4.5

5.0

>> x=linspace(1,5,9)
x = 1.0

1.5

2.0

2.5

3.0

3.5

4.0

4.5

5.0

To receive a certain element in a matrix you indicate the index inside a parenthesis e.g.
A(2,3). The first index indicates the row and the second index indicates the column. If a
colon are used as an index you receive the whole row or column.
>> A(2,:)
ans =
4

Every time you create a variable it will exist during the whole session if you do not
remove it. To remove a variable you use the command clear.

MAB760, september 2006


Arithmetic operators
^
*
/
+
-

Matrix power
Matrix multiplication
Matrix division
Addition
Subtraction

To receive an element-by-element operator you write a dot before the operator. Note the
difference:
>> a=[1 7; 5 3];
>> a^2
ans =
36
20

28
44

>> a.^2
ans =
1
25

49
9

Elementary math functions


MATLAB provides several mathematical functions for example:
Function

Command

ex
ln x

exp(x)
log(x)

log10 x
|x|

log10(x)
abs(x)

x
sin x

sqrt(x)
sin(x)

Most of the functions work element-by-element, the answer becomes a vector with the
same length as x.

MAB760, september 2006


Figures

In Matlab there are great possibilities to handle graphics. The simplest example is to draw
a function with the use of the function plot. To plot y=sin(x) in the interval -2 to 2
write
figure(1)
%ppnar/skapar figur 1
clf
%rensar figuren
x=-2*pi:pi/10:2*pi; %matrisen fr x mste definieras
y=sin(x);
%berknar vrden fr y
plot(x,y);
%ritar funktionen
The command hold on or hold off is used to plot several functions in the same
window, otherwise will the previous function be overwritten. Use help plot to find
out different arguments for the function, for example different colors.
Relational operators

<
<=
>
>=
==
~=

Less than
Less than or equal
Larger than
Larger than or equal
Equal
Not equal

Relational operators perform element-by-element comparisons between two arrays. They


return a logical array of the same size, with elements set to 1 where the relation is true,
and elements set to 0 where it is false.
Logical operators

&
|
~
xor

And
Or
Not
Exclusive or

The difference between or and exclusive or is that or becomes true if one or both of the
operands are true while xor is true if one but not both of the operands are true.
So xor means either or not both thing and the other.
M-files

Long sequences of matlab commands can be troublesome to write in the Command


window. The best way is to write and save them in a text-file and then request matlab to
execute the commands. This is especially important if the sequence of commands shall be
performed several times. But also if the commands are performed just one or a few times
because it is much easier to correct errors you make. Matlab provides an editor that is
opened if you write
>> edit

MAB760, september 2006

in Command window.
Before you start to declare your instructions write some text for explanations. This text is
written after a percent sign.
% Explanations
a=x+y
z=a+1

MAB760, september 2006


Save this text file as test.m. Then write,
>> x=6; y=5; a=17;
>> test
in Command window to create the variables x, y and a and execute the instructions of the
file test. Note that the current directory must be the one you have saved test in.
A file like this that contains a sequence of commands is called a script file. It is the
simplest type of m-files. A script file can operate on data that is contained in the
workspace and create new. All variables that are created here exists in the workspace to
be used for new calculations, they are global.
The result we receive from test is a=11 and z=12, we notice that the value of a is
changed. The fact that variables unintentional may change values can cause troubles.
The measure is to use a so-called function. In a function all variables are local, they do
not affect variables that have the same name outside the file. In a function you always
start to write a function definition line. This line defines the function name, and the
number and order of input and output arguments.
function [output]=filename(input1, ,inputn)
Example:
function z=test(x,y)
a=x+y;
z=a+1;

If you save this text file as text.m you can call it from the Command window writing e.g.
>> test(1,2)

You might also like