470 likes | 919 Views
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. Introduction to Matlab & Data Analysis. Tutorial Modes. Hands off (Most of the tutorial) Presentation –
E N D
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 Introduction to Matlab & Data Analysis
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
Before we start • ProblemsIf 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 usePlease 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
Great tool for scientific work. 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.
Installing Matlab • Classroom • Computing supervisor • Novel: • Enter to disk Z (username / password) • Boris 2443 See instructions on the course web page
Familiarizing ourselves with the Matlab Environment Learning basic operations (such as changing directory) Learning to use Matlab as a simple calculator Writing our first script Learning to use the Help!!! Goals of This Tutorial
CPU Memory Output devices Input devices Storage devices Computer Structure - Hardware
Hardware Computer Structure - Software Software Operating system • Gives services to software: • Access input/output devices • Memory allocations • And much more…
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
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…
Command Window Command line • Matlab as a calculator: • Lets run our first command: • 1 + 1
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
Workspace Window Workspace Window All workspace variables Variable value Demo: matlab(‘demo’) http://www.mathworks.com/support/2009b/matlab/7.9/demos/WorkingInTheDevelopmentEnvironment.html
Command History Window Command History Window History Try drag the command to the command window
Lets Try Matlab as a Calculator • I want to make a fruit salad for 10 people. • What is the total num of fruits needed? • How much will it cost to make the salad? • Answer:
Lets Try Matlab as a Calculator II Evaluation Operator evaluation priorities are mathematical (-1+3+2*pi)^(3/4) Inf NaN 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()
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! • Search for “elementary math functions reference” and find the mathematical function reference list • How do I calculate “square root” ? Search... • What does the function “factor” do? • What are the differences between “ceil” “floor”, “fix” and “round”? Try to run “Fix” – what happened?
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” …
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
1 2 Changing The Current Directory 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
Editor / Debugger Window • Open the Editor: Desktop menu -> Editor You can dock the window disp(‘hello world’); • F5 / run button
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.
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)
Handling Errors • Errors - Examples: dis(‘error’) , disp(‘error): • Syntax error in a command • Syntax error / Runtime error in a script
Desktop Layout • Layout – the arrangement of the windows on the desktop Any window on the upper right corner
Standard Toolbar • Help • Window • Desktop • Edit • File More on desktop : http://www.mathworks.com/support/2007a/matlab/7.4/demos/desktop.html
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
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 holdingthe 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