lecture25

advertisement
What does C store?
>>A = [1 2 3]
>>B = [1 1]
>>[C,D]=meshgrid(A,B)
a) 1 2 3
1 1 1
c) 1 1 1
1 1 1
b) 1 2 3
1 2 3
d) 1 2 3
1 1 1
What does D store?
>>A = [1 2 3]
>>B = [1 1]
>>[C,D]=meshgrid(A,B)
a) 1 2 3
1 1 1
c) 1 1 1
1 1 1
b) 1 2 3
1 2 3
d) 1 2 3
1 1 1
What does C store?
>>A = [1 2 3]
>>B = [1 1]
>>[C,D]=meshgrid(B,A)
a) 1 1 1
1 1 1
1 1 1
c) 1 2 3
1 2 3
b) 1 1
1 1
1 1
d) 1 1
2 2
3 3
What does D store?
>>A = [1 2 3]
>>B = [1 1]
>>[C,D]=meshgrid(B,A)
a) 1 1 1
1 1 1
1 1 1
c) 1 2 3
1 2 3
b) 1 1
1 1
1 1
d) 1 1
2 2
3 3
LEARNING ABOUT FUNCTIONS IN
MATLAB
A simple function (poly2)
Save the file as poly2.m (same as the name of the function)
The function is available from
the command window or from
other M-file programs
Comments
You should comment functions, just as
you would any computer code
The comment lines immediately after
the first line are returned when you
query the help function
Functions with Multiple Inputs
A user defined function with
multiple inputs
Functions with Multiple Outputs
This function returns 3
output values
If you don’t ask for
all three results, the
program just
returns the first
value
User Defined Input
To this point we have “hard coded” the
values of variables into our M-file
programs
The input function allows us to prompt
the user to enter a value
The input function is used in an M-file
program to prompt the user to enter a
value
The prompt is displayed
in the command window
Input also can input in matrices
and characters
Output Options
Use the disp function
Use the fprintf function – same as C
except single quotes
disp
The display
(disp) function
can be used to
display the
contents of a
matrix without
printing the
matrix name
The disp function can also be used to
display a string
You can combine disp functions to create
meaningful output from an M-file program,
but the result of each disp function is on a
separate line.
Using find and logical
operators in Matlab
Logical Operators
&
~
|
and
not
or
find
The find command searches a matrix
and identifies which elements in that
matrix meet a given criteria.
index numbers
element values
find used with a 2D matrix
x =[1 2 3; 10 5 1; 12 3 2; 8 3 1]
Returns a single element
element = find(x > 9)
number
the row and column
[row, column] = find(x > 9) Returns
designation of an element
The rest of Chapter 5 shows how to
use if/else if and for loops in Matlab –
very similar to C
Homework – Chapter 5, problems 1
and 2
These are a little tougher – so good
luck!
Simple if
if comparison
statements
end
For example….
if G<50
count = count +1;
disp(G);
end
If statements
Easy to interpret for scalars
What does an if statement mean if the
comparison includes a matrix?

The comparison is only true if it is true for
every member of the array
G=[30,55,10]
if G<50
count = count +1;
disp(G);
end
The code inside the if
statement is not executed,
because the comparison is
not true!!
The if/else structure
The simple if triggers the execution of a
block of code if a condition is true
If it is false that block of code is
skipped, and the program continues
without doing anything
What if instead you want to execute an
alternate set of code if the condition is
false?
Use an if structure to calculate a
natural log
Check to see if the input is positive


If it is, calculate the natural log
If it isn’t, send an error message to the
screen
The if/else/elseif structure
Use the elseif for multiple selection criteria
For example
 Write a program to determine if an applicant
is eligible to drive
Repetition Structures - Loops
Loops are used when you need to repeat
a set of instructions multiple times
MATLAB supports two types of loops


for
while
When to use loops
In general loops are best used with
scalars
Many of the problems you may want to
attempt with loops can be better solved
by vectorizing your code or with
MATLAB’s logical functions such as

find
For Loops
for index = [matrix]
commands to be executed
end
•The loop starts with a for statement, and ends with the
word end.
•The first line in the loop defines the number of times the
loops will repeat, using an index number. The loop is
executed once for each element of the index matrix
identified in the first line
•The index of a for loop must be a variable.
Here’s a simple example
thethis
In
index
case
can
k is
bethe
index – the
defined
using
loop
anyis
repeated
of
the techniques
once for
each value
we’ve
learned
of k
Here’s a simple example
thethis
In
index
case
can
k is
bethe
index – the
defined
using
loop
anyis
repeated
of
the techniques
once for
each value
we’ve
learned
of k
While Loops
while criterion
commands to be executed
end
While loops are very similar to for loops.
The big difference is the way MATLAB decides
how many times to repeat the loop.
While loops continue until some criterion is
met.
This loop creates the
matrix a, one element
at a time
Improving the Efficiency of Loops
In general, using a for loop (or a while
loop) is less efficient in MATLAB than
using array operations.
These two
Here’s
an example.
lines of code
This
code acreates
start
timer toa measure
40,000
element
the
elapsed
matrix
time
ofrequired
ones,
then
to
runmultiplies
the lines each
of MATLAB
element
code
between
in the them
matrix by pi
The amount of time it takes to
run this code will depend on
your computer
This code accomplishes the same
thing with a for loop
Download