31
1 Introduction to Matlab & Data Analysis Tutorial time : 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website : Tutorial 1: Introduction to Matlab Environment

1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

Embed Size (px)

Citation preview

Page 1: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

1

Introduction to Matlab & Data Analysis

Tutorial time: 3 * one hour sessions:

Wed 11:00-14:00Office hours:

Tue 09:00-10:00Course website:

Tutorial 1: Introduction to Matlab

Environment

Page 2: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

2

Tutorial Modes Hands off (Most of the tutorial)

Presentation – Repeating lecture material New material

Hands on Examples

Home work submit here Usually two weeks after submission Checkout the course website for updates and

FAQs

Page 3: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

3

Before we start

Problems If you have computer problems while using the room, please contact:

Irit Orr (#2470) or the WICC PC Helpdesk (#3440).

If you see issues that can be taken care of after your session is over, please contact Irit Orr (#2470) or Ariela Mendel (#3938).

Page 4: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

4

Before we start Rules of use

Please help us keep the room in good working order.

Do not allow food or drink to be brought into the room

Report problems promptly When leaving the room at the end of a session: Leave the computers on Turn off the projector Turn off all the heating / air conditioning units.

This can be tricky; cup your hand around the end of the remote control to help focus the beam on one unit at a time.

Put the doorstop on the ledge under the whiteboard at the front of the room

Put garbage into the receptacle Turn off all the lights, including the spotlights

that are controlled by the dimmer switch Close the door and make sure that it locks

Page 5: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

5

Become a Friend of Matlab

Why? It has very attractive visualizations It wants to be your friend:

Very easy to learn Great HELP (!) menu and tutorial Check out the resources in our website.

Very useful: From a simple calculator to complex

data analysis toolbox. Many implemented algorithms.

Great tool for scientific

work.

Page 6: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

6

Installing Matlab

Classroom Computing supervisor Novel:

Enter to disk Z (username / password) Boris 2443

See instructions on the course web page

Page 7: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

7

Goals of This Tutorial

1. Familiarizing ourselves with the Matlab Environment

2. Learning basic operations (such as changing

directory)

3. Learning to use Matlab as a simple calculator

4. Writing our first script

5. Learning to use the Help!!!

Page 8: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

8

Computer Structure - Hardware

Input devices Output devices

Storage devices

CPU Memory

Page 9: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

9

Computer Structure - Software

Hardware

Gives services to software:

Access input/output devices

Memory allocations

And much more…

Software

Operating system

Page 10: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

10

From Thoughts to Program

Human language

Matlab language

Intermediate language

Computer language

compiler

I want to add one and one

1+1 mambo-jumbo

010011010011

Operating System

Page 11: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

11

Matlab - Background

Matlab (Matrix Laboratory) – An interactive program that is suitable

for running computations, drawing graphs and much more (graphical interface).

A programming language for technical computing (scripts).

Lets start…

Page 12: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

12

Command Window

Command line

Matlab as a calculator:

Lets run our first command:

1 + 1

Page 13: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

13

Command Window

Previous Command line

The output is displayed

Get the previous command by pressing up arrow keyboard

Can clear window using the command clc

Output (answer)

Current Command line

Page 14: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

14

Workspace Window

Workspace Window

All workspace variables

Variable value

Demo:

matlab(‘demo’)

http://www.mathworks.com/support/2009b/matlab/7.9/demos/WorkingInTheDevelopmentEnvironment.html

Page 15: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

15

Command History Window

Command History Window

History

Try drag the command to the command

window

Page 16: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

16

Matlab Arithmetic Operators

Plus  +

Unary plus  +

Minus -

Unary minus -

multiply *

power ^

Backslash or left matrix divide \

Slash or right matrix divide /

Page 17: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

17

Matlab Special Numbers

pi 3.14159265…

i and j Imaginary unit

eps Relative precision 2-52

realmin Smallest number 2-1022

realmax Largest number (2-eps)* 21023

Inf Infinity (divide by zero or larger then realmax)

NaN Not a Number (0/0 Inf/Inf)

1

Page 18: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

18

Lets Try Matlab as a Calculator

I want to make a fruit salad for 10 people.

Ingredients for 5 people:

6 oranges

5 bananas

3 apples

1 melon

Price list:

orange 1.5

banana 1.2

apple 1

melon 5

What is the total num of fruits needed?

How much will it cost to make the salad?

Answer:

Page 19: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

19

Lets Try Matlab as a Calculator II

2*5+10

2*(5+10) What does it tell you?

2/3*4

1/0 What did you get?

0/0

1+2+3+4+5+6 Try to use … in order to write in more than one line

2*realmax What did you get?

Try to use:clc , up/down arrow

43231

Usually a Matlab command ends with “;” to disable echo to the command line.

There are 3 ways to display the output: disp() , no “;” fprintf()

Evaluation

Operator evaluation priorities are mathematical(-1+3+2*pi)^(3/4)

Overflow

Inf NaN

Page 20: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

20

Using the Help

Help: Help (“help cos”); lookfor Doc (“doc cos”); Search F1 / right click Google “matlab …”

“Getting started” Matlab contains many

mathematical functions. Matlab is case

sensitive!

1. Search for “elementary math functions reference” and find the mathematical function reference list

2. How do I calculate “square root” ? Search...

3. What does the function “factor” do?

4. What are the differences between “ceil” “floor”, “fix” and “round”? Try to run “Fix” – what happened?

Page 21: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

21

Output Format

Use the format function to control the output format of numeric values displayed in the Command Window.

format short (5 digits) , format Long (15 digits) , format rat

Pi = 3.1416, 3.141592653589793 , 355/113

format Compact / format loose – determines the line spacing in the output

Checkout the doc of “format” …

Page 22: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

22

Current Directory Window

Current Directory

Double click on a file will open it:

.m file (script) in Editor/Debugger

.fig file (figure) in Graphic window

.mat file (data) will load variables to the workspace

Current Directory Window

Files

Page 23: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

23

Changing The Current Directory

1

23

pwd – current path

cd – change directory

./ - current directory

../ - parent directory

Lets change our directory to our working directory.

We will need to do so on every tutorial start

Page 24: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

24

Editor / Debugger Window

Open the Editor: Desktop menu -> Editor

F5 / run button

disp(‘hello world’);

You can dock the window

Page 25: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

25

Set Matlab Search Path Matlab search for scripts and functions in:

Current directory. Path directories.

Can be done also using addpath, rmpath , path etc. Matlab search order:

variables -> keywords -> current directory -> search path.

Page 26: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

26

Editor / Debugger Window

open Editor: Desktop menu -> Editor

Save your script as helloWorld

run in the command window: helloWorld

More:

Cntrl-C (editor / command win.), Cntrl-V, Cntl-Z, Cntl-F, Cntrl-H

Comments % , %{ this is a comment %}

Case sensitive (and syntax errors)

disp(‘hello world’);

Page 27: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

27

Handling Errors Errors - Examples: dis(‘error’) ,

disp(‘error): Syntax error in a command Syntax error / Runtime error in a

script

Page 28: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

28

Desktop Layout Layout – the arrangement of the

windows on the desktop

Any window on the upper right corner

Page 29: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

29

Standard Toolbar

Help Window Desktop Edit File

More on desktop :

http://www.mathworks.com/support/2007a/matlab/7.4/demos/desktop.html

Page 30: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

30

A List Of Matlab Operators

Arithmetic operators.     plus       - Plus                                         +     uplus      - Unary plus                               +     minus      - Minus                                      -     uminus     - Unary minus                           -     mtimes     - Matrix multiply                        *     times      - Array multiply                           *     mpower     - Matrix power                          ^     power      - Array power                             .^     mldivide   - Backslash or left matrix divide    \     mrdivide   - Slash or right matrix divide        /     ldivide    - Left array divide                        .\     rdivide    - Right array divide                      ./     kron       - Kronecker tensor product         kron

  Relational operators.     eq         - Equal                                      ==     ne         - Not equal                                 ~=     lt         - Less than                                   <     gt         - Greater than                              >     le         - Less than or equal                     <=     ge         - Greater than or equal                >=

  Logical operators.     and - Logical AND                           &     or         - Logical OR                               |     not       - Logical NOT                            ~     xor       - Logical EXCLUSIVE OR     any      - True if any element of vector is nonzero     all        - True if all elements of vector are nonzero

Page 31: 1 Introduction to Matlab & Data Analysis Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: Tutorial 1:

31

Submitting Exercises – documentation and proper coding

% HW_1 solution <HW_# solution>% Yuval Hart 03922881 <name T.Z.> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% short description: % this program calculates and displays the Matlab students grades.% calculation is based on grades in the final exam and a number between % 0.0-2.0 which present the participation of each student in the class.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% grades = [80 77 70 60 65]; % vector holding the students gradesparticipation_in_class = [1.10, 1.05, 1.07, 0.99, 1.10]; % vector holding the students %participationfinal_grades = (grades .* participation_in_class) + 10; % calculate the students final grades disp(final_grades);

- if (final_grades<60) - for i=1:5- hourly_pay=500*3.62

Magic Numbers:

- if (final_grades<passing_grade)- for i=1:length(students_ID)- hourly_pay=salary*dollarRate