Matlab Notes
Matlab Notes
Matlab Notes
dr.dcd.h CS 101 /SJC 5th Edition 1 dr.dcd.h CS 101 /SJC 5th Edition 2
dr.dcd.h CS 101 /SJC 5th Edition 3 dr.dcd.h CS 101 /SJC 5th Edition 4
1
MATLAB Environment The MATLAB Desktop
In Windows or Apple operating systems MATLAB uses several different windows to
click on the desktop icon or menu item display data, commands and results.
- comand window
- toolstrip
- command history window
- document window
- figure window
In Unix/MSDOS type MATLAB at the
- workspace browser
shell prompt - current folder browser
- help browser
- path browser
dr.dcd.h CS 101 /SJC 5th Edition 5 dr.dcd.h CS 101 /SJC 5th Edition 6
Command Window
dr.dcd.h CS 101 /SJC 5th Edition 7 dr.dcd.h CS 101 /SJC 5th Edition 8
2
The Command Window2 The Toolstrip
A user can enter interactive commands at The controls on the Toolstrip are organized
the command prompt (>>) in the into related categories of functions, first by
command window. tabs and then by goups.
A user can place a series of commands into The Quick Access Toolbar is a place where
a script file, and the entire file can be the user can customize the interface.
executed by typing its name in the
command window.
MATLAB script files are also known as M-
files, because they have a file extension of
“.m”.
dr.dcd.h CS 101 /SJC 5th Edition 9 dr.dcd.h CS 101 /SJC 5th Edition 10
dr.dcd.h CS 101 /SJC 5th Edition 11 dr.dcd.h CS 101 /SJC 5th Edition 12
3
The Document Window The Document Window2
It is also called Edit/Debug Window.
An Edit Window is created when you create
a new M-file or open an existing one.
There are several ways to open an editing
window:
Click the New Script icon
Click the Ne w icon and then select Script
Type edit w/ or w/o a name from the command
window the n hit return
dr.dcd.h CS 101 /SJC 5th Edition 13 dr.dcd.h CS 101 /SJC 5th Edition 14
dr.dcd.h CS 101 /SJC 5th Edition 15 dr.dcd.h CS 101 /SJC 5th Edition 16
4
Figure Window2 Docking & Undocking Windows
When a window is docked, it appears as a
pane within the MATLAB desktop.
dr.dcd.h CS 101 /SJC 5th Edition 17 dr.dcd.h CS 101 /SJC 5th Edition 18
dr.dcd.h CS 101 /SJC 5th Edition 19 dr.dcd.h CS 101 /SJC 5th Edition 20
5
The MATLAB Workspace2 The MATLAB Workspace3
The who and whos commands. Use clear to delete a variable.
Syntax: clear [variable-list]
dr.dcd.h CS 101 /SJC 5th Edition 21 dr.dcd.h CS 101 /SJC 5th Edition 22
dr.dcd.h CS 101 /SJC 5th Edition 23 dr.dcd.h CS 101 /SJC 5th Edition 24
6
Array Editor3 Current Folder
Also called the current directory.
It shows all the files in the selected folder.
dr.dcd.h CS 101 /SJC 5th Edition 25 dr.dcd.h CS 101 /SJC 5th Edition 26
dr.dcd.h CS 101 /SJC 5th Edition 27 dr.dcd.h CS 101 /SJC 5th Edition 28
7
Getting Help2 Getting Help3
To show the usage of a known command, To search a command by keyword, using
using the help command. the lookfor command.
dr.dcd.h CS 101 /SJC 5th Edition 29 dr.dcd.h CS 101 /SJC 5th Edition 30
dr.dcd.h CS 101 /SJC 5th Edition 31 dr.dcd.h CS 101 /SJC 5th Edition 32
8
Useful Commands3 MATLAB Search Path
To find out where a command is located, If a name is typed at the system prompt,
using the which command. the MATLAB interpreter attempts to find the
command as follows:
C he cks as a variable in the workspace.
Che cks as an M-file in the current folder.
Che cks to see if it is an M-file in any directory
along the MATLAB search path.
dr.dcd.h CS 101 /SJC 5th Edition 33 dr.dcd.h CS 101 /SJC 5th Edition 34
dr.dcd.h CS 101 /SJC 5th Edition 35 dr.dcd.h CS 101 /SJC 5th Edition 36
9
Homework Assignment #1 How to Make Homework
1.6 Exercises Make sure your command window is under
Page 24: 1.1, 1.3, 1.4, 1.8, 1.9, 10 format compact;
This assignment is due by next week. Enter the homework questions line-by-line;
Late submission will be penalized. Copy and paste the outputs including the
Hand-written homework will not be commands into a word document and
accepted or graded. select the minimum spacing;
Hand in a hardcopy of your homework, no Save figures into image files and then
electronic submission will be accepted. insert them into your document. You may
need to change their sizes;
Print your homework to a nearby printer.
dr.dcd.h CS 101 /SJC 5th Edition 37 dr.dcd.h CS 101 /SJC 5th Edition 38
dr.dcd.h CS 101 /SJC 5th Edition 39 dr.dcd.h CS 101 /SJC 5th Edition 40
10
How to Make Homework4
11
9/4/2018
dr.dcd.h CS 101 /SJC 5th Edition 1 dr.dcd.h CS 101 /SJC 5th Edition 2
Arrays2 Arrays3
A single value is called a scalar The size of an array is specified by the
Matrix with only one row and one column number of rows and the number of
This is a 4x6 matrix, containing 24 columns.
elements. [ ] creates an empty array.
3 0 0 4 4 0
0 3 0 4 4 0
Data in an array are listed in row order,
1 1 3 4 4 1 i.e. the items in each row are listed from
0 0 1 1 1 0 left to right, with the topmost row first.
Syntax: var(row, col)
[1, 2, 3, 4] is a 1x4 array, known as a row
vector.
A 4x1 array is called a column vector.
dr.dcd.h CS 101 /SJC 5th Edition 3 dr.dcd.h CS 101 /SJC 5th Edition 4
Variables Variables2
A MATLAB variable is a region of memory There are certain keywords you can’t use
containing an array, which is known by a Use iskeyword function to check
user-specified name. whether a name is a system reserved
Variable names must start with a letter. name.
They may contain letters, numbers and the
underscore ( _ ).
Names are case sensitive.
The effective length of a name is 63
characters.
dr.dcd.h CS 101 /SJC 5th Edition 5 dr.dcd.h CS 101 /SJC 5th Edition 6
1
9/4/2018
Variables3 Variables4
MATLAB will let you use built-in function Use command clear (check Chap 1)
names as variables – but it’s a really bad Then how to clear an accidently created
idea. variable named as clear?
What if you accidently overwritten a
function to a variable name?
dr.dcd.h CS 101 /SJC 5th Edition 7 dr.dcd.h CS 101 /SJC 5th Edition 8
dr.dcd.h CS 101 /SJC 5th Edition 9 dr.dcd.h CS 101 /SJC 5th Edition 10
dr.dcd.h CS 101 /SJC 5th Edition 11 dr.dcd.h CS 101 /SJC 5th Edition 12
2
9/4/2018
dr.dcd.h CS 101 /SJC 5th Edition 13 dr.dcd.h CS 101 /SJC 5th Edition 14
dr.dcd.h CS 101 /SJC 5th Edition 15 dr.dcd.h CS 101 /SJC 5th Edition 16
[degrees;radians]’ would
have given the same result
dr.dcd.h CS 101 /SJC 5th Edition 17 dr.dcd.h CS 101 /SJC 5th Edition 18
3
9/4/2018
dr.dcd.h CS 101 /SJC 5th Edition 19 dr.dcd.h CS 101 /SJC 5th Edition 20
dr.dcd.h CS 101 /SJC 5th Edition 21 dr.dcd.h CS 101 /SJC 5th Edition 22
dr.dcd.h CS 101 /SJC 5th Edition 23 dr.dcd.h CS 101 /SJC 5th Edition 24
4
9/4/2018
dr.dcd.h CS 101 /SJC 5th Edition 25 dr.dcd.h CS 101 /SJC 5th Edition 26
dr.dcd.h CS 101 /SJC 5th Edition 27 dr.dcd.h CS 101 /SJC 5th Edition 28
dr.dcd.h CS 101 /SJC 5th Edition 29 dr.dcd.h CS 101 /SJC 5th Edition 30
5
9/4/2018
dr.dcd.h CS 101 /SJC 5th Edition 31 dr.dcd.h CS 101 /SJC 5th Edition 32
dr.dcd.h CS 101 /SJC 5th Edition 33 dr.dcd.h CS 101 /SJC 5th Edition 34
dr.dcd.h CS 101 /SJC 5th Edition 35 dr.dcd.h CS 101 /SJC 5th Edition 36
6
9/4/2018
dr.dcd.h CS 101 /SJC 5th Edition 37 dr.dcd.h CS 101 /SJC 5th Edition 38
7 9 11
8 10 12
1 3 5
2 4 6
dr.dcd.h CS 101 /SJC 5th Edition 39 dr.dcd.h CS 101 /SJC 5th Edition 40
A(1:2:3, :)=[1 2 3; 7 8 9]
dr.dcd.h CS 101 /SJC 5th Edition 41 dr.dcd.h CS 101 /SJC 5th Edition 42
7
9/4/2018
dr.dcd.h CS 101 /SJC 5th Edition 43 dr.dcd.h CS 101 /SJC 5th Edition 44
dr.dcd.h CS 101 /SJC 5th Edition 45 dr.dcd.h CS 101 /SJC 5th Edition 46
8
9/4/2018
dr.dcd.h CS 101 /SJC 5th Edition 1 dr.dcd.h CS 101 /SJC 5th Edition 2
dr.dcd.h CS 101 /SJC 5th Edition 3 dr.dcd.h CS 101 /SJC 5th Edition 4
dr.dcd.h CS 101 /SJC 5th Edition 5 dr.dcd.h CS 101 /SJC 5th Edition 6
1
9/4/2018
dr.dcd.h CS 101 /SJC 5th Edition 7 dr.dcd.h CS 101 /SJC 5th Edition 8
dr.dcd.h CS 101 /SJC 5th Edition 9 dr.dcd.h CS 101 /SJC 5th Edition 10
dr.dcd.h CS 101 /SJC 5th Edition 11 dr.dcd.h CS 101 /SJC 5th Edition 12
2
9/4/2018
dr.dcd.h CS 101 /SJC 5th Edition 13 dr.dcd.h CS 101 /SJC 5th Edition 14
ASCII ASCII2
ASCII stands for American Standard Code The ASCII chart:
for Information Interchange. ASCII encodes
128 characters into 7-bits. They are digits 0
to 9, letters a to z and A to Z,
punctuations, control codes, and a space.
dr.dcd.h CS 101 /SJC 5th Edition 15 dr.dcd.h CS 101 /SJC 5th Edition 16
dr.dcd.h CS 101 /SJC 5th Edition 17 dr.dcd.h CS 101 /SJC 5th Edition 18
3
9/4/2018
dr.dcd.h CS 101 /SJC 5th Edition 19 dr.dcd.h CS 101 /SJC 5th Edition 20
dr.dcd.h CS 101 /SJC 5th Edition 21 dr.dcd.h CS 101 /SJC 5th Edition 22
Note:
b\a is called the left division.
Parentheses may be used to group terms and the sub-
expressions inside the parentheses are evaluated first.
dr.dcd.h CS 101 /SJC 5th Edition 23 dr.dcd.h CS 101 /SJC 5th Edition 24
4
9/4/2018
dr.dcd.h CS 101 /SJC 5th Edition 25 dr.dcd.h CS 101 /SJC 5th Edition 26
Note:
a^b^c is not evaluated as but (a^b)^c.
dr.dcd.h CS 101 /SJC 5th Edition 27 dr.dcd.h CS 101 /SJC 5th Edition 28
X = A-1B =
dr.dcd.h CS 101 /SJC 5th Edition 29 dr.dcd.h CS 101 /SJC 5th Edition 30
5
9/4/2018
dr.dcd.h CS 101 /SJC 5th Edition 31 dr.dcd.h CS 101 /SJC 5th Edition 32
dr.dcd.h CS 101 /SJC 5th Edition 33 dr.dcd.h CS 101 /SJC 5th Edition 34
dr.dcd.h CS 101 /SJC 5th Edition 35 dr.dcd.h CS 101 /SJC 5th Edition 36
6
9/4/2018
dr.dcd.h CS 101 /SJC 5th Edition 37 dr.dcd.h CS 101 /SJC 5th Edition 38
dr.dcd.h CS 101 /SJC 5th Edition 39 dr.dcd.h CS 101 /SJC 5th Edition 40
dr.dcd.h CS 101 /SJC 5th Edition 41 dr.dcd.h CS 101 /SJC 5th Edition 42
7
9/4/2018
dr.dcd.h CS 101 /SJC 5th Edition 43 dr.dcd.h CS 101 /SJC 5th Edition 44
dr.dcd.h CS 101 /SJC 5th Edition 45 dr.dcd.h CS 101 /SJC 5th Edition 46
dr.dcd.h CS 101 /SJC 5th Edition 47 dr.dcd.h CS 101 /SJC 5th Edition 48
8
9/4/2018
dr.dcd.h CS 101 /SJC 5th Edition 49 dr.dcd.h CS 101 /SJC 5th Edition 50
dr.dcd.h CS 101 /SJC 5th Edition 51 dr.dcd.h CS 101 /SJC 5th Edition 52
dr.dcd.h CS 101 /SJC 5th Edition 53 dr.dcd.h CS 101 /SJC 5th Edition 54
9
9/4/2018
dr.dcd.h CS 101 /SJC 5th Edition 55 dr.dcd.h CS 101 /SJC 5th Edition 56
Homework Assignment #5
2.15 Exercises
Page 83: 2.1 - 2.8
This assignment is due by next week.
Late submission will be penalized.
10
Chapter 3
Two-Dimensional Plots
\beta b \cong
\gamma g \Gamma G \sim
\delta d \Delta D \infty
\epsilon e \pm
\eta h \leq
\theta q \geq
\lambda l \Lambda L \neq
\mu m \propto
\nu n \div
\pi p \Pi P \circ
\phi f \leftrightarrow
\rho r \leftarrow
\sigma s \Sigma S \rightarrow
\tau t \uparrow
\omega w \Omega W \downarrow
dr.dcd.h CS 101 /SJC 5th Edition 21
Enhanced Text Strings3
For example: the following expression
'\fontsize{18}\color{blue}\int_{\Omega}
\color{green}\int_{\Gamma}
\color{black}(\nu^{2}+3\nu\mu)
\color{red}d\nud\mu'
Produces the following text:
2 G11
G1
G12
1 3
G2
G 2 G31
G3 G32
4
3
G4
Define required
inputs and outputs Decomposition
End
Unit testing of
individual subtasks
Repeat as many
Alpha release times as needed
Repeat as many
Beta release times as needed
Notice:
== is a comparison operator.
= is an assignment operator.
For example:
5>3 == 5>3 produces 0
5>3 == 5<3 produces 1
For all the data shown above only characters are not
considered as numeric.
A: 94 < grade
B: 74 < grade < 95
C: 64 < grade < 75
D: 44 < grade < 65
F: 0 < grade < 45
if ctrl ==case1
statements_block1
elseif ctrl ==case2
statements_block2
…
else
statements_block3
end
(4.3)
R is 8314 L kPa/mol K
At T=273 K, n=1 mole, estimate V as P varies
from 1 to 1000 kPa and plot P versus V.
controlling fals
expression e
true
Statements
Update
true
Statements
Index++
a = cos(x)