ppt file

Engineering 1181
Final Exam Review
What to bring with you?

Pen / pencil and eraser.
P. 2
Overview
Final exam is cumulative, including
 Class lectures
• Team working
• Problem solving method
• Excel (graphing, cell referencing, data
collection and analysis)
 Matlab lectures (next page)
 Lab 2 – Lab 8 (go over all the lab
assignments and lab procedures)
Overview

MATLAB











Use of input/output statements
Use of built in functions
Array operations (dot operators)
Array Accessing
Display using fprintf
Graphing, following good graphing practices
Use of loops with arrays
Array indexing
Use of conditional statements
User-defined functions
Tic and toc application
Problem Solving
1.Define
2.Represent
3.Plan
4.Implement
5.Evaluate
Rev: 20120726, PAC
MAT - Structured Problem Solving
Structured Problem Solving




flowcharts
 develop not document
 high level
 executable only
 coding guideline
 specifics not included
Symbols
algorithm
 top down step-wise
 pseudo code
flowchart
 higher level
 verification
Script files




MATLAB allows you to save any series of commands
in a file and will execute the commands sequentially
as if they were typed directly into the command
window.
MATLAB program
text file
sequence
 start to finish
 executable commands
 non-executable comments – (%)
RULES ABOUT VARIABLES NAMES
 Can be up to 63 characters long.
 Must begin with a letter.
 Can contain letters, digits, and the underscore
character (no spaces).
 MATLAB is case sensitive; it distinguishes between
UPPERCASE and lowercase letters.
Avoid naming variables names that are used by MATLAB
for example,
sum, exp, sin, cos, sqrt, length, mean, max, min etc.
SAVING A SCRIPT FILE
 Once the script file is completed, it must be saved. In
our class use Save As to your desktop or USB drive.
This should be the same as the working directory you
specified when you started MATLAB.
 The name of the script file follows some rules
Valid File Names
Invalid File Names
Prob1a.m
Prob_1a.m
Prob 1a.m (Note a blank)
1aProb.m (Cannot start with numeric)
Prob-1a.m (No special characters)
Input


Command Window (CW)
CW and script file share memory
input()

assigns value to single variable

prompts user
numeric
character
prompt flag – ‘s’

response quotes



Output


Omit semicolon (;)
disp()
like omitting semicolon without name
 variables and values (numeric, text, variables must
have a value)
fprintf()


most flexible/powerful option
 write to monitor or file
fprintf(FID, FORMAT, V1, V2, ...)
 FID – file ID, 1 for monitor/Command Window
 FORMAT – controls appearance
 V1, V2, ... – values and variables being written

Repetition
• Indefinite: while
• start
• test – (number < 10)
• contents
• end
…
total = 0;
number = 0;
while (number < 10)
number=number+1;
total=total+number;
end
…
…
total = 0;
number = 1;
while (number <= 10)
total=total+number;
number=number+1;
end
…
Repetition




Definite: for
start
counter
test – (TRUE/FALSE?)
<= stop? OR >= stop?


contents
end
…
for c = start : step : stop
…
…
end
…
…
total = 0;
number = 1:1:10;
for i=1:length(number)
total=total+number(i);
end
total
…
Graphing Basics
•
•
•
•
•
•
•
help plot is your friend
plot(x_data, y_data) equal length vectors
plot(x_data, y_data, 'control string')
color, data point, line style
xlabel('Label for x-axis')
ylabel('Label for y-axis')
title('Title of my plot')
axis([xmin, xmax, ymin, ymax])
Rev: 20120723, PAC
MAT - Graphing
Multiple Plots Per Graph


single call to plot()
help legend is also your friend
• multiple calls to plot()
• hold on after first plot()
• hold off after final plot()
plot(x1,y1,x2,y2)
plot(x1,y1,'control1',x2,y2,’control2’)
legend('set1','set2')
plot(x1,y1,'control1')
hold on
plot(x2,y2,'control2')
plot(x3,y3,'control3‘)
hold off
legend('set1','set2','set3')
Decisions

a = 4;
b = 12;
TRUE


non-zero
evaluate to one

simple
(a < 5)

FALSE

zero

complex
(a < 5) & (b >

branching
20)
Relational Operators
<
>
less than
greater than
<= less than or equal to
>= greater than or
equal to
== equal to
~= not equal to
A < B
A > B
A <= B
A >= B
A == B
A ~= B
Logical Operators – AND

&


two operands
A & B
salsa = 1;
guacamole = 0;
salsa & guacamole


TRUE if both
operands are TRUE
FALSE if either
operand is FALSE
fish = -5;
chips = 1;
fish & chips
Logical Operators – OR

|


two operands
A | B
eggs = 1;
bacon = 0;
eggs | bacon


TRUE if either
operand is TRUE
toast = -5;
muffin = 1
FALSE if both
operands are FALSE
toast | muffin
Logical Operators – NOT

~





one operand
~A
negates operand
TRUE if operand
FALSE
FALSE if operand
TRUE
potato = 7;
~potato
tomato = 0;
~tomato
Math, Relational and Logical Operators
Order of Precedence
Precedence
Order
Highest
1
Lowest
Operator
(
)
Name
Parentheses
2
^
Exponent
3
~
Negation (Logical "NOT")
4
* /
Multiply, Divide
5
+ –
Add, Subtract
6
< > <= >= == ~=
Relational Operators
7
&
Logical "AND"
8
|
Logical "OR"
Selection Structures

if-elseif-else-end



multiple comparison types
relational operators
logical operators
Matrix Addressing
Just as with vectors, individual elements of matrices can be
accessed. The elements of a matrix are addressed by their
row number and column number.
For example let us define a 2 x 4 matrix “mat” as follows:
>> mat = [2 3 4 5 ; 1 6 8 9]
mat =
1st Row, 2nd Column
2 3 4 5
2nd Row, 3rd Column
1 6 8 9
>> mat(1,2)
ans =
3
>> mat(2,3)
ans =
8
Matrix Addressing : Examples
>> A = [1 3 5 7; 2 4 6 8;
3 6 9 12; 4 8 12 16]
A=
1
3
5
7
2
4
6
8
3
6
9
12
4
8
12
16
>> B = A(1:3, 2:4)
B=
3 5 7
4 6 8
6 9 12
The sub matrix is assigned
to a matrix B
Matrix Addressing : Examples
>> A = [1 3 5 7; 2 4 6 8;
3 6 9 12; 4 8 12 16]
A=
1
3
5
7
2
4
6
8
3
6
9
12
4
8
12
16
>> A(1:3 , : ) % All Columns
ans =
1 3 5 7
2 4 6 8
3 6 9 12
>> A( : , 2:4)
ans =
3 5
4 6
6 9
8 12
% All rows
7
8
12
16
Using a colon (:) when addressing matrices
A(: , 3)
Elements in all the rows of column 3
A(2 , :)
Elements in all the columns of row 2
A(: , 2:5)
Elements in columns 2 through 5 in all the rows
A(2:4, :)
Elements in rows 2 through 4 in all the columns
A(1:3, 2:4)
Elements in rows 1 through 3 and in columns 2
through 4
Scalar-vector math summary
(or scalar-matrix case)
For a scalar c and a vector v: (can be also applied to scalarmatrix case)
Addition
Subtraction
v+c
v–c
or
or
c+v
c–v
Multiplication
v*c
or
or c * v
v .* c or c .* v
Division
v/c
or
or c ./ v
v ./ c
Exponent
v .^ c or c .^ v
When in doubt, you can always use a dot operator for
Multiplication, Division, or Exponent
Vector-vector math summary
(or matrix-matrix case)
For two vectors x and y: (can be also applied to matrixmatrix case)
Addition
Subtraction
x+y
x–y
or
or
y+x
y–x
Multiplication
x .* y
or
y .* x
Division
x ./ y
or
y ./ x
Exponent
x .^ y or y .^ x
You must always use the dot operator for
Multiplication, Division, and Exponent
Example
Calculate y = (4a2 + a) / (2+a) for a = 1, 2, 3 and 4
Define a = [ 1 2 3 4 ]
>> a = [ 1 2 3 4 ] ;
a =
1 2 3 4
>> y = ((4 * a .^ 2) + a) ./ (2 + a)
y =
1.6667
4.5000
7.8000
11.3333
Built-in vector functions
MATLAB has built-in functions for vectors
When v is a vector:
max(v)
Returns the largest element in v
min(v)
Returns the smallest element in v
mean(v)
Returns the average value of the elements in v
sum(v)
Returns the sum of the elements of v
length(v)
Returns the number of elements in v
sort(v)
Sorts the elements of v
Characteristics of Flow Charts

Useful tool in program development

Describes the logic flow of the program

Not a complete description of program

Not only tool to use

Made before writing the program

No formal standards, but common
guidelines
Flow Chart Symbols
Start or End
a Program:
Start
Decision or
General
Selection Structure: Processing:
Quit?
Y
N
process
End
A<B?
T
F
Start
SAMPLE
FLOWCHART
Any Stones
left?
No
Yes
Get Stone
Discard
Stone
No
Does Stone
Fit?
Yes
Put Stone in
Bottle
No
Is Bottle
Full?
Yes
End
Creating a function file
The first line in the function file should be
the function definition line:
function [output vars] = function_name(input vars)
 If this is omitted, MATLAB interprets the
file as a script file, not a function file.
Saving Function Files
 After adding commands, save the function file
using the name function_name.m
function [output vars] = function_name(input vars)
 Thus, if the name of your function is my_calc,
save the function file as my_calc.m
What to bring with you?

Pen / pencil and eraser.
P. 36