Вы находитесь на странице: 1из 15

IE 361 Midterm Preparation

1 CHAPTER 1. MATLAB REVIEW


(1) Programing and DS&A
1. Programming: implementation tool
2. Conceptual thinking and design
3. Practical design and implementation
Good Design and Good implementation are both important

(2) MATLAB
1. Interpreter
2. Support Object-oriented
3. Dynamic types of variable
4. Not generic programming language, but can be used in generic tasks
5. Intensive computation employable
6. Prototyping language
1) Can see how model works
2) Run analyses
3) Easy and quick
7. Fast Development Speed, slow execution speed

(3) Program File Lists, Selected File Details, Main Console, Variables in Memory, Recent
Commands

(4) Very Simple MATLAB Command
1. +, -, *, /
2. Value: 1,2, ln2 > noted log2, exp(5)
3. Variable: memory place that can hold a value at a certain time
4. Assign and Use

(5) Basics of MATLAB
1. Computation oriented programming tool
2. Excellent performance on handling matrices
3. Operators: What to do?
1) +, -, *, /
4. Operands: With What?
1) Scalars
2) Vectors one by n matrix
3) Matrices

(6) Defining and Handling Matrices
1. Separate column: Blank or comma
2. Separate row: semicolon
3. Difference between * and .*
1) * : Typical matrix multiplication
2) .*: Array based matrix calculation
4. Specifying a cell
1) a(1,2) matrix a row 1, column 2 entry
2) a(1,:) matrix a row 1 entry
3) a(:,1) matrix a column 1 entry

(7) Operations on Matrices
1. Division Same Operation as multiplication

(8) Special Matrices
1. eye(2) : identity 2X2 matrix
2. zeros(2,2): [ 0,0;0,0]
3. ones(2,2): [1,1;1,1]
4. diag(1,2,3) = [1 0 0; 0 2 0; 0 0 3]
5. rand(2,2) 2X2 matrix of which entries are from [0,1] uniform distribution
6. randn(2,2) 2X2 matrix of which entries are from normal distribution
7. a[1:2,2:3] Sub range of matrix

(9) Diverse Operation
1. Logical Operators -> True Entry 1 False entry 0
return , Dimension .
1) == : equal to
2) ~= : not equal to
3) > , <: greater than or less than
4) >=, <=: greater than or equal to // less than or equal to
5) &&: and returns 1 if two statements are BOTH true
6) || : or returns 1 if at least one of the two statements are true
7) ~ : not
2. Functions
1) log: natural log
2) find: find the index of the entry
3) max: find the row which has the largest entry
4) min: find the row which has the smallest entry
5) mean: find the average of rows
6) std: find the standard deviation of rows
7) sum: find the sum of the rows
8) cumsum: cumulative sum of the rows
9) prod: get the product of the rows
10) cumprod: get the cumulative product of the rows
11) diff: find the differences of the rows

(10) More Matrix manipulations
1. Concatenation
X = [1 2]
Y = [3 4]
Z1 = [X Y] = [1 2 3 4]
Z2 = [X;Y] = [1 2; 3 4]
2. Reshape
X = [ 1,2,3;4,5,6;7,8,9]
X(:) 1,4,7,2,5,8,3,6,9
Reshape(x(:), 3,3) 3X3 Matrix , X
Return

3. Replication
X = [1 2 ; 3 4]
Repmat if repmat(X, 3, 4) X entry
3X4 matrix .

(11) When you dont know a function
1. Help <function>
2. Like help diff, help cumsum
3. Examples, parameter lists, related functions

(12) Hello World in Matlab
(1) Not really program
(2) Procedure-oriented program : main() is a function
(3) Two parts (Definition Part, Execution Part)
<Code 1> Procedure oriented HelloWorld
function [ ] = HelloWorldFunction()
disp(Hello, world);
disp(This program computes the average of two exam scores);
inputFirstScore = What is the First score?;
inputSecondScore = What is the Second score?;
score1 = input(inputFirstScore);
score2 = input(inputSecondScore);
average = (score1 + score2)/2;
fprintf(Average score is %0.3f\n, average);

<Code 2>Object Oriented HelloWorld
Classdef HelloWorld < handle
Methods
Function this = HelloWorld()
Disp(HelloWorld);
End
Function performAverage(this, val1, val2)
Average = (val1+val2)/2;
Fprintf(Average is %.3f\n,average);
End
End
End
1. Disp string Expression
2. Fprintf print the result value with the string

(13) Naming and Styling
1. Use Camel Casing
1) Class name capitalize the first letter of each world
2) Variable name start with lower case word
3) Method name start with lower case world
2. Indentation : 4 spaces per each level

(14) Comments
1. Comments are used for
1) Explain codes to others
2) Note your ideas
3) Declare Copyrights
2. Writing comments is important
1) For boss
2) For future colleagues
3) For myself in the future
3. %: Single line comment
4. %{ ~ } : Block comments

(15) Variable Statements
<Code 3> Variable Statements

numYearBase10 = 2011;
numYearBase16 = 708;
% hex2dec is a function to convert to string of hex number into decimal
Fprintf(Year by base 10 : %d, by base 16, %d \n, numYearBase10,
hex2dec(numYearBase16)); hex2dec : inbuilt function

numComplex1 = complex(3,4); 3+4i
numComplex2 = 4+3i

fprintf(Complex value : %d+%di \n, real(numComplex1),imag(numComplex1));
fprintf(Absolute value : %.1f\n, abs(numComplex2));
fprintf(Real Value: %.1f\n,real(numComplex2));
fprintf(Image Value:%.1f\n,imag(numComplex2));

---- real, abs, imag : inbuilt functions

strDeptName = Industrial & System Engineering;
strUnivName = KAIST;
strWholeName = strcat(strDeptName,strUnivName);
fprintf(Department : %s \n,strDeptName);
fprintf(Full name of dept: %s\n,strWholeName);

---- strcat : string concatenation

(16) String
1. String variable is used as wrappers
2. String variable is actually a linear collection of letters, and the letters have indices
3. You can use a simple iteration technique with :
4. Negative iteration works too!
<Code 4> String

strTest = Hello World! ISE;
disp(strTest);
fprintf(%s%s%s, strTest(1), strTest(2), strTest(3));
disp( strTest(1:3));
disp(strTest(3:end));
disp(strTest(2:2:10));
disp(strTest(2:2:end));
disp(strTest(5:-1:1));

(17) More on Strings
<Code 5> More on strings

strTest = Hello World! ISE;
disp(strTest);
fprintf(%s, %s, strTest(1), strTest(2));

disp(length(strTest));
disp([strTest Dept]
disp(repmat(strTest,1,2));
disp(strcmpi(strTest,ISE));
disp(strcmpi(strTest(end-2:end),ISE));

length(~): length of the string
strcmpi: string comparison
string end

(18) If Statement
1. A condition statement
1) If: statement for true
2) Elseif : statements for true
3) Else: statements for false
4) End
2. Switch-case
1) Many ifs are possibles
<Code6> if
numScore = 95
if numScore > 90
disp(A);
end

numScore = 75
if numScore > 90
disp(A)
else
disp(lower grade)
end

if numScore > 90
disp(A);
elseif numScore > 80
disp(B);
elseif numScore > 70
disp(C);
else
disp( D or F);
end

(19) for
1. A loop statement
2. Most common loop statement in programming languages
3. Format
- for idx = values in vector;
Statements for loop
end
4. continue: for
5. break: iteration (termination)
<Code 7> for
for itr = 1:10
fprintf(%d ,itr);
end
disp(done);
sum = 0;
for itr = 1: 11
sum = sum + itr
end
disp(sum);
disp(done);
for itr = 1:10:100
if itr == 51
continue
else
fprintf(%d,itr);
end
end
disp(done)

for itr = 1:5
if itr == 3
break
end
fprintf(%d,itr);
end
disp(done);
(20) while
1. second loop statement
2. Syntax
-while Boolean
statements for loop
else:
3. Continue break
<Code 8> while
Itr = 0;
Sum = 0;
While itr < 10
Itr = itr +1 ;
Sum = sum + itr;
End
Fprintf(Sum from 1 to 10: %d,sum);

Itr = 0; sum = 0;
While itr < 10
Itr = itr +1;
If itr == 5;
Break
End
Sum = sum + itr;
End
Fprintf(sum from 1 to 10: %d,sum);

(21) Function Statement
1. Function [ return variables ] = name(parameters)
Statements
End
2. Can return multiple variables, but keep them in order!
<Code 9> function

Function [output 1 output2 output3] = add_multiply_increase(input1, input2, step)
If nargin < 3
Step = 1;
End

If nargin < 2
Input2 = 5;
End

Output1 = input1 + input2;
Output2 = input1 * input2;
Output3 = input1 + step;
End

(22) Inline Functions
1. functionName = inline(x+y,x,y)
1) inline : this is an inline function
2) number of parameters for the function specified
2. can just call the function with the function name
3. Shortened version f = @(x,y) 3.*x+y./2
4. Function handle
<Code 10> inline Function

f = inline(x+y,x,y)
numA = 1;
numb = 2;
numC = f(2,3);
numD = f(5,7);
numE = f(7,7);

f = inline(3.*x+y./2,x,y)
numA = 1;
numb = 2;
out = (f[2 3 4],[3 5 7])
disp(out);

(23) Finding Prime Number
<Code 10. Finding prime number>
Function findPrime(numParam1, numParam2)
If nargin == 0
numParam1 = 1;
numParam2 = 10;
end
numCount = 1;
for itr = numParam1:numParam2
if isPrimeNumber(itr) == true
fprintf( %d th prime : %d \n,numCount,itr);
numCount = numCount + 1;
end
end

function out = isPrimeNumber(in)
out = true;
for inltr = 2:in-1
if mod(in,inltr) == 0
out = false;
break
end
end
end
end
(24) Class and Instance
(25) Important Elements in Class
1. Properties
1) Attributes
2) Member Variables
3) Hold the important information of the class
2. Methods
1) Functions
2) Member functions
3) Specify what to do when the class behaves
3. Special Method
- Constructor: Execute when a class is instantiated
- Use the class name as the method name

(26) Class with Handle and without handle
1. < handle means that it inherits the Object in Matlab world
2. If no handle, matlab recognizes it as a value not a reference
3. Two instances: one from handle one from no handle
4. { } : hold an array
5. H1: reference, h2: value
6. Without handle: leaves the original value untouched, instead copies the original
value to the new variable
7. How to make it work? Receive a return value, so create another copy of
instance with changed properties
8. H2.paintroof(green) does not work but h2 = h2.paintroof(green) works
9. Eq, ge, le function
10. paintRoof this = paintRoof

Вам также может понравиться