matlab & practical applications on climate …...8 istituto nazionale di geofisica e...

15
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 SSTA detrending filtering • 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/06 Centro de Investigaciones del Mar y la Atmosfera and Department of Atmospheric and Oceanic Sciences (UBA) E.Scoccimarro, A.F.Carril

Upload: others

Post on 02-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MATLAB & Practical Applications on Climate …...8 Istituto Nazionale di Geofisica e Vulcanologia •Built-in functions: Functions that are frequently used or that can take more time

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

Page 2: MATLAB & Practical Applications on Climate …...8 Istituto Nazionale di Geofisica e Vulcanologia •Built-in functions: Functions that are frequently used or that can take more time

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

Page 3: MATLAB & Practical Applications on Climate …...8 Istituto Nazionale di Geofisica e Vulcanologia •Built-in functions: Functions that are frequently used or that can take more time

• 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.

Page 4: MATLAB & Practical Applications on Climate …...8 Istituto Nazionale di Geofisica e Vulcanologia •Built-in functions: Functions that are frequently used or that can take more time

Programming

MATLAB & Practical Applications on Climate Variability Studies day 2

>> edit simple_script.m

4

Istituto Nazionale di Geofisica e Vulcanologia

• ….. simple script :

Page 5: MATLAB & Practical Applications on Climate …...8 Istituto Nazionale di Geofisica e Vulcanologia •Built-in functions: Functions that are frequently used or that can take more time

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

Page 6: MATLAB & Practical Applications on Climate …...8 Istituto Nazionale di Geofisica e Vulcanologia •Built-in functions: Functions that are frequently used or that can take more time

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.

Page 7: MATLAB & Practical Applications on Climate …...8 Istituto Nazionale di Geofisica e Vulcanologia •Built-in functions: Functions that are frequently used or that can take more time

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.

Page 8: MATLAB & Practical Applications on Climate …...8 Istituto Nazionale di Geofisica e Vulcanologia •Built-in functions: Functions that are frequently used or that can take more time

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.

Page 9: MATLAB & Practical Applications on Climate …...8 Istituto Nazionale di Geofisica e Vulcanologia •Built-in functions: Functions that are frequently used or that can take more time

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

Page 10: MATLAB & Practical Applications on Climate …...8 Istituto Nazionale di Geofisica e Vulcanologia •Built-in functions: Functions that are frequently used or that can take more time

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

Page 11: MATLAB & Practical Applications on Climate …...8 Istituto Nazionale di Geofisica e Vulcanologia •Built-in functions: Functions that are frequently used or that can take more time

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.

Page 12: MATLAB & Practical Applications on Climate …...8 Istituto Nazionale di Geofisica e Vulcanologia •Built-in functions: Functions that are frequently used or that can take more time

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.

Page 13: MATLAB & Practical Applications on Climate …...8 Istituto Nazionale di Geofisica e Vulcanologia •Built-in functions: Functions that are frequently used or that can take more time

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 ………..

Page 14: MATLAB & Practical Applications on Climate …...8 Istituto Nazionale di Geofisica e Vulcanologia •Built-in functions: Functions that are frequently used or that can take more time

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

Page 15: MATLAB & Practical Applications on Climate …...8 Istituto Nazionale di Geofisica e Vulcanologia •Built-in functions: Functions that are frequently used or that can take more time

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