Transcript ppt

CS100A, Fall 1998
Lecture 18, Tuesday Nov 3
Introduction to Matlab
Concepts:
• Matlab as a graphical calculator for scalars
& arrays
Readings: choose one of:
• Getting Started with Matlab, Pratap
• Mastering Matlab, Hanselman &
Littlefield
• Student Edition of Matlab User’s Guide,
Hanselman & Littlefield
• or any other basic Matlab book
CS100A, Fall 1998, Lecture 18
1
Why Matlab?
• The premier package for numerical
computing, particularly arrays (matrices).
Widely used in science/engineering.
• Provides high-level interface to best-ofclass numerical methods. Problem-solving
without lower-level programming details.
• Powerful graphics and visualization tools.
– has variables, loops, conditionals,
functions
– but much array/matrix computation
can be done directly without loops.
CS100A, Fall 1998, Lecture 18
2
•
•
•
•
•
•
Matlab Environment
Enter expressions or commands in the
console window. Commands are executed
immediately. An expression is evaluated
and its value is immediately displayed.
Can define command scripts and new
functions (future lecture).
Most important feature: help command.
Enter help to get a general list of
available topics, or help topic for
information on topic.
Enter
more on
in the console window to pause output after
each full screen. Hit space to continue.
Anything following a % is ignored. Use it
to include notes or comments in a session.
To leave Matlab, enter
quit
CS100A, Fall 1998, Lecture 18
3
•
•
•
•
•
Expressions
The usual basic arithmetic operations are
provided (+, –, *, /, and ^). Everything is
floating-point†, although integer values are
displayed without a fractional part.
– 9/10 is 0.9 in Matlab
– ^ is exponentiation (2 ^ 10)
Logical operations treat 1 as the value true
and 0 as false.
Comparisons: <, <=, ==, ~=, >=, >
Logical Operators: &, |, ~
Examples:
3*4+5
3*4+5/2
3 * (4 + 5) / 2
(3 < 2 ^ 2) & ~ (3 < 2)
(3 < 2 ^ 2) | ~ (3 < 2)
† Actually, everything in Matlab is a matrix containing
complex, floating-point numbers. But for now we can
restrict ourselves to integers and real numbers.
CS100A, Fall 1998, Lecture 18
4
•
•
•
•
•
Variables
Variables are created when they are first
assigned a value.
x = 17
y=3*x
All variables are global (for now).
A variable exists from the time it is created
until you quit Matlab.
Variable names are case-sensitive. Entering
a = 17
A = 42
creates two separate variables.
Several variables containing useful
constants are already defined
pi
3.14159…
Inf
i, j
sqrt(–1)
NaN 0/0
CS100A, Fall 1998, Lecture 18
5
Functions
• Matlab provides a rich collections of standard
functions.
– Trigonometry: sin, cos, tan, cot, asin, acos,
atan, atan2…
– Exponential: exp, log, log10, sqrt
– Complex: real, imag, abs, …
– Rounding: floor, ceil, round, rem, sign
– Specialized: bessel, gamma, erf, log2, rat,
…
• Syntax: function-name ( arg1, arg2, … )
• Examples:
x = 3;
y = 4;
d = sqrt(x ^ 2 + y ^ 2)
sin(pi / 2)
exp(1)
sqrt(–1)
CS100A, Fall 1998, Lecture 18
6
Output and Input
• The value of a Matlab expression or
statement is displayed immediately unless it
is followed by a semicolon.
z=x^2
w = x ^ 3;
• To change the precision of the output enter
format long
format short
Other formats are also available. (Enter
help format for details.)
• You can edit and reenter previous console
input. Use the up- and down-arrow keys to
access previous entries.
CS100A, Fall 1998, Lecture 18
7
Arrays (Vectors)
• All data in Matlab is actually an array — a
1- or 2-dimensional table of numbers. (We
only consider 1-D arrays in this lecture.)
• A single value, called a scalar, is simply an
array of size 1.
• To construct a 1-D array, list its elements
surrounded by square brackets.
y = [4 -5 10 0 5.2]
x = [–5 sqrt(2) 17 2^3]
• Individual elements are accessed using a
parenthesized subscript.
x(3)
• The first element of array x is x(1).
• Can assign to elements of array as in Java.
y(1) = 0
• The number of elements in array x is given
by the built-in function
length(x)
CS100A, Fall 1998, Lecture 18
8
Array Functions
• An array of evenly-spaced values can be
generated by
linspace(minVal, maxVal, nVals)
Example: array of 100 values spaced from 0
to 2.
v = linspace(0, 2*pi, 100);
• There are many functions to compute facts
about arrays.
min(x)
max(x)
mean(x)
sum(x)
x(1) + … +x(length(x))
prod(x)
x(1) * … * x(length(x))
cumsum(x) cumulative sum
CS100A, Fall 1998, Lecture 18
9
Creating Arrays
• Two arrays can be combined with a comma
and brackets:
x = [1 2 3];
y = [4 5 6];
[x, y]
(is [1 2 3 4 5 6])
z = [x, [x, y]];
CS100A, Fall 1998, Lecture 18
10
Creating Arrays
• The colon can be used to generate a
sequence of values. Forms:
lowValue : highValue
lowValue : step : highValue
Examples:
1 : 10
1 : 2 : 10
1 : 0.5 : 10
10 : –1 : 1
0 : 0.01 : 0.5
• A sequence of values is an array value.
a = 0 : 2 : 16
b = [1 : 6] / 3
• A sequence of integers can also be used to
select a segment of an array.
a(3:6)
CS100A, Fall 1998, Lecture 18
11