Port and Harbour Engineering Tutorial: 20 February 2019
Port and Harbour Engineering Tutorial: 20 February 2019
20 February 2019
Part 1
-
MATLAB Introduction
1. Matlab introduction
a. Basic operations with matrices and arrays
b. Plots
c. Programming
d. Functions
2. Data analysis tutorial
a. Spectral analysis
b. Significant Wave Height (Hs)
Default MATLAB desktop
Entering Commands and Expressions
whos Lists the current variables and sizes, and indicates if they have
imaginary parts
an array
Inf Infinity
3x3 Matrix
2x4 Matrix
Matrices (continued...)
E(i,j) means a single number – the element on intersection
of the i-th row and the j-th column
Not to be confused with i and j used here as counters of rows
and column with i and j used in mathematics to denote the
imaginary unit
D(1,2) = 2
D(2,1) = 3
D(2,2) = 4
>>A = [2,4,10;16,3,7];
A =
2 4 10
16 3 7
In Matlab, they use both matrices and what they call arrays
u = [0:0.1:10];
>>u = [0:0.1:10];
>>w = 5*sin(u);
101 times.
Arrays indexing
>>u(7)
ans =
0.6000
>>w(7)
ans =
2.8232
Use the length function to determine how many values are in an array
>>m = length(w)
m =
101
Commonly used mathematical functions
MATLAB trigonometric
functions use radian measure
Relational operators
Relational operators examples
>> x = [6,3,9]; y = [14,2,9];
>> z = (x < y)
z =
1 0 0
>>z = ( x > y)
z =
0 1 0
>>z = (x ~= y)
z =
1 1 0
>>z = ( x == y)
z =
0 0 1
Some additional array functions
[u,v,w] = find(A) Computes the arrays u and v, containing the row
and column indices of the nonzero elements of
the matrix A, and the array w, containing the
values of the nonzero elements. The array w may
be omitted.
You can run the file by typing its name at the Command window prompt.
Command window and Editor
Keep in mind when using script files
1. The name of a script file must begin with a letter, and may include
digits and the underscore character, up to 31 characters.
3. If not, MATLAB then looks in the current directory for a file named
problem1.m and executes problem1 if it finds it.
x =
Note that the portion of the line before the % sign is executed to
compute x.
Programming Style
1. Comments section:
a. The name of the program and any key words in the first line.
b. The date created, and the creators' names in the second line.
c. The definitions of the variable names for every input and output.
2. Input section: Include input data and/or the input functions and
comments for documentation
3. Calculation section
4. Output section: This section might contain functions for displaying
the output on the screen.
Help center
User-defined functions
The first line in a function file must begin with a function definition
line that has a list of inputs and outputs. This line distinguishes a
function M-file from a script M-file. Its syntax is as follows:
Note that the output variables are enclosed in square brackets, while
the input variables must be enclosed with parentheses. The function
name (here, name) should be the same as the file name in which it is
saved (with the .m extension).
Example
function z = fun(x,y)
u = 3*x;
z = u + 6*y.^2;
Note the use of a semicolon at the end of the lines. This prevents the
values of u and z from being displayed.
Note also the use of the array exponentiation operator (.^). This
enables the function to accept y as an array.
Functions definition line examples
1. One input, one output: