![Page 1: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/1.jpg)
MATLAB Basics
MATLAB
Basics
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth
![Page 2: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/2.jpg)
MATLAB Basics
Command Desktop
Integrated development system:
• Command Window
• Editor
• Command History
• Workspace Browser
• Profiler
• Current Directory Browser
• Shortcut-Bar
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 2
![Page 3: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/3.jpg)
MATLAB Basics
Online–Help
• Online-Help for all commands, functions etc.
• Commmand Window Help: help [command]
• Help-Browser: helpwin [command]
doc [command]
• Search for searchstring: lookfor searchstring
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 3
![Page 4: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/4.jpg)
MATLAB Basics
Variables
• Names for variables:
– maximum of 63 characters
– Letters, underscore “ “ und digits
– First character must be a letter
– Distinguish small and capital letters
• Assign value to variable: variable 1 = 25
• Standard answering variable ans
• Variables are globally defined in Workspace
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 4
![Page 5: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/5.jpg)
MATLAB Basics
Types of Variables
• Types of Variables:
logical logical values 0 oder 1char character (letters)single floating point single precision 32 Bitdouble floating point single precision 64 BitintX integer X = 8, 16, 32, 64
uintX unsigned integer X = 8, 16, 32, 64
struct array of C-like structurescell array of indexed cellsmap data fast key lookup data structure
• double (default) and char widely used
• Optimised storage with sparse in contrast to full
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 5
![Page 6: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/6.jpg)
MATLAB Basics
Output, basic math operations and constants
• Comma or space after expression shows output,
semi-colon omits ouptut
• Operations: + Addition - Subtraction
* Multiplication / Division
^ Power
• Constants: pi Pi
eps floating point accuracy
inf Infinity
NaN Not–a–Number
• Complexe numbers: i, j Imaginary Unit√−1
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 6
![Page 7: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/7.jpg)
MATLAB Basics
Vectors and matrices
• Vectors: Separation of elements by comma or space:
vektor = [ 1 2 3 ]
• Matrizes: Separation of rows by semi-colon:
matrix = [ 1 2 3 ; 5 6 7 ]
• First element has index 1
• Colon for row/column: zeile 1 = matrix(1,:)
• Combining: matrix = [ vektor ; 5 6 7 ]
• Last element: matrix(:,end)
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 7
![Page 8: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/8.jpg)
MATLAB Basics
Specific vectors und matrizes
• Elements with same step width: fort = (1:2:100)
• Number num of points between start und stop:
linear: linspace(start,stop,num)
logarithmic: logspace(start,stop,num)
• Specific matrizes(m rows, n columns):
Identity matrix: eye(m)
Matrix of ones: ones(m,n)
Matrix of zeros: zeros(m,n)
Random matrix: rand(m,n)
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 8
![Page 9: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/9.jpg)
MATLAB Basics
Math functions
• Numeruous math & trigonometric funktions
sqrt(x) square root rem(x, y) rest after division x/y
exp(x) exponential function round(x) roundlog(x) natural Logarithm ceil (x) round to ceillog10(x) logarithmu of 10 floor(x) round to floor
abs(x) absolut sum(v) sum of vector elementssign(x) signum prod(v) product of vector elementsreal(x) real part min(v) smalles vector elementimag(x) imaginary part max(v) largest of vector elementangle(x) angle of complex number mean(v) arithmetic mean
sin(x) sine atan(x) arcus–tangens ±90◦
cos(x) cosine atan2(x,y) arcus–tangens ±180◦
tan(x) tangens sinc(x) sinc-function sin(πx)/(πx)
• Online–Help by help elfun and help datafun
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 9
![Page 10: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/10.jpg)
MATLAB Basics
Calculating with vectors und matrizes
• Lots of operations usable with vectors and matrizes
• element-by-element operations with dot-operator “.“
• Specific operations for vectors und matrices:
.’ transpose
’ conjugated trans.
inv(x) inversion
det(x) determinant
rank(x) rank
eig(x) eigenvalue
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 10
![Page 11: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/11.jpg)
MATLAB Basics
Structures and Cell Arrays
• Structures for data of different types:
scalars, matrizes, strings, etc.
• Fields of structure contain values
str = struct (’name 1’,value 1,’name 2’,value 2,...)
• Access to field values with “.“-operator: str.name
• Cell arrays: Multidimensional structure
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 11
![Page 12: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/12.jpg)
MATLAB Basics
Administration of variables
• Dimension:
– of a vector: length(vektor)
– of a matrix: size(matrix)
• Anzeigen:
– who [variable]: only variable names
– whos [variable]: name, size,bytes and class
• Deletion:
– of a variable: clear variable
– of all variables: clear, clear all
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 12
![Page 13: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/13.jpg)
MATLAB Basics
Relational operators
• Relational operators: ==, ~=, <, <=, >, >=,
• Test if variable x exists: exist(x)
• Relational operators can be used with scalars, vectors
and matrizes
• Results: Zero (logical false) if operation is not true,
otherwise One (logical true)
• Operator precedence:
1. Mathematical expressions
2. Relational operators from left to right
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 13
![Page 14: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/14.jpg)
MATLAB Basics
Logical operators
• Logical operators: ~ NOT, & AND, | OR, xor XOR
• Logical operators can be used with scalars, vectors
and matrizes
• Results: Zero is logical false, all other values are logical
true. Results are always 0 or 1.
• Operator precedence:
1. Mathematical expressions
2. NOT
3. AND and OR from left to right
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 14
![Page 15: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/15.jpg)
MATLAB Basics
Further operators und logical indexing
• “Shortcut“ operators: && (AND) und || (OR)
Stops if result is fully determined: (1 || expression)
expression is not evaluated
• Any element nonzero: any(vektor)
• All elements nonzero: all(vektor)
• Indizes of nonzero elements: find(vektor)
• Logical indexing: a(a<0) = 1
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 15
![Page 16: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/16.jpg)
MATLAB Basics
Conditional Control
IF–statement
if logical expressionstatements
elseif logical expressionstatements
else
statementsend
SWITCH–statement
switch expression(scalar or string)case value1
statementscase {value2 value3 ...}
statementsotherwise
statementsend
• Separate statements and commands with “,“, “;“
• Nesting of if and switch possible
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 16
![Page 17: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/17.jpg)
MATLAB Basics
Loop Control
• FOR: for variable = expression
statements
end
for n = 1:1:10 ,
f = n^2,
end
• WHILE: while expression
statements
end
while w > 1 ,
w = w + 1 ;
end
• Loop: – Next iteration: continue
– Termination: break
– Termination: return
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 17
![Page 18: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/18.jpg)
MATLAB Basics
MATLAB–Scripts
• M–File: Commands in ASCII–File with extension .m
• Comments: % Comment%{ ... %} Comment in several lines%% Comments as cell-divider
• Continue command in next line: ...
• Show commands and comments: echo on
• Controlled paged output: more on
• Display contents of file: type file
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 18
![Page 19: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/19.jpg)
MATLAB Basics
MATLAB–functions I
• Specific sort of M–files: – Input arguments
– Output of values
– Local variables
• Definition: function [var] = functionname (par)
• Internal indicators: nargin, nargout
• Abort function with message: error(’info’)
• Nested functions possible
• Comments in functions: % Comment
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 19
![Page 20: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/20.jpg)
MATLAB Basics
MATLAB–functions II
• Local variables in function are deleted after function
call
• Static variables: persistent var
Keep their value after function call
• Global variables: global var
Are to be defined as global in function and workspace
and are available to all functions declaring it global
Delete with: clear global
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 20
![Page 21: MATLAB Basicswohlfart/download/folien_grundlagen... · 2016. 3. 17. · MATLAB Basics Math functions † Numeruous math & trigonometric funktions sqrt(x) square root rem(x;y) rest](https://reader034.vdokument.com/reader034/viewer/2022051811/601d29e3636a0019fa12e10e/html5/thumbnails/21.jpg)
MATLAB Basics
MATLAB–functions III
• Inline Functions: f = inline (funktion,var)
• Pseudo-Code: pcode (funktion)
• Delete all functions: clear functions
• Function Handle: f handle = @function
Information: functions(f handle)
Call with: [y1, ..., ym] = feval (f handle, x1, ..., xn)
Matlab/Simulink
Dipl.-Ing. Ulrich Wohlfarth 21