Beruflich Dokumente
Kultur Dokumente
M-files
functions
Display Windows
Variables
Variable names:
Must start with a letter
Matlab is case sensitive, i.e. one & OnE are different variables.
Assignment statement:
Variable = number; Variable = expression;
Example:
>> tutorial = 1234; >> tutorial = 1234 tutorial = 1234
Vectors
Vector Addressing A vector element is addressed in MATLAB with an integer index enclosed in parentheses. Example:
>> x(3) ans = 1.5708
The colon notation may be used to address a block of elements. (start : increment : end)
start is the starting index, increment is the amount to add to each successive index, and end is the ending index. A shortened format (start : end) may be used if increment is 1.
Example:
>> x(1:3) ans = 0 0.7854 1.5708
Matrices
A Matrix array is two-dimensional, having both multiple rows and multiple columns, similar to vector arrays: it begins with [, and end with ] spaces or commas are used to separate elements in a row semicolon or enter is used to separate rows.
A is an m x n matrix.
Matrix Index
The matrix indices begin from 1 The matrix indices must be positive integer
Given:
Example: >> f = [ 1 2 3; 4 5 6] f= 1 2 3 4 5 6
A(-2), A(0) Error: ??? Subscript indices must either be real positive integers or logicals. A(4,2) Error: ??? Index exceeds matrix dimensions.
Operators (arithmetic)
+ * / ^
Matrices
Transpose Identity Matrix B=A eye(n) returns an n x n identity matrix eye(m,n) returns an m x n matrix with ones on the main diagonal and zeros elsewhere. C=A+B C=A B B = A, where C = A*B B = inv(A), A must be a square matrix in this case. rank (A) returns the rank of the matrix A. B = A.^2 C =A*A matrix. squares each element in the matrix computes A*A, and A must be a square is a scalar.
Addition and subtraction Scalar Multiplication Matrix Multiplication Matrix Inverse Matrix Powers
x1 x2 x3
10 b 5 1
Determinant
Ax = b
Control Structures
Example: (if else and elseif clauses) if temperature > 100 disp ( Too hot equipment malfunctioning. ) elseif temperature > 90 disp ( Normal operating range. ); else disp ( Too cold turn off equipment. ) end
Visualization: Plotting
Example: >> s = linspace (-5, 5, 100); >> coeff = [ 1 3 3 1]; >> A = polyval (coeff, s); >> plot (s, A), >> xlabel ('s') >> ylabel ('A(s)')
-0.8 -1
10
20
30
40
50
60
70
80
90
100
Display Facilities
title(.)
This is the sinus function 1 0.8
0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 -0.1 -0.2 -0.3 0 10 20 30 40 50 60 70 80 90 100
xlabel(.) ylabel(.)
Use of M-File
Click to create a new M-File
You should write this command at the beginning of the m-file and you should save the m-file with a file name same as the function name
Example
function [V,D,r]=properties(A) % Esta funcin encuentra los % eigenvalores, eigenvectores % y rango de A, dode A es una % matriz cuadrada [m,n]=size(A); if m!=n display else r = rank(A); [V,D] = eig(A); end
Function mult computes the multiplication of A by its transpose The variable r is only visible inside the function
To get all the output parameters the funcion must be called as:
[V,D,r]=properties(A)
Useful Commands
>>lookfor keyword