Download - Introduction to Matlab Tutorial 1
-
8/13/2019 Introduction to Matlab Tutorial 1
1/31
Yuval Hart, Weizmann 2010 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:http://www.weizmann.ac.il/midrasha/courses/MatlabIntro
Tutorial 1:
Introduction to Matlab Environment
http://www.weizmann.ac.il/midrasha/courses/MatlabIntrohttp://www.weizmann.ac.il/midrasha/courses/MatlabIntro -
8/13/2019 Introduction to Matlab Tutorial 1
2/31
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
-
8/13/2019 Introduction to Matlab Tutorial 1
3/31
3
Before we start
Problems
If you have computer problems while using theroom, 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).
-
8/13/2019 Introduction to Matlab Tutorial 1
4/31
4
Before we start Rules of use
Please help us keep the room in goodworking order. Do not allow food or drink to be brought into
the room
Report problems promptlyWhen 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 thebeam 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
-
8/13/2019 Introduction to Matlab Tutorial 1
5/31
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 forscientific work.
-
8/13/2019 Introduction to Matlab Tutorial 1
6/31
6
Installing Matlab
Classroom
Computing supervisor
Novel:
Enter to disk Z (username / password)
Boris 2443
See instructions on the course web page
-
8/13/2019 Introduction to Matlab Tutorial 1
7/317
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!!!
-
8/13/2019 Introduction to Matlab Tutorial 1
8/31
-
8/13/2019 Introduction to Matlab Tutorial 1
9/319
Computer Structure - Software
Hardware
Gives services to
software:
Access input/outputdevices
Memory allocations
And much more
Software
Operating system
-
8/13/2019 Introduction to Matlab Tutorial 1
10/3110
From Thoughts to Program
Humanlanguage
Matlablanguage
Intermediatelanguage
Computerlanguage
compiler
I want to addone and one
1+1 mambo-jumbo 010011010011
OperatingSystem
-
8/13/2019 Introduction to Matlab Tutorial 1
11/3111
Matlab - Background
Matlab (Matrix Laboratory)
An interactive program that is suitable for
running computations, drawing graphs andmuch more (graphical interface).
A programming language for technicalcomputing (scripts).
Lets start
-
8/13/2019 Introduction to Matlab Tutorial 1
12/3112
Command Window
Command line
Matlab as a calculator:
Lets run our first command:1 + 1
-
8/13/2019 Introduction to Matlab Tutorial 1
13/3113
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
-
8/13/2019 Introduction to Matlab Tutorial 1
14/3114
Workspace Window
Workspace Window
All workspace variables
Variable value
Demo:
matlab(demo)
http://www.mathworks.com/support/2009b/matlab/7.9/demos/WorkingInTheDevelopmentEnvironment.html
-
8/13/2019 Introduction to Matlab Tutorial 1
15/3115
Command History Window
Command History Window
History
Try drag the command tothe command window
-
8/13/2019 Introduction to Matlab Tutorial 1
16/3116
Matlab Arithmetic Operators
Plus +
Unary plus +
Minus -
Unary minus -
multiply *
power ^Backslash or left matrix divide \
Slash or right matrix divide /
-
8/13/2019 Introduction to Matlab Tutorial 1
17/3117
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
-
8/13/2019 Introduction to Matlab Tutorial 1
18/3118
Lets Try Matlab as a Calculator
I want to make a fruit salad for10people.
Ingredients for 5 people:
6 oranges5 bananas
3 apples
1 melon
Price list:
orange 1.5
banana 1.2
apple 1
melon 5
What is the total num of fruitsneeded?
How much will it cost to makethe salad?
Answer:
-
8/13/2019 Introduction to Matlab Tutorial 1
19/3119
Lets Try Matlab as a Calculator II2*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
43
231
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
-
8/13/2019 Introduction to Matlab Tutorial 1
20/3120
Using the Help
Help:
Help (help cos);
lookfor
Doc (doc cos);
Search
F1 / right click
Google matlab
Getting started
Matlab contains manymathematical functions.
Matlab is case sensitive!
1. Search forelementarymath functions referenceand find the mathematical
function reference list2. How do I calculate square
root ? Search...
3. What does the function
factor do?4. What are the differences
between ceil floor, fixand round? Try to run
Fix what happened?
-
8/13/2019 Introduction to Matlab Tutorial 1
21/3121
Output Format
Use the format function to control theoutput formatof numeric values
displayed in the Command Window. formatshort (5 digits) , formatLong (15 digits) , formatrat
Pi = 3.1416, 3.141592653589793 , 355/113
formatCompact / formatloose
determines the line spacing in theoutput
Checkout the doc of format
-
8/13/2019 Introduction to Matlab Tutorial 1
22/3122
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 tothe workspace
Current Directory Window
Files
-
8/13/2019 Introduction to Matlab Tutorial 1
23/31
23
Changing The Current Directory
1
23
pwdcurrent path
cdchange directory
./- current directory
../- parent directoryLets change our directory to ourworking directory.
We will need to do so on everytutorial start
-
8/13/2019 Introduction to Matlab Tutorial 1
24/31
24
Editor / Debugger WindowOpen the Editor: Desktop menu -> Editor
F5 / run button
disp(hello world);
You can dock thewindow
-
8/13/2019 Introduction to Matlab Tutorial 1
25/31
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.
-
8/13/2019 Introduction to Matlab Tutorial 1
26/31
26
Editor / Debugger Windowopen 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);
-
8/13/2019 Introduction to Matlab Tutorial 1
27/31
27
Handling Errors Errors - Examples: dis(error) , disp(error):
Syntax error in a command
Syntax error / Runtime error in a script
-
8/13/2019 Introduction to Matlab Tutorial 1
28/31
28
Desktop Layout Layoutthe arrangement of the
windows on the desktop
Any window on the upper right corner
-
8/13/2019 Introduction to Matlab Tutorial 1
29/31
29
Standard Toolbar
Help
Window
Desktop
Edit
File
More on desktop :
http://www.mathworks.com/support/2007a/matlab/7.4/demos/desktop.html
http://www.mathworks.com/support/2007a/matlab/7.4/demos/desktop.htmlhttp://www.mathworks.com/support/2007a/matlab/7.4/demos/desktop.html -
8/13/2019 Introduction to Matlab Tutorial 1
30/31
-
8/13/2019 Introduction to Matlab Tutorial 1
31/31
Submitting Exercisesdocumentationand proper coding
% HW_1 solution % Yuval Hart 03922881
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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 holdingthe students%participationfinal_grades = (grades .* participation_in_class) + 10; % calculate the students final grades
disp(final_grades);
- if (final_grades