matlab & practical applications on climate …...8 istituto nazionale di geofisica e...
TRANSCRIPT
1
Istituto Nazionale di Geofisica e Vulcanologia
1. Basics Tutorial Program• Working environment• Dealing with matrices• Useful functions• Logical operators• Saving and loading• Data management• Exercises
2. Programming • Basics graphics settings - ex• Functions & scripts• Vectorization• Exercises
3. Applications • Generic scripts • Means• Anomalies • STD • Correlations • Time series: SOI
SSTAdetrendingfiltering
• Interpolation• EOFs REOFs CEOFs• Spectral analysis• Wavelet
day 1
day 2
days 3, 4 & 5
MATLAB & Practical Applications on Climate Variability Studies day 2
B.Aires, 20-24/02/06Centro de Investigaciones del Mar y la Atmosfera andDepartment of Atmospheric and Oceanic Sciences (UBA)E.Scoccimarro, A.F.Carril
2
Istituto Nazionale di Geofisica e Vulcanologia
• …edit the exe-5 graphic settings :Programming
MATLAB & Practical Applications on Climate Variability Studies day 2
script
functions
• functions & scripts :Programming
MATLAB & Practical Applications on Climate Variability Studies day 2
3
Istituto Nazionale di Geofisica e Vulcanologia
Matlab allows commands to be executed in two ways:
InteractiveYou can type commands directly to Matlab, allowing you to proceed through a computation step-by-step,
inspecting variables, and plotting intermediate results.
ProgramBy creating a file of commands, a sequence of commands can be stored and executed as if each were typed in interactive mode.
The file, created in an ordinary editor or by Matlab, should be named ____.m. It is executed by typing its name without the '.m'.
A Program can be a script or a function.
Programming
MATLAB & Practical Applications on Climate Variability Studies day 2
>> edit simple_script.m
4
Istituto Nazionale di Geofisica e Vulcanologia
• ….. simple script :
5
Istituto Nazionale di Geofisica e Vulcanologia
A function is a script that contains “function” on the first line function [avgy] = runavg ( y , len)
• functions & scripts :Programming
MATLAB & Practical Applications on Climate Variability Studies day 2
FUN
CTI
ON
A function can be called from command line,
>> avgy = runavg (y , len);
or by using others functions or scripts.
output inputs
MATLAB & Practical Applications on Climate Variability Studies day 2
6
Istituto Nazionale di Geofisica e Vulcanologia
Programming• function call:
No memory of the internal variables, used by the function.
Programming
MATLAB & Practical Applications on Climate Variability Studies day 2
7
Istituto Nazionale di Geofisica e Vulcanologia
• functions with dynamic number of inputs:
How to use varargin.
Programming
MATLAB & Practical Applications on Climate Variability Studies day 2
8
Istituto Nazionale di Geofisica e Vulcanologia
•Built-in functions:
Functions that are frequently used or that can take more time to execute are often implemented as executable files. These functions are called built-ins.
Unlike M-file functions, you cannot see the source code for built-ins.
As with M-file functions, you can identify which functions are built-ins using the exist function. This function identifies built-ins by returning the number 5:
>> exist maxans = 5
Or trying to edit it: for built-in function you will get only help info.
9
Istituto Nazionale di Geofisica e Vulcanologia
Indexing
MATLAB & Practical Applications on Climate Variability Studies day 2
• Subscripted Indexing:
rows column
• Linear Indexing:
A
B
C
D
10
Istituto Nazionale di Geofisica e Vulcanologia
Indexing
MATLAB & Practical Applications on Climate Variability Studies day 2
•… Two new functions:
IND2SUB: Multiple subscripts from linear index.
SUB2IND: Linear index from multiple subscripts.
>> [ i, j, k ] = ind2sub ( size (A), 16 )
i =1
j =2
k =2
>> ind = sub2ind ( size (A), i, j, k )
ind = 16
Indexing
MATLAB & Practical Applications on Climate Variability Studies day 2
11
Istituto Nazionale di Geofisica e Vulcanologia
• Logical Indexing:
E
With logical, or Boolean, indexing, the index parameter is a logical matrixthat is the same size as A and contains only 0's and 1's.
The elements of A that are selected have a '1' in the corresponding position of the logical indexing matrix.
Indexing
MATLAB & Practical Applications on Climate Variability Studies day 2
12
Istituto Nazionale di Geofisica e Vulcanologia
• Logical Indexing example:
How to use logical indexing power of MATLAB to remove negative values in a series.
Vectorization
MATLAB & Practical Applications on Climate Variability Studies day 2
13
Istituto Nazionale di Geofisica e Vulcanologia
Let’s talk about things that can be done in parallel ………..
Vectorization
MATLAB & Practical Applications on Climate Variability Studies day 2
14
Istituto Nazionale di Geofisica e Vulcanologia
• Matrix Functions of Two Vectors:
To evaluate a function F of two variables:
F(x,y) = x * exp ( -x2 – y2 )
for yi=1:16for xi=1:21
f(yi,xi) = x(xi) .* exp (-x(xi).^2 - y(yi).^2);end
end
MATLAB & Practical Applications on Climate Variability Studies day 2
15
Istituto Nazionale di Geofisica e Vulcanologia
Exercises
•Exe 5:Read nino3 SSTA series in binary format, plot and save the image. DONE
•Exe 6:Vectorizing loops:• 6a. vectorizing a double FOR loop. • 6b. vectorizing code that finds the cumulative sum of a vector every fifth element. (need cumsum function)• 6c. Create a code that repeats a vector value when the following value is 0.
•Exe 7:Read an SST field and plot global mapping in different projections. (starting from the Exe 3 [day 1])
•Exe 8:
Create Scatter Plots. Some info about subplotting and page layout settings.
B.Aires, 20-24/02/06Centro de Investigaciones del Mar y la Atmosfera andDepartment of Atmospheric and Oceanic Sciences (UBA)E.Scoccimarro, A.F.Carril