Beruflich Dokumente
Kultur Dokumente
2. MATLAB calculator
Exercises
3. MATLAB visualisation
Exercises
4. MATLAB scripts / functions
Editor
Workspace
Browse for
Current
functions
Folder
Command
Window
History
MATLAB desktop
• Command window: interactive input via command line >>
• Current folder: overview of files in current working directory.
• Workspace: content of workspace (can be modified using the
array editor).
• Command history: overview of recently used commands.
• Run command: F9
• Run m-file: F5
Useful commands
• List of variables in the workspace: who
• Assignment statement
– 1 variable on left side of assignment ( >> A = ...)
– The variable type is automatically determined/adapted
(attention: easy but can cause problems!)
– Example: >> A = width*length;
>> B = 267;
• Variable names
– Up to 31 characters (letters, numbers and _ )
– Case sensitive (A_1 ≠ a_1)
Summary
• MATLAB uses basic Windows logic
• j:i:k - builds[j,j+i,j+2i,…,k]
- is empty when i>0 and j>k or
when i<0 and j<k
• Example: >> d = [1:5]
d =
1 2 3 4 5
>> e = [1:2:10]
d =
1 3 5 7 9
Building arrays
Option 2
• Specify each element explicitly:
a(1) = 3;
You can also select a specific matrix element (of an already defined
matrix) this way, e.g. a_23 = a(2,3);
Option 3
• Use a built-in command such as linspace or logspace
a = linspace(0,4,20);
Option 4
• Use built-in standard arrays
eye, zeros, ones,…
Matrices: elementary functions
• diag: diagonal matrix with vector elements on diagonal
• eye: unit matrix
• linspace: linearly increasing array
• logspace: logarithmically increasing array
• ones: matrix with constant values
• zeros: zero matrix
• rand: random matrix
• sparse: take sparsity of matrix into account
• Example:
size(A,1) yields the number of rows of matrix A
size(A,2) yields the number of columns of matrix A
Matrices: operations
array summation a+b
• MATLAB:
>> A = [ 5 -3 2;
-3 8 4;
2 4 -9];
>> b = [10; 20; 9];
>> x = A \ b
Summary
• The array is the basic MATLAB unit and can be built in
several ways.
• MATLAB allows for all types of computations and
operations.
• Use the HELP-tool!
• Other data types are possible: look up ‘data structures’
using the help-tool for further details.
Exercises
• Exercise 1 – Building a matrix
• plot(X1,Y1,...)
→ plot lines determined by vectors Xn versus Yn
• plot(X1,Y1,LineSpec,...)
→ plot lines determined by Xn,Yn,LineSpec triplets
• plot(...,'PropertyName',PropertyValue,..)
→ setting properties
2D plots: plot → line specs
• LineStyle:
- solid line (default)
-- dashed line
: dotted line
-. dash-dot line
none no line
• Marker (symbol)
+ o * . x s d ^ > < p h
• Color:
y m g r c b k w → help plot !!
2D plots: plot → basic commands
axis on/off → show / don’t show axes
grid on/off → show / don’t show grid
plot(x,y,'--rs',...
'LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10)
Multiple figures
• In same coordinate system: hold on
• In same window: subplot
• In new window: figure
• Programming in MATLAB:
o Scripts: MATLAB commands are executed from a file
(file.m) in the workspace
o Functions: definition of program modules which produce
output(s) given certain input(s)
keywords indents
• Functions
o First line starts with keyword function
o Code is built in memory when first called
o Variables are stored in the function workspace, not in the user
workspace
o Input arguments can be passed
o Output is possible but not necessary (function [])
.m-files: functions
• Name starts with a letter and ends in extension .m
• Maximum 31 characters
• The name of the function is defined on the first line of the m-file
• Should be the same as the name of the file!
• Structure function .m-file:
1. Function definition
2. H1-line
3. Help text
4. Body
5. Commentary
.m-files: functions
1. Function definition
A function is always defined after the keyword function
function y = gemiddelde(x)
function [x, y, z] = sphere(theta, phi, rho)
function printresult(x) of function [] = printresult(x)
2. H1 line
First line of help text where lookfor searches
3. Help text
Commented lines after H1, which are shown using the help function
4. Body
MATLAB code
5. Commentaar
Explanatory text after %
.m-files: functions
Example: traparea.m
Function definition
H1 line
Body Commentary
How does a function function?
• A function can be called:
o From the command window
o From another .m-file
• Passing arguments
o Pass-by-value (intelligently): values that are not adapted are
passed as reference
function area = traparea(a,b,h)
output input
if else elseif
switch case
while
for
Logical expressions
• Logical and conditional expressions:
o Comparative operators: < <= > >= == ~=
o Boolean operators: & (and) | (or) ~ (not)
Example: >> a = 1;
>> b = 2;
>> disp(a>b)
0
• Format: • Example:
switch(switchcondition) x=1;
case case_exp_1 switch x
statements case 0
case case_exp_2 disp(‘x equals 0’)
statements case 1
… disp(‘x equals 1’)
otherwise end
statements
end >> x equals 1
while statement
• while is an extension of the if statement:
o A repetitive action is added
o The statements are executed while the condition is true (i.e. ≠ 0)