Você está na página 1de 31

Introduction to Matlab & Data Analysis

Introduction to Matlab Environment


Tutorial time: 3 * one hour sessions: Wed 11:00-14:00 Office hours: Tue 09:00-10:00 Course website: http://www.weizmann.ac.il/midrasha/courses/MatlabIntro uvhart@weizmann.ac.il
Yuval Hart, Weizmann 2010 1

Tutorial 1:

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
2

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

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

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. From a simple calculator to complex data analysis toolbox. Many implemented algorithms.

Very useful:

Great tool for scientific work.

Installing Matlab

Classroom Computing supervisor Novel:

Enter to disk Z (username / password)

Boris 2443

See instructions on the course web page


6

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!!!

Computer Structure - Hardware


CPU Memory

Input devices

Storage devices

Output devices
8

Computer Structure - Software

Software Operating system


Gives services to software:

Hardware

Access input/output devices


Memory allocations And much more
9

From Thoughts to Program


I want to add one and one 1+1 mambo-jumbo 010011010011

compiler

Human language

Matlab language

Intermediate language

Computer language
Operating System

10

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

11

Command Window

Command line Matlab as a calculator: Lets run our first command:

1+1

12

Command Window
Previous Command line Output (answer)

Current Command line


The output is displayed Get the previous command by pressing up arrow keyboard Can clear window using the command clc
13

Workspace Window
Workspace Window All workspace variables

Variable value
Demo: matlab(demo) http://www.mathworks.com/support/2009b/matlab/7. 9/demos/WorkingInTheDevelopmentEnvironment.htm l
14

Command History Window

Command History Window History

Try drag the command to the command window

15

Matlab Arithmetic Operators


Plus Unary plus Minus Unary minus multiply power Backslash or left matrix divide Slash or right matrix divide + + * ^ \ /
16

Matlab Special Numbers


pi
i and j eps realmin realmax

3.14159265
Imaginary unit

Relative precision 2-52 Smallest number 2-1022 Largest number (2-eps)* 21023

Inf
NaN

Infinity

(divide by zero or larger then realmax) (0/0 Inf/Inf)

Not a Number

17

Lets Try Matlab as a Calculator

I want to make a fruit salad for 10 people.


Ingredients for 5 people: 6 5 3 1 Price list: orange banana apple melon 1.5 1.2 1 5 oranges bananas apples melon

What is the total num of fruits needed? How much will it cost to make the salad? Answer:

18

Lets Try Matlab as a Calculator II


2*5+10 2*(5+10) What does it tell you? Evaluation Operator evaluation priorities are mathematical

2/3*4

1 3 2 3 4
1/0 0/0 1+2+3+4+5+6 2*realmax Try to use: clc , up/down arrow

(-1+3+2*pi)^(3/4)
What did you get? Inf NaN Try to use in order to write in more than one line What did you get? Overflow

Usually a Matlab command ends with ; to disable echo to the command line. There are 3 ways to display the output: disp() , no ; fprintf()
19

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?
20

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
21

Current Directory Window


Current Directory Current Directory Window

Files
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
22

Changing The Current Directory


1 2 3
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
23

Editor / Debugger Window


Open the Editor: Desktop menu -> Editor You can dock the window

disp(hello world);

F5 / run button

24

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

Editor / Debugger Window


open Editor: Desktop menu -> Editor Save your script as helloWorld run in the command window: helloWorld disp(hello world);

More: Cntrl-C (editor / command win.), Cntrl-V, Cntl-Z, Cntl-F, Cntrl-H Comments % , %{ this is a comment %} Case sensitive (and syntax errors)
26

Handling Errors

Errors - Examples: dis(error) , disp(error):

Syntax error in a command Syntax error / Runtime error in a script

27

Desktop Layout

Layout the arrangement of the windows on the desktop

Any window on the upper right corner

28

Standard Toolbar

Help Window Desktop Edit File


More on desktop :

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

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

30

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 grades participation_in_class = [1.10, 1.05, 1.07, 0.99, 1.10]; % vector holding the students %participation final_grades = (grades .* participation_in_class) + 10; % calculate the students final grades disp(final_grades);

Magic Numbers: - if (final_grades<60) - for i=1:5 - hourly_pay=500*3.62 - if (final_grades<passing_grade) - for i=1:length(students_ID) - hourly_pay=salary*dollarRate
31

Você também pode gostar