Professional Documents
Culture Documents
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.
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.
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
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.
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
&
|
~
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
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
If you save this text file as text.m you can call it from the Command window writing e.g.
>> test(1,2)