CSE1121 Chapter2
CSE1121 Chapter2
Scientific Programming
ENGR 101
Introduction to Programming
Introduction to MATLAB, Variables
Command Window
Workspace Window
When you define variables in the
command window, they are listed
in the workspace window
Scalar
Vector
2-D
Matrix
Commands for managing the work
session
who: lists the names of defined variables
whos: lists the names and sizes of defined variables
iskeyword
ans =
'break' 'global'
'case' 'if'
'catch' 'otherwise'
'classdef' 'parfor'
'continue' 'persistent'
'else' 'return‘
'elseif' ‘spmd’
'end‘ 'switch'
'for‘ 'try'
'function' 'while'
• test
• Test
• if x
• my-book x
• my_book
• Thisisoneverylongnamebutisitstillallowed? x
• 1stgroup x
• group_one
• zzaAbc
• z34wAwy?12# x
• sin bad
• log idea >>which sin
Special variables and constants
Variables and Assignment Statements
Example:
>> mterm = 60
>> final=80;
>> quiz=60
>> grade = 0.5*mterm+0.1*quiz+0.4*final
• Variables: mterm,final,quiz,grade
• Results displayed and stored by variable name
• Semicolon at the end of a line (as in the line
>> final=80;) tells Matlab to evaluate the line
but not to display the results
Variables and Assignment Statements
Examples more…
>> x=5;
>> y=x+5;
>> x=2;
y is still 10 not 7 you have to run >> y=x+5 again to obtain
new value of y.
>> z=z+2 is a correct assignment in MATLAB.
If you have not supplied a value for xx, Matlab will return a
syntax error:
>> y=xx+5
??? Undefined function or variable ’xx’.
a\b
Operators
Try following operations:
>> 3 + 5 + 2
>> 4*22 + 6*48 + 2*82
>> 4 * 4 * 4
>> 4^3
>> 56/8
>> 8\56
Numbers
Matlab represents numbers in two form, fixed point and floating
point.
Fixed point: Decimal form, with an optional decimal point.
For example: 2.6349 -381 0.00023
Floating point: Scientific notation, representing m x 10e
For example: 2.6349 x 105 is represented as 2.6349e5
The number has two parts:
mantissa m: fixed point number (signed or unsigned), with an
optional decimal point (2.6349 in the example above)
exponent e: an integer exponent (signed or unsigned) (5 in
the example).
Mantissa and exponent must be separated by the letter e (or
E).
Precedence of operations (order of evaluation)
• Use only ( )
• { } and [ ] mean something different
• MATLAB does not assume operators
•
Variables and Assignment
Statements
Examples more…
TC=5/9*(TF-32)
>> a =2;
>> b =10;
>> c =12;
>> disc = b^2-4*a*c;
>> x1 = (-b + sqrt(disc)) /(2*a)
>> x2 = (-b - sqrt(disc)) /(2*a)
x1 =
-2
x2 =
-3
Array Operations
• Using MATLAB as a glorified calculator is fine, but its real
strength is in matrix manipulations.
Matrices in MATLAB
The basic data type
• Group of numbers arranged into rows and columns
• Single Value (Scalar)
• Matrix with one row and one column
• Vector (One dimensional matrix)
• One row or one column
• Matrix (Two dimensional)
To create a row vector, enclose a list of values
in brackets
You may use either a space or a
comma as a “delimiter” in a row
vector
Use a semicolon as a delimiter to create a
new row
Use a semicolon as a delimiter to create a
new row
Hint: It’s easier to keep track of how many
values you’ve entered into a matrix, if you
enter each row on a separate line. The
semicolons are optional
Shortcuts to setup matrices
b= 1:5
or the command
b = [1:5]
• linspace()
• logspace()
number of elements in
the array
Initial value in the
Final value in the
array
array
Example:
>> x = [1 2 3];
>> y = [x ; 4 5 6]
y=
123
456
>> z = [7 4 2 x]
z=
742123
Mixed calculations between scalars and
arrays
n
C ij= ∑ A ik ∗B kj i=1 ,... , m j=1 ,..., q
k =1
Array Operations
• Array multiplication .*
• Array division ./
• Array exponentiation .^
NOTATION
“dot”
a11 b11 a12 b12 a13 b13
A B a21 b21 a22 b22 a23 b23 M
a b a33 b33
“multiply”
31 31 a32 b32
N
Array Operations
Examples: Multiplication & Division (element by element)
1 2 3 1 2 3
A 4 5 6 B 4 5 6
7 8 9 7 8 9
1 4 9 1 1 1
A B 16 25 36
A / B 1 1 1
49 64 81
1 1 1
Example
Newton
Pound
Transpose Operator (‘)
• The transpose operator changes rows to columns or
vice versa.
Transpose Operator (‘)
diary
or
diary on
diary My_diary_file
or
diary (‘My_diary_file’)
LOADING
MATLAB automatically saves
to a .mat file
• If you want to save to another format,
such as .dat, you need to explicitly tell
the program
ASCII format is standard
save <file_name> <variable_list> -ascii between computer
platforms
Again – Remember that the only things being saved are the
values stored in the workspace window – not the commands
from the command window
Script M-Files
• If you want to save your work,
(the commands you entered)
you need to create an M-file
• File->New->M-file
• Type your commands in the edit
window that opens
The Save
and Run Icon
Getting Help
help – On-line help, display text at command line help, by
itself, lists all help topics
help topic provides help for the specified topic
help command provides help for the specified command
help help provides information on use of the help command
helpwin – On-line help, separate window for navigation.
helpdesk – Comprehensive hypertext documentation and
troubleshooting
help ops: help about operators and special characters
F1 + topic and help tab from File menu.
lookfor