Title: Laura Broussard, Ph.D.
1COS 131 Computing for EngineersCh. 2 Getting
Started with MATLAB
- Laura Broussard, Ph.D.
- Professor
2Ch. 2 Getting Started with MATLAB
- Introduction
- Programming Language Background
- Basic Data Manipulation
- The MATLAB User Interface
- Scripts
- Engineering Example
3I. Introduction
- The name MATLAB is a contraction of Matrix
Laboratory. - MATLAB was developed for engineers to create,
manipulate, and visualize matrices - (matrix lots of numbers arranged in a
- rectangular array).
4I. Introduction
- Fundamental components of MATLAB
- Accepts one instruction at a time in text form
and implements the logic of that instruction - (similar to a calculator)
- Large library of modules that provide high-level
- capabilities for processing data.
- Large collection of toolboxes
- (Toolbox separate application program that
provides graphical capabilities - A Graphical User Interface (GUI) that lets users
assemble and implement programs that solve
specific problems.
5I. Introduction
- Advantages over compiled languages
- MATLAB is an interpreted language.
- Problem-solving can be much faster than for
compiled - languages.
- Excels in numerical and especially matrix
calculations - A wide set of toolboxes for graphical
problem-solving - Build your own GUIs with MATLABs GUIDE
- (Graphical User Interface Development
Environment) - Graphic output is high quality
- Can use in professional reports
6I. Introduction
- Compiled programs are better for
- Large computing projects, especially where there
are multiple programmers - Major GUI and graphics-based programs
7Ch. 2 Getting Started with MATLAB
- Introduction
- Programming Language Background
- Basic Data Manipulation
- The MATLAB User Interface
- Scripts
- Engineering Example
8II. Programming Language Background
- Abstraction
- Algorithms
- Programming Paradigms
9II. Programming Language Background
- Abstraction
- Abstraction expressing a quality apart from
a particular implementation. - Example Exacto, maroon, Standup stapler
- or stapler
10Programming Language Background
- Abstraction
- Two types for computer programming
- data abstraction
- To convert from degrees Celsius to Kelvin, you
add 273 to the temperature. - Specific is 33C, abstract is temperature
- 2. procedural abstraction
- He drove home from the office.
- Specific is turn left, drive 3 blocks, turn
right, - Abstract is drive home
11II. Programming Language Background
- Algorithms
- Algorithm A sequence of instructions for
solving a problem. - Algorithms vary in level of abstraction.
- Baking cookies instructions
- For your grandmother high level of abstraction
- For a beginner low level
- Computer program problems Start with abstract
algorithms,
12II. Programming Language Background
- Algorithms
- Algorithms abstraction in problem-solving
- Method Divide entire problem into subproblems.
- Solutions to subproblems are algorithms.
- Process of solving and assembling subproblems
into - solution for entire problem is also an alogorithm
of higher - abstraction.
- Process Start with big picture and go down
- Initial attempt at entire problem - high level of
abstraction. - Successive attempts for subprograms at lower and
lower levels of abstraction until they work.
13II. Programming Language Background
- Programming Paradigms
- paradeigma to show alongside (Greek)
- Everyday meaning
- a set of assumptions, concepts, values, and
practices that constitutes a way of viewing
reality for the community that shares them,
especially in an intellectual discipline - Computer Programming
- a codified set of practices allowing the
community of computer professionals to frame
their ideas.
14II. Programming Language Background
- Programming Paradigms
- Three different types of paradigms
- Functional Programming
- Procedural Programming
- Object Oriented Programming or OOP
- Side effect - when a function returns a result
and also changes other objects, it has side
effects - Useful, but problematic
- Computer paradigms allow to different degrees
15II. Programming Language Background
- Programming Paradigms
- Functional Programming
- Every programming operation is actually
implemented as a function call without side
effects (program surroundings do not change by
the function call) - Can prove solution to be mathematically correct
- Of some value in theoretical computer science
- Languages Lisp and Forth
16II. Programming Language Background
- Programming Paradigms
- Procedural Programming
- Programs are sequences of operations on data
items that are generally accessible to all
programs. - Side effects are possible but considered bad
programming practice - Languages FORTRAN, C, and MATLAB
17II. Programming Language Background
- Programming Paradigms
- Object-Oriented Programming or OOP
- Objects (data, variables) packaged together with
the methods or functions that manipulate those
data items ? everything is an object - Side effects explicitly managed
- MATLAB exhibits OOP-like traits, but you wont
need to use them! - Languages C, Ada, and Java
18Ch. 2 Getting Started with MATLAB
- Programming Language Background
- Basic Data Manipulation
- The MATLAB User Interface
- Scripts
- Engineering Example
19III. Basic Data Manipulation
- Starting and Stopping MATLAB
- Assigning Values to Variables
- Data Typing
- Classes and Objects
20Basic Data Manipulation
- Starting and Stopping MATLAB
- Practice Exercise 2.1 (p. 21b in text)
- Starting and stopping MATLAB
- Macintosh
- Start Click icon on Dock
- End
- Menu MATLAB/Quit
- Click red circle Upper-Left
- Type quit or exit after prompt in Command
Window - (next slide)
- PCs?
21III. Basic Data Manipulation
Note the double cursor to the left side of the
command window
MATLAB Screen
22III. Basic Data Manipulation
- Definition of Variable
- A representative for a number (algebra)
- A symbolic name associated with a value and
- whose value may be changed (programming)
- Significantly different meanings
- ? be careful!
23III. Basic Data Manipulation
- Assigning values to Variables
- Point of confusion MATLABs syntax for assigning
values to variables looks like an algebra
equation. Its not! - Example z x y
- In programming this means take the values stored
in x and in y, sum them, and place them in the
memory location defined as z. - If x or y has not been assigned a value, get an
error. - In algebra, if z and y are known, can solve for x.
24III. Basic Data Manipulation
- Assigning values to Variables
- Example z x y
- Note True only for this statement.
- Programmer might change this in the next
instruction z 4x y - Some programs (Pascal and Ada) use the
- to denote the difference
25Basic Data Manipulation
- Assigning values to Variables
- Practice Exercise 2.2 (p. 23t in text)
- Assigning Variables
- Set radius equal to 49
- Retrieve radius. Default variable is ans.
- Terminate lines by pressing
Enter
26III. Basic Data Manipulation
- Assigning Values to Variables
-
- Variable names may contain any combination
of - uppercase and lowercase letters
- numbers
- special characters _ (underscore) and
(dollar sign) - Underscore represents a space (not
allowed). - Be consistent (_ or , not a mix)
-
- May be really long (100s of characters!)
- First 64 characters must be unique
- Hyphens not allowed file-size (no)
file_size (yes)
27III. Basic Data Manipulation
- Assigning Values to Variables
- Style Points
- Choose names that describe variables content
- Example for the velocity of an object, not just
v - velocity_in_feet_per_second
- or
- VelocityInFeetPerSecond
28III. Basic Data Manipulation
- Data Typing
- How does MATLAB treat the data stored in a
variable? - Computer languages may be categorized as
- Untyped
- Typed
- Interpreted or untyped languages determine type
of data in a variable by the data that is there. - Typed languages require user to declare the data
type.
29III. Basic Data Manipulation
- Data Typing
- Data types
- char (assign by single quotes around
characters) - numeric (numbers)
- MATLAB may handle different data types the same
way
30III. Basic Data Manipulation
- Data Typing
- Exer 2.3 Performing basic mathematical
operations
- gtgt radius 49
- radius
- 49
- gtgt radius 1
- ans
- 50
- gtgt radius 'radius of a circle
- radius
- radius of a circle
- gtgt radius 1
- ans
- Columns 1 through 12
- 115 98 101 106
MATLAB allows radius to be both types Initial
radius is a numeric type. Second radius is a
char type. radius 1 depends on the type of
data radius is!
Good or bad? Good Can assign a variable type
without advance preparation. Bad A typo can
make a new variable Other program runtime
errors
31III. Basic Data Manipulation
- Data Typing
- Typed languages require the programmer to declare
the name and data type of a variable - Compiler controls the appropriate use of each
variable - Weak typing programmer uses only the normal
data types - Strong typing programmer defines specific data
types with a limited set of permissible
interactions
32III. Basic Data Manipulation
- Classes and Objects
- Variables have two attributes
- Value - determined by what is assigned
- Data type (class) - the type of data
stored - Example myShoeSize 9.5
- MATLAB considers
- the value contained in myShoeSize to be 9.5
- its class to be double (the default numeric
type). - A value of data is also called an object.
33Ch. 2 Getting Started with MATLAB
- Introduction
- Programming Language Background
- Basic Data Manipulation
- The MATLAB User Interface
- Scripts
- Engineering Example
34IV. The MATLAB User Interface
- MATLAB Interface Basics
- Several display windows are visible
- Default view
- Left side Current Folder
- Middle Command Window
- Right Workspace and History Windows (stacked)
- Other windows (Editor, Graphing) open as needed
35IV. The MATLAB User Interface
Close icon
Current directory
Workspace Window
Command window
Macintosh Display
Command history
36V. The MATLAB User Interface
- MATLAB can be used in two modes
- Command mode when you need immediate responses
to specific MATLAB commands - Commands not saved permanently
- Edit mode where you develop practical solutions
to real problems - Create and execute a text file of commands
37V. The MATLAB User Interface
- Command Window
- Exercise 2.4 Using the Command window
- Smith text, page 29, top
38V. The MATLAB User Interface
- Command History Window
- Records the commands you issued in the command
window in chronological order - Retains list of commands when Command Window is
cleared (clc) - Window retains a list of commands from previous
MATLAB sessions - Clear by menu Edit / Clear Command History
39V. The MATLAB User Interface
- Command History Window
- Exercise 2.5 Using the Command History Window
- Smith text, p. 28t
40V. The MATLAB User Interface
- Workspace Window
- Keeps track of the variables you use
- Columns display information about variables
- the name of the variable
- the current value
- class (data type) with an icon and entry
- others information can be selected
41V. The MATLAB User Interface
- Workspace Window and Variables
- Exercise 2.6 Showing more details in the
workspace window, p. 28b - Exercise 2.7 Defining other variables, p. 29m
- Exercise 2.8 Creating a vector, p. 29b
42V. The MATLAB User Interface
- Workspace Window and Variables
- Notes on vectors and matrices
- Semicolons are used to separate rows
- Can recall the values for any variable by just
typing in the variable name - If you suppress the workspace window, you can
still find out what variables have been defined
by using the who and whos commands. - who lists the variable names
- whos lists the variable names together with
their size and class
43V. The MATLAB User Interface
- Workspace Window and Variables
- Exercise 2.9 Creating a 3 x 4 matrix, p. 30b
- Exercise 2.10 Recalling values for variables,
p31t - Exercise 2.11 Using the who and whos command, p.
33b
44V. The MATLAB User Interface
- Current Directory Window
- This window gives the current directory path
where MATLAB is retrieving and storing your files
Note how unintuitive the Current directory
window is. Your eye skips right over it.
45V. The MATLAB User Interface
- Variable Editor
- Double-clicking on any variable in the Workspace
window automatically launches a document window
containing the Array editor. - Array editor allows you to enter new data or
change existing data - A semicolon at the end of data entry to a
variable suppresses the display of those values
in the command window.
46V. The MATLAB User Interface
- Variable Editor
- Exercise 2.12 Creating a two-dimensional
matrix, - p. 33t
47V. The MATLAB User Interface
- Graphics Window
- Created automatically when a MATLAB command
requests a graph. - Additional graphics requests will overwrite the
contents of the current graphic window unless you
request MATLAB to open a new Graphics window
48V. The MATLAB User Interface
- Graphics Window
- Created automatically when a MATLAB command
requests a graph. - Additional graphics requests will overwrite the
contents of the current graphic window unless you
request MATLAB to open a new Graphics window - Exercise 2.13 Creating a graph, p. 33b
49The MATLAB User Interface
50V. The MATLAB User Interface
- Editor Window
- The text editor for MATLAB
- Create or modify text files here
- Opened by choosing File gt New gt M-File
- Lets you type and save a series of commands
without executing them - Also open the editor window by double-clicking a
file name in the current directory window or by
typing - gtgt edit ltfile_namegt in the command window
51Ch. 2 Getting Started with MATLAB
- Introduction
- Programming Language Background
- Basic Data Manipulation
- The MATLAB User Interface
- Scripts
- Engineering Example
52VI. Scripts
- Text files
- Section describes the basic mechanism for
creating, saving, and executing scripts
(programs) as m-files - Use text files as a permanent means of saving
scripts - A script is like writing an email message lines
of text written in a smart editor
53VI. Scripts
- Creating Scripts
- A combination of executable instructions
interpreted by MATLAB and comment statements that
document the script help readers understand
what the program is doing - Comments are created by placing a percent symbol,
, at the beginning of any text you desire to
be a comment good through the end of the line in
which it was created comment lines are green in
editor window - Uses the extension .m for script files
- Exercise 2.14 Creating a script, p. 37b
54VI. Scripts
See line by line description of this simple
MATLAB program on page 36 of the Smith text.
55VI. Scripts
- The Current Directory
- Must name and save the script in a directory
where MATLAB can find it - MATLAB expects a path like c\MATLABxxx\work
- You can provide MATLAB with a different directory
to use in the current directory window you will
then need to use the browse button pointed out
earlier to retrieve the m-file - Exercise 2.15 Saving a script, p. 39t
56VI. Scripts
- Running Scripts
- Can run a script using any of the following
methods - Type the name of the script in the command window
- Choose the Debug gt Run menu item in the MATLAB
editor window - Press the F5 key when the script is visible in
the editor. This automatically saves the script
before executing it. - Exercise 2.16 Running a script, p39b
57VI. Scripts
- Debugging Scripts
- Uses breakpoints places in your program where
you want to verify your code is doing what you
want it to - Click the small dash between the line number and
the start of the text line in the Editor window - Can examine the content of variables by passing
the mouse slowly over the variable in the
Workspace window or variable in the Editor window - Watch for inadvertently typed semicolons does
weird things to your programs! - Exercise 2.17 Debugging a script, p. 40b
58Ch. 2 Getting Started with MATLAB
- Introduction
- Programming Language Background
- Basic Data Manipulation
- The MATLAB User Interface
- Scripts
- Engineering Example
59VI. Engineering Example Spacecraft Launch
- Problem Assuming that the spacecraft uses all
its fuel to achieve a vertical velocity u at
25,000 feet, what is the value of u for the
spacecraft to reach outer space?
60VI. Engineering Example Spacecraft Launch
- Solution
- Two parts to this problem
- Converting units to the metric system
- Choosing and solving an equation for motion under
constant acceleration
61VI. Engineering Example Spacecraft Launch
- Part 1 converting units to the metric system
- 1 inch 2.54 cm
- We have
62VI. Engineering Example Spacecraft Launch
- MATLAB code for converting units to the metric
system
Note line by line description of code on pages
41-42 of text.
63VI. Engineering Example Spacecraft Launch
- MATLAB code for solving for the equation
- Need the following
- Initial and final altitudes from which you can
compute the distance traveled s - Motion is under constant acceleration, the force
of gravity, g - Just to reach outer space, the final velocity, v,
is 0 - Initial velocity, u, is needed
64VI. Engineering Example Spacecraft Launch
- MATLAB code for solving for the equation
- Equation for motion under constant acceleration
connecting u, v, s, and a is
65VI. Engineering Example Spacecraft Launch
- Complete MATLAB code for solving for the equation
Note line by line description of code on pages
41-43 of text.