Title: CS101 Lecture 3
1Lecture 3
2What will I learn in this lecture?
How to program functions. How do the built-in
functions sqrt and diff work? What is the .
operator? Can a function return more than one
array ? Readings Matlab by Pratap Chapter 4.2
3Function - trunc
- 1. Problem Definition
- Write a function named trunc that truncates
(cuts off) the ends of a vector. - 2. Refine, Generalize, Decompose the problem
definition - (i.e., identify sub-problems, I/O, etc.)
- If the input value is 1 2 3 4 5 then trunc
returns the vector 2 3 4. We will assume that
the input vector has at least two values.
4Function - trunc
3. Develop Algorithm (processing steps to solve
problem)
Natural-Language Algorithm If n is the
number of elements of the input vector x then we
want to use subscripting to return the vector
x(2 n-1). Use the Matlab end function to
determine the number of elements in a vector.
5Function - trunc
4. Write the Function" (Code) (instruction
sequence to be carried out by the computer)
Use the Matlab editor to create a file trunc.m .
function result trunc(x) function result
trunc(x) x is a vector that has at least two
values. Programmer Tom Gambill Date
2/2/01 Input a vector, x Output The
function trunc returns x minus its first and
last values. result x(2 end-1)
6Function - trunc
- 5. Test and Debug the Code
- Note on the next slide that the function itself
does not alter the value of x. - 6. Run Code
- Does the function work if x is a column vector?
7Function - trunc
8Function - trunc
trunc works with column vectors too!
9Function - xy_length
- 1. Problem Definition
- Given a sequence of points in the plane, we
can connect these points with lines. Write a
function named xy_length that computes the total
length of all the line segments connecting these
points in the plane. - 2. Refine, Generalize, Decompose the problem
definition - (i.e., identify sub-problems, I/O, etc.)
- See the example on the next slide showing seven
points in the plane connected by line segments.
The points are - (0,20),(5,11.5),(10,6),(15,5),(20,6),(25,11.5),(3
0,20) - but these points can be represented in Matlab
using - x 0 5 10 15 20 25 30
- y 20 11.5 6 5 6 11.5 20
10(No Transcript)
11Function - xy_length
3. Develop Algorithm (processing steps to solve
problem)
Natural-Language Algorithm Given two
points (a,b) (c,d) in the plane. The distance
between these two points is distance sqrt( (c
- a)2 (d - b)2) Therefore, we can compute the
distances between each of the pairs of points
and then use the built-in sum function to add
all of these. But how do we compute the distance
between each of the pairs of points? We must
think in terms of vectors. And use the built-in
Matlab functions and operators whenever we can.
12Example Using vectors
3. Develop Algorithm (processing steps to solve
problem)
For example Lets consider a specific problem
given x and y as follows x 0 5 10 15
20 25 30 y 20 11.5 6 5 6 11.5
20 We would like the solution to be
sqrt((5-0)2 (11.5-20) 2) sqrt((10-5)2
(6-11.5) 2) sqrt((15-10)2 (5-6) 2)
sqrt((20-15)2 (6-5) 2) sqrt((25-20)2
(11.5-6) 2) sqrt((30-25)2 (20-11.5) 2)
working backwards we would like sum(
sqrt((5-0)2 (11.5-20) 2) , sqrt((10-5)2
(6-11.5) 2) , sqrt((15-10)2 (5-6) 2) ,
sqrt((20-15)2 (6-5) 2) , sqrt((25-20)2
(11.5-6) 2), sqrt((30-25)2 (20-11.5) 2) )
but the vector inside the sum function can be
written as (next slide)
13Example Using vectors
3. Develop Algorithm (processing steps to solve
problem)
sqrt( (5-0)2 (11.5-20) 2 , (10-5)2 (6-11.5)
2 , (15-10)2 (5-6) 2 , (20-15)2 (6-5) 2 ,
(25-20)2 (11.5-6) 2, (30-25)2 (20-11.5)2
) but the vector inside the sqrt function can be
written as (5-0)2 , (10-5)2 , (15-10)2 ,
(20-15)2 , (25-20)2 , (30-25)2 (11.5-20) 2
, (6-11.5) 2 , (5-6) 2 , (6-5) 2 , (11.5-6) 2,
(20-11.5)2 but each of these vectors can be
written as (5-0), (10-5) , (15-10) , (20-15),
(25-20) , (30-25) . 2 (11.5-20) ,
(6-11.5) , (5-6) , (6-5) , (11.5-6), (20-11.5)
. 2 now we can use the built-in diff function
since diff(x) (5-0), (10-5) , (15-10) ,
(20-15), (25-20) , (30-25) diff(y)
(11.5-20) , (6-11.5) , (5-6) , (6-5) , (11.5-6),
(20-11.5)
14Example Using vectors
3. Develop Algorithm (processing steps to solve
problem)
Now we can go forwards and write the total
length as sum( sqrt( diff(x) . 2 diff(y).
2 ) ) and this is the formula we will use in our
function.
15Function - xy_length
4. Write the Function" (Code) (instruction
sequence to be carried out by the computer)
Use the Matlab editor to create a file
xy_length.m .
function total xy_length(x,y) function total
xy_length(x,y) x and y are vectors of the
same length. Programmer Tom Gambill Date
2/2/01 Input a vector, x and y Output a
scalar representing the total length of the
line segments formed by points
(x(i),y(i)). total sum( sqrt( diff(x) . 2
diff(y). 2 ) )
16Function - xy_length
- 5. Test and Debug the Code
- Try two points (0,0) and (3,4) since the length
5 - 6. Run Code
- How do you know this code works in all cases?
17Function - xy_length
18Function - xy_length
19. operator
The . operator performs exponentiation on each
element of the array. Examples
gtgt vec1 4 3 2 gtgt vec2 4 3 2 gtgt
vec1 . 2 input is a 3 x 1 column vector
. ans 16 9 4 gtgt vec2 . 2 input is
a 1 x 3 row vector. ans 16 9 4 Note the
exponent can be any number including
fractions. gtgt vec2 . 2.5 32.0000 15.5885 5.6569
20sqrt function
sqrt(x) computes the square root of the values of
x . Examples
gtgt vec1 16 9 4 gtgt vec2 16 9 4
gtgt sqrt(vec1) input is a 3 x 1 column vector
. ans 4 3 2 gtgt sqrt(vec2) input is a 1
x 3 row vector. ans 4 3 2 Note the sqrt
function is equivalent to . 0.5 , e.g. gtgt
vec2 . 0.5 4 3 2
21diff function
diff(x) (see p. 135)computes the consecutive
differences in the values of x. Examples
gtgt vec1 3 4 10 gtgt vec2 -1 0 1
2 3 4 gtgt diff(vec1) input is a 3 x 1
column vector output is a 2 x 1 . ans 1 (
4 - 3) 6 (10 - 4) gtgt diff(vec2) input is a
1 x 6 row vector output is 1 x 5. ans 1 1 1 1 1
22Function - minmax
- 1. Problem Definition
- Write a function named minmax that given a
vector of values, returns two scalar values, the
min and max values of the vector. - 2. Refine, Generalize, Decompose the problem
definition - (i.e., identify sub-problems, I/O, etc.)
- If the input value is -1 -2 3 5 4 then minmax
returns two values, -2 and 5.
23Function - minmax
3. Develop Algorithm (processing steps to solve
problem)
Natural-Language Algorithm If x is the
input vector then min(x) is the minimum value
and max(x) is the maximum.
24Function - minmax
4. Write the Function" (Code) (instruction
sequence to be carried out by the computer)
Use the Matlab editor to create a file minmax.m .
function smallest,largest minmax(x)
function smallest,largest minmax(x) x is a
vector. Programmer Tom Gambill Date
2/2/01 Input a vector, x Output smallest
and largest values of x.smallest
min(x)largest max(x)
25Function - minmax
Note the use of lower, upper to receive the
values that minmax(x) returns .
26Function - minmax
See what happens if you use one variable. You
only get one of the values that minmax(x) returns.
27What have I learned in this lecture?
A Matlab function can return a scalar or array
value or multiple arrays.Programming in Matlab
requires thinking in terms of arrays with
functions and operators acting on arrays. Start
with a specific set of values, write the solution
in terms of these values. Then break down the
solution by finding built-in Matlab functions and
operators that represent the solution.