To get started, type one of these commands: helpwin, helpdesk,
or demo.
>>
The ">>" is a prompt, requiring you to type in commands. One of the
first commands you should type in is to find out what your working directory
is. The working directory is where you will save the results of your calculations.
So, type in
>> pwd
pwd stands for "print working directory". You probably would get an
output like:
ans =
\\ENGNET\MATLAB\bin
Matlab always stores the result of its last calculation in a variable
called ans (short for answer). Now this indicates the working directory.
You can change the working directory using the cd (short for change
directory) command. Do
>> cd C:\temp
>> pwd
ans =
C:\temp
Now, the results of your computations will be saved in the C:\temp directory, when you issue the correct commands (more about that later).
Online help can be accessed for all Matlab commands by issuing the help
command.
>> help <type name of command here>
To get started, you can simply type
>> help
r =
1
2 3 4
A column vector can be created by
>> c = [1; 2; 3; 4]
c =
1
2
3
4
On the other hand, you can use the ' operator (transpose)
>> c = r'
c =
1
2
3
4
Vectors can also be created by incrementing a starting value with a
constant quantity. For example,
>> r = [0:2:10]
r =
0 2
4 6 8
10
creates a row vector, with the first element = 0; each element inceremented
by 2; until the final value of 10.
You can index specific parts of a vector. For example, to get the third
element in the vector r, you can do
>> r(3)
ans =
4
Matrices are 2 dimensional quantities and are created similar to vectors.
We can do
>> a = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
a =
1 2
3
4 5
6
7 8
9
10 11 12
which is a 4x3 matrix (4 rows and 3 columns). We can also use the incremenation
principle to do
>> b = [0:2:10; 1:2:11]
b =
0 2
4 6 8
10
1 3
5 7 9
11
which is a 2x6 matrix. Again, individual elements of the matrix, for
instance the element in the 2nd row, 5th column can be accessed using the
notation:
>> b(2, 5)
ans =
9
The semicolons (;) in the first two commands direct Matlab not to echo
the values of the variables a and b on to the screen immediately after
you type them. Obviously, only vectors that have the same number of elements
can be added or subtracted. Similarly, two matrices with identical number
of rows and columns can be subtracted as follows:
>> a = [1:3:20; 21:3:40];
>> b = [2:3:20; 22:3:40];
>> c = a-b
c =
-1 -1 -1
-1 -1 -1 -1
-1 -1 -1
-1 -1 -1 -1
Matrix multiplication using the * symbol is possible only if
the number of columns in the first matrix equals the number of rows in
the second:
>> a=[1 2 3; 4 5 6]
a =
1 2
3
4 5
6
>> b = a'
b =
1 4
2 5
3 6
>> c=a*b
c =
14 32
32 77
However, if you want to multiply corresponding elements of two matrices,
then you do an array multiply using the .* symbol.
>> a = [1 2 3 4; 5 6 7 8]; b=[2 2 2 2; 3 3 3 3];
>> c=a .* b
c =
2 4
6 8
15 18 21
24
Discussion on matrix "division" using the / symbol for matrices is postponed till later.
Solving systems of linear equations in Matlab
To solve the set of equations
a1 x + b1 y + c1 z = d1
a2 x + b2 y + c2 z = d2
a3 x + b3 y + c3 z = d3
we set this up as a matrix equation of the form
P U = Q
where
P = [a1 b1 c1; a2 b2 c2; a3 b3 c3]
U = [x; y; z]
Q = [d1; d2; d3]
The solution of this system of equations is U = P-1 Q;
this is accomplished in Matlab using
>> U = inv(P)*Q
or by using the backslash \ operator
>> U = P\Q
Exercise:
>> for i = 1:10;
>> a(i) = i*i;
>> end
>> a
a =
1 4 9 16 25 36 49 64 81 100
All statements between the for and the end statements will be executed
as per the command specifications. Example of a for loop where the increment
is not 1 would be >> for i = 1:3:20; ..... etc.
Type in
>> help for
for more details.
Surface or 2-D (functions of two variables) plots are generated using the surf command. More on that later.
To clear a plot, type in >> clg (clear graph)
To generate another plot window, do >> figure
An ASCII data file dat.txt can be loaded using
>> load dat.txt
This provides a variable called dat in the Matlab workspace. All manner of vector/matrix operations can be performed on dat, just like any other variable.
Do >> help save and >>help load for learning all the load/save options.
After you read the next section, click here for a fun Matlab project - generating music with Matlab!
Save the file using the Save Option under the File Menu in the M-File Editor Window. Call it, say, cosineplot.m. Now, to run this program in Matlab, move over to the Matlab Command Window and just type in >> cosineplot