Download Chapter 2 (Part 2) MATLAB Basics

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Chapter 2 (Part 2)
MATLAB Basics
Display Format
{
{
{
{
{
dr.dcd.h
CS 101 Spring 2009
1
dr.dcd.h
Display Format2
{
dr.dcd.h
Descriptions
4 digits after decimal
3.1416
long
14 digits after decimal
3.141592653589793
short e
5 digits plus exponent
3.1416e+000
short g
5 digits plus w/ or w/o exponent
3.1416
long e
15 digits plus exponent
3.141592653589793e+000
long g
15 digits plus w/ or w/o exponent
3.14159265358979
bank
dollars and cents format
3.14
hex
4-bit hexadecimal
400921fb54442d18
355/113
rat
approximate ratio of small integers
approximate ratio of small integers
loose
restore extra line feeds
+
only displays signs
CS 101 Spring 2009
{
{
Example
short
compact
CS 101 Spring 2009
2
Output Option: disp
The default format shows four digits after
the decimal point, it is also known as short
format
In the command window, integers are
always displayed as integers
Characters are always displayed as strings
Other values are displayed using a specified
display format
No matter what display format you choose,
MATLAB uses double precision floating
point values in its calculations
The display format can be changed by
using format command
{
The disp function displays the contents of a
matrix without printing its name
The disp function can also be used to
display a string
The general form of disp function
z
{
disp(string)
Supplemental data converting functions
z
z
num2str: convert a number to a string
int2str: convert an integer to a string
+
3
dr.dcd.h
CS 101 Spring 2009
4
Output Option: disp2
Output Option: disp3
{
To include an apostrophe (’) in a string,
you need to enter the apostrophe twice
z
z
{
fact: a string is an array of character
z
z
z
Notice that both b & c
are listed as character
arrays
dr.dcd.h
CS 101 Spring 2009
5
dr.dcd.h
{
{
CS 101 Spring 2009
fprintf gives you more control over your
output than the disp function
You can combine text and numbers in a
specified format
You can control how many digits to display,
and their position
{
CS 101 Spring 2009
6
7
The general form of disp function
z
z
z
{
{
dr.dcd.h
Usage: convert an integer to a string
disp( [’abc = ’ num2str(1.23)] )
Returns ’abc = 1.23’
Output Option: fprintf2
Output Option: fprintf
{
disp( ’I’’m what I am’ )
returns ’I’m what I am’
dr.dcd.h
fprintf(format, data)
format is a string describing the way the data is
to be printed
data is one or more scalars or arrays
The format is a string containing text plus
special conversion characters ’%?’
describing the format of the data
A set of conversion characters can be
considered as a data-holder
CS 101 Spring 2009
8
Output Option: fprintf3
%?
Desired Results
{
%d
display value as an integer
%e
display value in exponential form
%f
display value in floating-point form
%g
Extra Format
Character
dr.dcd.h
display ’+’ sign if data is positive
–
display data in a left-adjusted fashion
m
display data in a field m-digits wide
0
To include a percentage sign in an fprintf
statement, you need to enter the ’%’ twice
CS 101 Spring 2009
{
replaces extra blanks by zeros
fprintf(’%07.2f\n’, 12.345) produces ’0012.35’
dr.dcd.h
Output Option: fprintf5
display data in a field m-digits wide,
.including n-digits after the decimal point
For example:
z
9
Descriptions
+
m.N
line feed, skip to a new line
Notice:
z
{
Advanced formatting characters
display value in either floating-point or
.exponential form, whichever is shorter
\n
{
Output Option: fprintf4
CS 101 Spring 2009
10
Data Files
Advanced formatting characters
{
{
Extra blanks are
replaced by 0’s
The save command saves data from the
current workspace into a disk file.
The general form of save function
z
No line feed is applied
z
Letters are numbers,
eg. ’a’ is encoded as
97
z
{
save <–ascii> filename <var_list>
If no variables are specified, all variables in the
workspace will be saved.
If option –ascii is used, the data will be saved in
an exponential form.
MATLAB automatically saves to a .mat file,
other formats need to explicitly specified.
Left-adjusted layout
dr.dcd.h
CS 101 Spring 2009
11
dr.dcd.h
CS 101 Spring 2009
12
Data Files2
{
{
The load command loads data from a disk
file into the current workspace.
The general form of load function
z
z
{
{
Data Files3
load filename
load –mat filename.dat
If a MAT-file is loaded, all of the variables
will be restored with the names and types.
The contents of an ASCII-file will be
converted into an array having the same
name as the file (w/o the extension).
dr.dcd.h
CS 101 Spring 2009
13
dr.dcd.h
Data Files4
{
{
dr.dcd.h
CS 101 Spring 2009
14
CS 101 Spring 2009
16
Data Files5
When the –ascii option is used, information
such as variable names and types will be
lost. However, the dimensional information
will be kept.
Data having different column sizes should
not be saved together if the –ascii option
will be used.
CS 101 Spring 2009
15
dr.dcd.h
Scalar Arithmetic Operations
Operation
Algebraic Form
MATLAB Form
Addition
a+b
a+b
Subtraction
a–b
a–b
Multiplication
axb
a*b
a
____
Division
b
Exponentiation
{
ab
Precedence of Arithmetic Operations
a / b or b \ a
a^b
dr.dcd.h
17
Perform calculations inside all parentheses,
working from the innermost set to the outermost.
2
Perform exponential operations.
3
Perform multiplication and division operations,
working from left to right.
4
Perform addition and subtraction operations,
working from left to right.
dr.dcd.h
Expressions
z
Operation
2 ^ (8 + 6 / 3) returns 1024
2 ^ 8 + 6 / 3 returns 258
CS 101 Spring 2009
CS 101 Spring 2009
18
Array & Matrix Operations
An expression can be any valid
combination of scalars, arrays,
parentheses, and arithmetic operators.
For example:
z
dr.dcd.h
1
b\a is called the left division, which is equal to a/b
CS 101 Spring 2009
{
Descriptions
Note:
z
{
Precedence
19
dr.dcd.h
Expression
Descriptions
Addition
a+b
Array addition and matrix addition are identical.
Subtraction
a–b
Array subtraction and matrix subtraction are
identical.
Multiplication
a*b
The no. of columns in a must equal the no. of
rows in b.
Division
a/b
In MATLAB, it is defined by a*inv(b), where
inv(b) is the inverse of b.
Array
Multiplication
a .* b
Element-by-element multiplication of a and b :
a(i,j)*b(i,j). Both a and b must be the same
shape.
Array Division
a ./ b
Element-by-element division of a and b :
a(i,j)/b(i,j). Both a and b must be the same
shape.
Array
Exponentiation
a .^ b
Element-by-element exponential of a and b:
a(i,j)^b(i,j). Both a and b must be the same
shape.
CS 101 Spring 2009
20
Solutions of Linear Equations
{
Homework Assignment #4
Consider the following system of three equations with
three unknowns:
{
z
3x + 2y – z = 10
–x + 3y + 2z = 5
x – y – z = –1
{
{
AX = B
where A=
2
-1
–1
3
2
1
-1
-1
x
10
, B=
5
, and X=
-1
y.
z
{
Page 46: 2, 3
Quiz 2.4
z
which can be expressed as
3
Quiz 2.3
Page 53: 1, 2
You should do your assignment at your Lab
session and hand in your work to your TA
at the end of the Lab before our next class.
Late submission will not be accepted.
It can be solved for X using linear algebra. The solution is
X = A-1B = [–2, 5, –6]T
dr.dcd.h
CS 101 Spring 2009
21
dr.dcd.h
Common MATLAB Functions
{
z
z
z
{
{
abs, min, max, mod, rem
cos, sin, tan
acos, asin, atan, atan2(y,x)
exp, log, log10, sqrt
z
{
z
z
{
char, double, int2str, num2str, str2num
dr.dcd.h
title(str)
xlabel(str)
Ylabel(str)
Grid lines can be enabled by
z
23
plot(x, y)
y is a 1-1 function of x
Title and axis labels can be added by
z
ceil, floor, round, fix
CS 101 Spring 2009
The general form of plot function
z
String conversion functions
z
dr.dcd.h
{
Rounding functions
z
22
Two-Dimensional Plots
Mathmatical functions:
z
CS 101 Spring 2009
grid on
CS 101 Spring 2009
24
Two-Dimensional Plots2
Save Plots
{
The print command can be used to save a
plot as an image from a M-script
z
{
Valid options
z
z
z
z
z
dr.dcd.h
CS 101 Spring 2009
25
dr.dcd.h
z
26
plot(x1, y1, x2, y2, …)
x1 and x2 can be in different ranges
2nd method: plot y1 and y2 separately with
hold function enabled
z
z
z
z
dr.dcd.h
CS 101 Spring 2009
1st method: plot y1 and y2 at once
z
{
–deps: monochrome encapsulated postscript
___ __ image
–depsc: color encapsulated postscript image
–djpeg: JPEG image
–dpng: portable network graphic image
–dtiff: compressed TIFF image
Multiple Plots2
Multiple Plots
{
print <option> filename
plot(x1, y1)
hold on
plot(x2, y2)
hold off
CS 101 Spring 2009
27
dr.dcd.h
CS 101 Spring 2009
28
Graph Properties2
Graph Properties
Line Style
{
You can change the appearance of your
plots by selecting user defined
z
z
z
{
line styles
color
mark styles.
Legends can be added by
z
z
legend(str1, str2, …, <position>)
Position: *Best – least conflict with the figure
NorthWest
North
NorthEast
West
SouthWest
dr.dcd.h
East
South
29
dr.dcd.h
Color
Indicator
.
blue
b
dotted
:
circle
o
green
g
dash-dot
-.
x-mark
x
red
r
dashed
--
plus
+
cyan
c
star
*
magenta
m
square
s
yellow
y
diamond
d
black
k
triangle down
v
triangle up
^
triangle left
<
triangle right
>
pentagram
p
hexagram
h
CS 101 Spring 2009
30
plot(x, y, opstr)
z
z
Opstr combining line style, marker style, and
color
For example: ’:ok’ indicates dotted line ’:’, circle
marker ’o’, and black color ’k’.
legend(str_list, ’Location’, ’Best’)
z
z
dr.dcd.h
Indicator
point
Specify Graph Property2
Specify Graph Property
{
Marker Style
-
SouthEast
CS 101 Spring 2009
{
Indicator
solid
Best – least conflict with the figure
All locations can be outside plot, eg. ’BestOutside’
CS 101 Spring 2009
31
dr.dcd.h
CS 101 Spring 2009
32
Logarithmic Plots2
Logarithmic Plots
{
A logarithmic scale (base 10) is useful if
z
z
{
Logarithmic plot functions
z
z
z
dr.dcd.h
a variable ranges over many orders of
magnitude, because the wide range of values can
be graphed, without compressing the smaller
values.
data varies exponentially.
semilogy – uses a log10 scale on the y axis
semilogx – uses a log10 scale on the x axis
loglog – uses a log10 scale on both axes
CS 101 Spring 2009
33
dr.dcd.h
CS 101 Spring 2009
Example 2.4
Example 2.42
Figure below shows a voltage source V=120 Volt with an
internal resistance RS of 50 Ω supplying a load of
resistance of RL. Find the value of load resistance that will
result in the maximum possible power being supplies by
the source to the load. Plot the power supplied to the
load as a function of RL.
{
{
The power supplied to the load RL is given by
PL = I2 RL
where I is the current supplied to the load, it can be
obtained by Ohm’s law
V
_______
RS+RL
Procedures to perform the work:
z
z
z
z
z
dr.dcd.h
CS 101 Spring 2009
35
dr.dcd.h
34
Define an array of possible values for RL
Compute the current for each value of RL
Compute the supplied power for each value of RL
Plot the power supplied to the load for each value of RL
Determine the maximum power
CS 101 Spring 2009
36
Example 2.43
Homework Assignment #5
{
2.15 Exercises
z
{
{
dr.dcd.h
CS 101 Spring 2009
37
dr.dcd.h
Page 79: 2.1 - 2.7, 2.16
You should do your assignment at your Lab
session and hand in your work to your TA
at the end of the Lab before our next class.
Late submission will not be accepted.
CS 101 Spring 2009
38