Professional Documents
Culture Documents
Matlab Introduction
Is an interactive, matrix-based system for scientific and
engineering numeric computation and visualization.
You can solve complex numerical problems in a fraction
of the time required with a programming language such
as Fortran or C.
The name MATLAB is derived from MATrix LABoratory.
The command prompt is the symbol >>.
When using MATLAB, the command help functionname
will give information about a specific function.
A list of available functions and commands may be
displayed by typing "help"
You can preview some of the features of MATLAB by
just entering the command demo and then selecting from
the options offered.
Matlab introduction
NOTES:
1.The variable "ans" always stores results of the most
recent command.
2.Comments may be inserted with any command
following the percent sign %"
>> exp(-1) % The exponential quantity e^(-1)
>> ans*exp(1) % Ans here is e^(-1) so this should yield 1
>> ans*2 % Ans here is 1, so this should yield 2
Arrays or rows of values can be generated by entering
values separated by commas or spaces and enclosed in
square brackets. The statement x=[1 3 5 7] stores the
numbers 1 3 5 7 in the variable x
Matlab introduction
THE ELLIPSIS
An array list longer than one line can be continued on the next by
using an ellipsis (... i.e. two or more periods followed by a carriage
return) No MATLAB prompt or display will appear until the enter key
is pressed again. In the following command, the first line contains an
ellipsis followed by a carriage return. The second line is also
followed by a carriage return Only after the second line is entered is
the command executed
>>x=[1 2 3 4 ...
5 6]
THE COLON
Generation of arrays is made painless using the colon ":". Thus
>> x=1:6 % yields the array [1 2 3 4 5 6]
The default increment is one. For a different increment, use for
example
>> y=0.2:.2:1.1 %yields the array [.2 .4 .6 .8 1.0]
Matlab introduction
THE SEMI-COLON
The semi-colon ";", used after a command is equally
useful. It actually suppresses the onscreen display of
results (but not the execution of a command). Thus the
command z=1:300; generates an array with values from
1 to 300 and simply returns to the MATLAB prompt upon
execution without displaying the values on the screen.
The result is
>> z=1:300;
The variable z does indeed exists in the workspace. For
example
>> z(1) % the first element of the array z which should
equal 1
Matlab introduction
11
12
Matlab introduction
13
14
Matlab introduction
15
Matlab introduction
16
17
18
Plotting
For a standard solid line plot, simply type
plot(x,z)
Axis labels are added by using the following commands
xlabel('x')
ylabel('z')
Consider now the following equation
y(t) = 4 e-0.1 t
we can solve this for a vector of t values by two simple
commands
t = 0:1:50;
y = 4*exp(-0.1*t);
and we can obtain a plot by typing
plot(t,y)
Matlab introduction
19
Matlab introduction
20
Matlab introduction
21
Matlab introduction
22
Matlab introduction
23
Matlab introduction
24
25
Matlab introduction
26
Matlab introduction
27
Matlab introduction
28
t = 0:pi/10:2*pi;
[X,Y,Z] = cylinder(4*cos(t));
subplot(2,2,1); mesh(X)
subplot(2,2,2); mesh(Y)
subplot(2,2,3); mesh(Z)
subplot(2,2,4); mesh(X,Y,Z)
Setting Grid Lines
The grid command toggles grid lines on and off. The
statement
grid on
turns the grid lines on and
grid off
turns them back off again.
Matlab introduction
29
30
31
Matlab introduction
32
Workspace window
lists variables that you have either entered or computed
in your MATLAB session
The command who (or whos) lists the variables currently
in the workspace.
A variable or function can be cleared from the workspace
with the command clear variablename or by rightclicking
the variable in the Workspace editor and selecting
Delete. The command clear alone clears all variables
from the workspace.
invoking the command save before exiting causes all
variables to be written to a machine-readable file named
matlab.mat in the current working directory.
When you later reenter MATLAB, the command load will
restore the workspace to its former state.
Matlab introduction
33
Matlab introduction
34
Matlab introduction
35
36
37
Matlab introduction
38
Matlab introduction
39
Matlab introduction
40
The if statement
The general form of a simple if statement is:
if expression
statements
end
The statements will be executed only if the expression is
true. Multiple conditions also possible:
Matlab introduction
41
M-files
MATLAB can execute a sequence of statements stored
in files. These are called M-files because they must
have the file type .m as the last part of their filename
Matlab introduction
42
Matlab introduction
43
Script files
A script file consists of a sequence of normal MATLAB
statements. Typing ddom in the Command window
causes the statements in the script file ddom.m to be
executed. Variables in a script file refer to variables in
the main workspace, so changing them will change your
workspace variables. Type:
A = rand(3)
ddom
A
in the Command window. It seems to work; the matrix A
is now diagonally dominant.
Matlab introduction
44
Function files
Function files provide extensibility to MATLAB.
You can create new functions specific to your problem,
which will then have the same status as other MATLAB
functions.
Variables in a function file are by default local.
Convert your ddom.m script into a function by adding
these lines at the beginning of ddom.m:
function B = ddom(A)
% B = ddom(A) returns a diagonally
% dominant matrix B by modifying the
% diagonal of A.
and add this line at the end of your new function:
B=A;
Matlab introduction
45
46
User input
In an M-file the user can be prompted to interactively
enter input data, expressions, or commands. When, for
example, the statement:
iter = input('iteration count: ') ;
is encountered, the prompt message is displayed and
execution pauses while the user keys in the input data
(or, in general, any MATLAB expression).
Upon pressing the return or entry key, the data is
assigned to the variable iter and execution resumes.
Matlab introduction
47
Echoing Commands.
the commands in a script M-file will not automatically be
displayed in the Command Window. If you want the
commands to be displayed along with the results, use
echo:
echo on
format long
x = [0.1, 0.01, 0.001];
y = sin(x)./x
echo off
Matlab introduction
48
Adding Comments.
It is worthwhile to include comments in a lengthy script M-file.
Any line in a script M-file that begins with a percent sign is treated as
a comment and is not executed by MATLAB.
echo on
% Turn on 15 digit display
format long
x = [0.1, 0.01, 0.001];
y = sin(x)./x
% These values illustrate the fact that the limit of
% sin(x)/x as x approaches 0 is 1.
echo off
If you use echo on in a script M-file, then MATLAB will also echo
the comments, so they will appear in the Command Window.
Matlab introduction
49
Matlab introduction
50
Matlab introduction
51
Matlab introduction
52
Matlab introduction
53
Matlab introduction
54
Matlab introduction
55
Matlab introduction
56
Matlab introduction
57
Matlab introduction
58
Matlab introduction
59
Matlab introduction
60
Matlab introduction
61
Matlab introduction
62
Matlab introduction
63
Matlab introduction
64
Matlab introduction
65