introduction to matlab

45
Eran Eden, Weizmann 2008 © 1 Introduction to Matlab & Data Analysis Lecture time: Thursday 11:00 – 13:00 Course website: http://www.weizmann.ac.il/midrasha/courses/MatlabIntro/ Lecture 1: Introduction

Upload: sureshcool

Post on 13-Dec-2015

25 views

Category:

Documents


6 download

DESCRIPTION

matlab

TRANSCRIPT

Page 1: Introduction to Matlab

Eran Eden, Weizmann 2008 © 1

Introduction to Matlab & Data Analysis

Lecture time: Thursday 11:00 – 13:00

Course website: http://www.weizmann.ac.il/midrasha/courses/MatlabIntro/

Lecture 1: Introduction

Page 2: Introduction to Matlab

2

Team members Lecturers: Natalie Kalev-Kronik [email protected] Eran Eden Maya Geva

Tutors: Yuval Hart Maya Geva

Exercise checkers: Yuval Dorfan Anat Tzimmer

Page 3: Introduction to Matlab

3

Tips / formalities Course website

http://www.weizmann.ac.il/midrasha/courses/MatlabIntro

The website contains Course material: Lectures + tutorials + other Matlab resources HW and solutions News

Where can I do the HW? On any pc computer at Weizmann (installation of Matlab will be

discussed in the first tutorial) In the tutorial class

Grade HWs 60% + 40% ( Exam)

Course references Official course book: Mastering Matlab 7, Hanselman & Littlefield Matlab built in tutorial and references

Page 4: Introduction to Matlab

4

Tips / formalities Signing up for tutorials

(#1) Wed 11:00 – 12:00 (#2) Wed 12:00 – 13:00 (#3) Wed 13:00 – 14:00

HW assistance at the computer room

Once a week in Levine 101Tuesday 8-9 am

Page 5: Introduction to Matlab

Maya Geva: Image Processing toolbox

Signal processing tollbox, curvefitting toolbox

Eran Eden The Bioinformatics Toolbox Unsupervised learning using Statistics

Toolbox Supervised learning & the Artificial

Neural network Toolbox

5

Course overview Introduction to Matlab

Matlab building blocks: 1D 2D and 3D arrays

Simple data analysis and graphics

Control and boolean logic

Loops

Functions and program design

Cells, structures and Files

Simple algorithms and complexity

Debugger

GUI toolbox

Page 6: Introduction to Matlab

6

For whom is the course intended?

• For student with no or little experience of Matlab- first two thirds of the course.

• For students familiar with Matlab who want to expand their knowledge using Matlab toolboxes- especially last four weeks.

Page 7: Introduction to Matlab

7

What is the course about?

(1) Programming in Matlab

(2) Tackling data analysis problems with Matlab

Page 8: Introduction to Matlab

8

What is the course about?

CAGCATATTTGAAGCCGGGCCCACACACAATTGGGGAACGGATCCCCGCGGCCTCCCGGCA GACCCCGTCCGGCACGACGACGAAGAAGGGGAGGATGAAGTCGAATTTGAAGCGGATGAAG GATGAGGAGAGTGACGAAGAAGAGGACGAAGACGACGAGGTCCTTGACGAGGAAGTGAACT ATTGAATTTGAAGCTTATTCCATCTCAGATAATGATTATGACGGAATTAAGAAATTACTAG CAGCAGCTTTTCCTAAAGGCTCCTGTGAACACTGCAGAACTAACAGATCTCTTAATTCATA CAGAACCATATTGGAAGTGTGAATTTGAAGCTTAAGCAAACAAATGTTTCAGAAGACAGCGATGATGATGATGCAGATGAAGATGAAATTTTTGGTTTCATAAGCCTTTTAAATTTAACTGAAAGAAAGGTACCCAGTGTGCTGAACAAATTAAAGAGTTGGTATTTGAAGCGGGTGAGAAGAACTGTAAAGAATTTGAAGCGGCAGCTGGACAAGCTTTTAAATGACACCACCAAGCCTGTGGGCTTTCTCCTAAGTGAAAGATTCATTAATGTCCCTCCTCAGATTGCTCTGCCCATGCACCAGCAGCTTCAGAAAGAATTTGAAGCAATTTGAAGCCTAGTATTTGAAGCTTCTACCTTCTGAGACCCCGTCCGGCACGACGACGAAGAAGGGGAGGATGAAGTCGAGGATGAAGACGAAGATC GATGAGGAGAGTGACGAAGAAGAGGATTTGAAGCACGAAGACGACGAGGTCCTTGACGAGGAAGTGAATATTGAATTTGAAGCTTATTCCATCTCAGATAATGATTATGACGGAATTAAGAAATTACTGCAGCAATTTGAAGCAAAGGCTCCTGTGAACACTGCAGATTTGAAGCAACTAACAATTCAACAGAACCATATTGGAAGTGTGATTAAGCAAACAAATGTTTCAGAAGACAGCGATGATGATGATGCATTTGAAGCAGATGAAGATGAAATTTTTGGTTTCATAAGCCTTTTAAATTTCTAATAAGCCATGTGGGAAGTGCTCTTTCTACCTTATTTGAAGCACACCATTTGTGGAAGAATTACTGCAGCAATTTGAAGCAAAGGCTCCTGTGAACACTGCAGATTTGAAGCAACTAACA

Example #1 of a data analysis problem

Page 9: Introduction to Matlab

9

What is the course about?

CAGCATATTTGAAGCCGGGCCCACACACAATTGGGGAACGGATCCCCGCGGCCTCCCGGCA GACCCCGTCCGGCACGACGACGAAGAAGGGGAGGATGAAGTCGAATTTGAAGCGGATGAAG GATGAGGAGAGTGACGAAGAAGAGGACGAAGACGACGAGGTCCTTGACGAGGAAGTGAACT ATTGAATTTGAAGCTTATTCCATCTCAGATAATGATTATGACGGAATTAAGAAATTACTAG CAGCAGCTTTTCCTAAAGGCTCCTGTGAACACTGCAGAACTAACAGATCTCTTAATTCATA CAGAACCATATTGGAAGTGTGAATTTGAAGCTTAAGCAAACAAATGTTTCAGAAGACAGCGATGATGATGATGCAGATGAAGATGAAATTTTTGGTTTCATAAGCCTTTTAAATTTAACTGAAAGAAAGGTACCCAGTGTGCTGAACAAATTAAAGAGTTGGTATTTGAAGCGGGTGAGAAGAACTGTAAAGAATTTGAAGCGGCAGCTGGACAAGCTTTTAAATGACACCACCAAGCCTGTGGGCTTTCTCCTAAGTGAAAGATTCATTAATGTCCCTCCTCAGATTGCTCTGCCCATGCACCAGCAGCTTCAGAAAGAATTTGAAGCAATTTGAAGCCTAGTATTTGAAGCTTCTACCTTCTGAGACCCCGTCCGGCACGACGACGAAGAAGGGGAGGATGAAGTCGAGGATGAAGACGAAGATC GATGAGGAGAGTGACGAAGAAGAGGATTTGAAGCACGAAGACGACGAGGTCCTTGACGAGGAAGTGAATATTGAATTTGAAGCTTATTCCATCTCAGATAATGATTATGACGGAATTAAGAAATTACTGCAGCAATTTGAAGCAAAGGCTCCTGTGAACACTGCAGATTTGAAGCAACTAACAATTCAACAGAACCATATTGGAAGTGTGATTAAGCAAACAAATGTTTCAGAAGACAGCGATGATGATGATGCATTTGAAGCAGATGAAGATGAAATTTTTGGTTTCATAAGCCTTTTAAATTTCTAATAAGCCATGTGGGAAGTGCTCTTTCTACCTTATTTGAAGCACACCATTTGTGGAAGAATTACTGCAGCAATTTGAAGCAAAGGCTCCTGTGAACACTGCAGATTTGAAGCAACTAACA

Example #1 of a data analysis problem

Page 10: Introduction to Matlab

10

What is the course about?

10 21 10 21

73 21 18 21

10 4 8 21

3 21 10 45

8 21 2 21

Example #2 of a data analysis problem

Page 11: Introduction to Matlab

11

What is the course about?

Examples #3-4 of data analysis problems

Page 12: Introduction to Matlab

12

What is the course about?

(1) Programming in Matlab

(2) Tackling data analysis problems with Matlab

(3) Learn how to learn Matlab by yourself

Page 13: Introduction to Matlab

13

Why Matlab?

Easy to learn

Easy to debug

Great tool for scientific work Exploring your data Visualizing your data

Many useful “toolboxes”

Page 14: Introduction to Matlab

14

Matlab’s main disadvantage…

It’s slower than other programming languages. (unless you use the compiler)…

Page 15: Introduction to Matlab

15

Background - computers

Input

Output

Page 16: Introduction to Matlab

16

Background - hardware

Memory

CPU

Page 17: Introduction to Matlab

17

Background - software

Machine language

Example: 0111010101111101…

Low level language

Example: Assembly

High level languages

Examples:

Another important player: The operating system

C, C++, C#, Java, Pascal, Perl, Lisp, Matlab

Page 18: Introduction to Matlab

18

The Matlab environmentFirst we need to Open Matlab

Page 19: Introduction to Matlab

19

The Matlab environment

Changing current directoryPrompt / Command

line

Files and Directories inside the current directory

Opening/saving a file

workspace

The command window

Page 20: Introduction to Matlab

20

Matlab can be used as a calculator

Page 21: Introduction to Matlab

21

Our first commandWriting a command in the command line

Page 22: Introduction to Matlab

22

o

Our first script (M-file)(1) Writing the script (2) Saving the script

(3) Defining script name (4) Running the script

Comments start with a %

Page 23: Introduction to Matlab

23

Making errors…

Pressing here will bring you to the line in the script where the error occurred

This command does NOT exist in Matlab!

Page 24: Introduction to Matlab

24

Another script…

Z = peaks; surf(Z);

axis tight

set(gca,'nextplot','replacechildren');

% Record the movie

for j = 1:20

surf(sin(2*pi*j/20)*Z,Z)

F(j) = getframe;

end

% Play the movie twenty times

movie(F,20)

Making sophisticated graphics and animation in Matlab is easy.

We will learn how to do this in two lectures

-3-2

-10

12

3

-2

0

2

-5

0

5

x

Peaks

y

Page 25: Introduction to Matlab

25

Help!!!

help doc

Example: doc disp Google

Page 26: Introduction to Matlab

26

Matlab toolboxes

Page 27: Introduction to Matlab

Eran Eden, Weizmann 2008 © 27

Introduction to Matlab & Data Analysis

Topic #2: The Matlab Building Blocks -

Variables, Arrays and Matrices

Page 28: Introduction to Matlab

28

identifiers Identifiers are all the words that build up the program An identifier is a sequence of letters, digits and underscores “_” Maximal length of identifiers is 63 characters Can’t start with a digit Can’t be a reserved word

Examples of illegal identifiers:

007bond #time ba-baluba if while

Examples of Legal identifiers:

time day_of_the_week bond007 findWord

Page 29: Introduction to Matlab

29

An overview of the main players in a program

VariablesConstantsLibrary functions

User defined functions

Reserved words

Identifiers

Page 30: Introduction to Matlab

30

Reserved words (keywords)

Words that are part of the Matlab language There are 17 reserved words:

for function otherwise try break end return switch catch

Do NOT try to redefine their meaning!

Don NOT try to redefine their library function names either!

if elseif continue global while case else persistent

Page 31: Introduction to Matlab

31

Constants

The value of a constant is fixed and does not change throughout the program

Numbers1000.3

Arrays[ 1 2 3 4 5 ]

Matrices[5 34 2]

Chars‘c’

Strings‘I like to eat sushi’ ‘1 + 2’

Page 32: Introduction to Matlab

32

Variables Why do we need variables?

Example:

>> salary = 9000;>> new_salary = salary * 3;>> disp(new_salary);

27000

salary

new_salary

Computer memory

constant

variable

Library functions

9000

27000

Page 33: Introduction to Matlab

33

Variables Another example:

price_bamba = 3

The Matlab Console

price_bamba = 3

What happens if you omit the ‘;’ ?

Page 34: Introduction to Matlab

34

Variables Another example:

price_bamba = 3n_bamba = 2;

The Matlab Console

price_bamba = 3

What happens when we add the ‘;’ ?

Page 35: Introduction to Matlab

35

Variables Another example:

price_bamba = 3n_bamba = 2;price_bisly = 5n_bisly = 3;

total_price = price_bamba * n_bamba + price_bisly * n_bislyn_bamba = 5total_price

The Matlab Console

price_bamba = 3

price_bisly = 5

total_price = 21

n_bamba = 5

total_price = 21

Page 36: Introduction to Matlab

36

Variables

Tip #1: Give your variable meaningful names.

a = 9000b = 100 are a bad choice for naming variables that store your working

hours and salary!

A more meaningful choice of names wouldsalary = 9000;hours = 5;

Page 37: Introduction to Matlab

37

Variables

Tip #2: Don’t make variable names too long

salary_I_got_for_my_work_at_the_gasoline_station = 9000;

salary_I_got_for_my_work_in_the_bakery =

salary_I_got_for_my_work_at_the_gasoline_station * 3;

disp(salary_I_got_for_my_work_in_the_bakery);

Very bad choice of variable name!!!

When should I use capital letters ?

Tip #3: Whatever you do - be consistent.

Page 38: Introduction to Matlab

38

Variables Types Each variable has a type

Why do we need variable types?

Different types of variable store different types of data

>> a = 10a = 10

>> class(a)ans =double

Returns the type of a variable

The default variable type in Matlab is double

Page 39: Introduction to Matlab

39

Variables Types Each variable has a type

Why do we need variable types?

Different types of variable store different types of data

>> b = 10.56b = 10.5600

>> class(b)ans =double

>> c = 'Bush'c =Bush

>> class(c)ans =char

>> d = trued = 1

>> class(d)ans =logical

>> a = 10a = 10

>> class(a)ans =double

Page 40: Introduction to Matlab

40

Variables Types Different variable types require different memory allocations

How many bytes are required to store this variable: c = 'Bush' ?

>> a = 10.4 %double requires 8 bytesa = 10.4

1 2 3 8

>> b = 'B' %char requires 2 bytesb =B

1 2

1 0 0 0 1 1 0 0 1 0 1 1 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0…

1 0 0 0 1 1 0 0 1 0 1 1 1 0 0 0

Memory allocation and release is done

automatically in Matlab

Page 41: Introduction to Matlab

Computer precision limitations

How much is:>> 0.42 + 0.08 - 0.5 ans = 0

How much is:>> 0.42 - 0.5 + 0.08 ans = -1.3878e-017

41

Page 42: Introduction to Matlab

Special variables

ans

42

>> 4 * 5ans = 20

>> ans + 1ans = 21

Page 43: Introduction to Matlab

Special variables

ans pi inf

43

>> 2 * infans = Inf

>> 1 / 0Warning: Divide by zero.ans = Inf

Page 44: Introduction to Matlab

Special variables

ans pi inf NaN In the tutorial you’ll see more…

44

>> 0 / 0Warning: Divide by zero.ans = NaN

>> NaN + 1ans = NaN

Page 45: Introduction to Matlab

45

Summary

Matlab is a high level language

Matlab working environment

Variables & variable types + how to use them