BIL 108E Introduction to Scientific and Engineering Com puting

•
•
•
2
•
!
#
"
3
4
5
!
$
"
$"
%
"
&
%
$
'
%
$
6
"
# $
%
Interactive commands
Executed on the spot
Command Prompt
7
&
'
Displays a list of the commands
Entered in the Command Window
8
&
• #
'"
#
"
• ( "
– ) "$
–#
9
( )
'
*
10
!
'
#
11
*
•
• )
• &
•
+
$"
12
Select the tool from submenu
13
+
•
,#
-
.
,
+# .
•
•
%
/ /
,
"
.
"
$
#
'
"
"
"
"
,00.
14
+
15
+
•
"
$
"
*
00 12
•
"
"
12
"
"
"
12
•
"
16
+
•
"
$
"
00
*3
00 12
4
*3
"
17
,!
•
• !
"
#
'
Tool bar icon
•
• '
5) $"
18
,!
•
•
"
"
#
"
*
–#
–6
–#
–
–7
$
" $
$
$"
19
Matlab Editor
Access to
commands
Color keyed
text with auto
indents
tabbed sheets for other
files being edited
20
,!
"
#
21
,!
"
#
Saving the script: filename.m
22
-
!
Comments!!!
• .
)
'
//
'
#
) #
#) '
# #
)
23
For example, here is some cryptic code
without comment
for j=0:2
k=(2-j)*(1+3*j)/2
end
What does it do?
Put a comment in
% turns (0,1,2) into (1,2,0)
24
•
"
–
–)
–+
–
–)
"
"
8
8
"
25
'
0
• 1
• - )
)
)
$2%
•
' 3/
•
' //
)
4
/
0
/
//
26
(
/
>> algo <Enter>
Don’t write algo.m
27
'
•
0
5
6
•
$
•
•
7
#
0
%
/
)
+
$
%
• 3 74
7
/)
0
28
6
•
"
%
% "
$
92
• + " $
•
%
$
"$ :"
% "
"
2
29
86
•
•
8
3
0$ × %
/
$%
•
*9 × 9
0
• ( # 6
*9 ×
0
•
6
* ×9
A = [5.27]
4
# $ %
0
B = [5.02 − 2.3 7.21]
5.02
C = B' = − 2.3
7.21
1
3 −2
5
D= −2
4 3.2 9.5
− 0.5 − 1 7.2 − 2
30
>> pi
ans =
3.1416
>> size(pi)
ans =
1 1
pi = 3.1416
>> a=[1 2 3; 4 5 6]
a=
1 2 3
4 5 6
>> size(a)
ans =
2 3
a=
1 2 3
4 5 6
31
• ( "
"
"; "
"
Command
Prompt
>> 9 + 10
ans=19
Result
32
:
•
%
$
91<=
"
$
% "
*
1<=
33
& #
)
//
•
$
/
;
'<
:"
92<>
$
•
"
92<>
% "
:"
34
)
• ( ???
•
$
9 <1
•
"
' //
<
"
,
% "
% "
.
:"
<1
35
0
•
• 1
)
0
)
'
'
'
•
)
'
=
» x=5+2*i
x =
5.0000 + 2.0000i
» y=5*x+3
y =
28.0000 +10.0000i
36
MATLAB Example
» x=3+5-0.2
x =
7.8000
» y=3*x^2+5
y =
187.5200
» z=x*sqrt(y)
z =
106.8116
» A=[1 2 3; 4 5 6]
A =
1
2
3
4
5
6
» b=[3;2;5]
b =
3
2
5
» C=A*b
C =
22
52
» who
Your variables are:
A
C
b
x
» whos
Name
A
C
b
x
y
z
y
z
Size
2x3
2x1
3x1
1x1
1x1
1x1
» save
Bytes
48
16
24
8
8
8
Class
double
double
double
double
double
double
array
array
array
array
array
array
default filename
Saving to: matlab.mat
» save matrix1
filename “matrix1”
37
!
'
•
•
0
• .
#)
•
'
5)
/ )
)
'
/
$ /
%
>>>
38
6
• ?
• 6
• 6
A%
• 6
• 1
•
)
'8 )
1
/
'
'
# ) )
/
@9 )
)
8
8
)
)
$
# )
'#
'
5
ABC, A1, C56, CVEN_302
day, year, iteration, max
time, velocity, distance, area, density, pressure
Time, TIME, time (case sensitive!!)
39
?
) B
'#
5/
'#
/
/
40
.
/
•
" " $" $ @ $" ;
%
•
92
"
$
"
%
$
41
") ) /)
#
<
•
•
•
•
•
•
•
•
•
•
•
•
x
-$
x
A$
%
$"
B x
1
x
"
" A
CC $
C2D
B1=8
x x
bad
idea
42
7
•
•
•
•
0
'
/
)
/
6
)
/
$*%
)
'
A = [1; 3; 5; 10];
C = [2 3 5 1; 0 1 …
B = [1 3 5; -6 4 -1]
(continuation)
1 -2; 3 5 1 -3]
E = [A; 1; A];
F = [C(2,3); A]
43
Built-in Variables
pi: π value up to 15 significant digits
i, j: sqrt(-1)
Inf: infinity (such as division by 0)
NaN: Not-a-Number (division of zero by zero)
clock: current date and time in the form of a 6-element
row vector containing the year, month, day, hour,
minute, and second
• date: current date as a string such as 16-Feb-2004
• eps: epsilon is the smallest difference between two
numbers
• ans: stores the result of an expression
•
•
•
•
•
44
"
•
%
$
% "
"
≡
$ "
"
•
•
•
%
$
,
.
$
"
E
45
"
#
46
When you define variables in the command window, they are
listed in the workspace window
Scalar
Vector
2-D Matrix
47
6
"
*
%
@ 00
$
% A
@ 00
@
@
+ %
@ %
6
*
*
6
$
%
%
C
$
A
6
@
$
$
$
A
48
CC# )
D
C
List of variables and arrays in the
current workspace can be
generated with the whos command
49
!
•
"
"
"$
#
%
$
"
'
• ( "
%
$
50
Matlab Array Editor
Direct access
into arrays
Can change
individual values
51
!
'
52
'
/
0@
Allows to modify the
information stored in
the variable
53
Document Window
54
0
• '" $
• F
• 6
$
• !"
55
:
56
/#
/
• +:"
4
•
e
x
• ' "
ln( x)
, .
• )
log10 (x)
1G , .
: ,D .
, .
57
/
•
•
•
,.
,.
,.
• asin(x) arcsine of x
• acos(x) arccosine of x
• atan(x) arctangent of x
58
(
Operator Name
:
Operator Symbol EXAMPLE
less than
<
x<y
less than equal
to
<=
a<=22
equal to
==
x==100
not equal to
~=
x~=10
greater than
equal to
=>
pi=>3
greater than
>
c>100
59
Example:
x=15;
y=20;
ave=(x+y)/2;
testit=(ave>17)
60
/
0
%
'
9
$ *
x tan x + sin x
cos =
2
2 tan x
2
6
:"
"$
"
$
9H5>
"
61
0
/
&
'
/
To=Initial temperature of object at time t=0
Ts=temperature of chamber object is placed into
T=temperature of object at time t
k=constant
Temperature of object rises according to function shown
below
− kt
S
0
S
T = T + (T − T )e
If a soda can (from inside a hot car) at 120F is placed into a
refrigerator with 38F hat is the temperature after 3 hours
(k=0.45)?
62
+
•
•
"
#
! "
;
63
The semicolon suppresses the
output from each command
64
65
.
•
"
%
% "
"
%
%
$
"
%
66
Save either by using the file menu or...
Save with a command in the
command window
67
'
•
"
%
E
/
"
"
save <file_name> <variable_list> -ascii
68
Operation
Save variable m in MATLAB
file named file.mat
Save variable m in file
named file.dat using 8 digit
precision/text format
Save variable m in file
named file.dat using 16 digit
precision/text format
Save variable m in file
named file.dat using 16 digit
precision/text format with
individual elements delimited
by tabs
MATLAB Syntax
save file m
save file.dat m -ascii
save file.dat m -ascii -double
save file.dat m -ascii –double -tabs
•ASCII (text) files can be viewed, modified, or prepared using
programs like WordPad or NotePad in the Windows
environment, or vi in the UNIX environment
•ASCII files are formatted such that each row of a matrix is
contained on a separate line
69
•
•
%
"
"
70
F
&
One-click
71
&
"
#
72
CC)
0
D
C
73
CC
/
)
D
C
74
/
00
4
00
4
%
;
; %
%
%
&
%
"
$
#
"
"
%
75
)
–
–#
%
#
#
$
76
)- )
•
%
"
, -
•
" *
• +
$
.
"
)
I
77
!
- )
78
!
*+
)
79
CC
)D
C
80
CC
)D
C
81
- )
• +
)
I
+
"
#
+ %
• J"
$
"
"
+
I
I
"
"
$
82
!
• + $
"
% "
•
$
* )
,!
"
"
#
"
"
"
$
$
" "
83
Left-click in the breakpoint alley at
the line where you want to set the
breakpoint.
84
After left-click in the breakpoint
alley at the line where you want to
set the breakpoint a red icon will
appear
85
0
• F
)
# )
$
% $
"
" " $
#
86
(
/(
• K"
+
E
#
"
*
700
$"
•
"
$
=L
5) $"
$
$
"
"
M"
87
(
/(
+
Green arrow appears
by the current line
Program run until
breakpoint
88
!
•
$
5
,%
%
$
"
$
$
$
.
#
•
"
$
!1G
•
)
5) $" 5
$"
) $"
"
89
• #)
%
•
•
• /
•
•
•
• )
#)
-- +
-%
---- %
--- # K -#
-N
"
%
"
$
%
O
Really good “help” command
90