Digital Adventures with Avatars! Tips & Resources for Teachers
Ieee2013_upgrading_knowledge_matlab_pt2
-
Upload
georgios-drakopoulos -
Category
Education
-
view
138 -
download
0
description
Transcript of Ieee2013_upgrading_knowledge_matlab_pt2
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
IEEE upgrading knowledgeMatLab workshop (second part)
Georgios Drakopoulos
CEID
December 15, 2013
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Agenda
Topics
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Agenda
Topics
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Overview
Official MatLab users community
Questions can be posted and answered.
Feedback required.
Login credentials required; Free account.
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Agenda
Topics
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Cipher system
Data
A lowercase letter string.
Task
A string where each original letter is shifted right by three.
Wrap around ’z’.
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
First approach
Code
function y = cipher1(x)%CIPHER1 First approach to cipher system.% y = cipher1(x) transforms x into cipher y using% arithmetic operations.
if isa(x, 'char')na = 'a'; nz = 'z';y = na + mod(((x − na) + 3), nz − na + 1);y = char(y);
elseerror('Input is not a string.');
endreturn
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
if conditional
Syntax
if x == 5disp('five!');
elseif x == 6disp('now six!');
elsedisp('something else!');
end
Notes
Equality with ==
Assignment with =
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Class check
Syntax
isa(x, 'char')
Frequent classes
single
double
logical
(u)int8/16/32/64
cell
struct
function handle
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Assessment
Plus
Simple.
Quick.
Minus
Assumes linear character representation.
True for ASCII character set.
Still machine dependent.
Notes
Frequently used by Julius Cæsar.
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Second approach
Code
function y = cipher2(x)%CIPHER2 Second approach to cipher system.% y = cipher2(x) transforms x into cipher y using tables.
T = 'a':'z'; %not proper but ok ..
if isa(x, 'char')n = length(x);for k = 1:n
xpos = find(x(k) == T);ypos = circshift(xpos, 3);y = [y T(ypos)];
end;else
error('Input is not a string.');endreturn
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
for loop
Syntax
for k = a:s:b% do something with kend
for k = a:b% assumes s = 1end
Notes
a must be lower than b when s is positive.
a must be greater than b when s is negative.
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
while loop
Syntax
while a > b% do somethingend
Notes
for and while are equivalent.
One form may be more convenient.
Be careful when bounds change.
Make sure loop terminates.
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
find function
Syntax
% find non−zero entries of X by defultI = find(X);[I, J] = find(X);
% find positive entries of XI = find(X > 0)
% find the first n positive entries of XI = find(X > 0, n);
Notes
Use sub2ind to switch from pairwise to linear.
Use ind2sub to switch from linear to pairwise.
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Help functions
help
Displays text based help.
Preamble comments are important.
doc
Graphical help.
Extended function description.
lookfor
Term based search.
Similar to UNIX apropos command.
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Wrapper function
Code
function y = cipherw(x, method)%CIPHERW Wrapper function to ciphper1 and cipher2.
switch nargincase 1
y = cipher2(x);case 2
switch methodcase 'numeric'
y = cipher1(x);case 'table'
y = cipher2(x);otherwise
error('Incorrect method.');end
endreturn
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
switch conditional
Syntax
switch methodcase 'zero'
disp('It''s zero point!');case { 'one', 'two' }
fprintf('%s\n', 'A string');case 4
pause(4)otherwise
warning('Droplets in the ocean!');end
Notes
cell groups strings and matrices.
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
break and continue
Syntax
for k = 1:−0.005:1if k ˜= 0% do somethingelse
continueend
end
Notes
break terminates innermost loop.
continue jumps to next iteration.
Same functionality with their C counterparts.
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Agenda
Topics
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Least squaresThe problem
Data
n observations in (xk , yk) tabular format.
Linear relation between x and y .
yk = α0 xk + β0, 1 ≤ k ≤ n
Result
Compute α0 and β0 which minimize the cost function
J (α0 , β0) =n∑
k=1
(yk − (α0 xk + β0))2
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Least squaresSolution
Setting the derivative to zero yieldsx1 1x2 1...
...xn 1
︸ ︷︷ ︸
A
[α0
β0
]︸ ︷︷ ︸
x
=
y1
y2
...yn
︸ ︷︷ ︸
b
Least squares solution (ATA
)x̂ = AT b
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Least squaresThe task
Formulation[∑nk=1 x
2k
∑nk=1 xk∑n
k=1 xk n
] [α0
β0
]=
[∑nk=1 xk yk∑nk=1 yk
]
Data
Create the straight line y = 5x − 1
α0 = 5β0 = −1
Corrupt it with noise.
Record performance vs noise power.
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Sneak peek
Code
a0 = 5;b0 = −1;x = 0:15;yc = a0*x + b0;
rng('shuffle');Ey = norm(yc);y = yc + sqrt(0.05*Ey)*randn(size(x));save lsqtest a0 b0 x y
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
First approach
Code
function xls = lsq1(x, b)%LSQ1 First approach to least squares.% xls = lsq1(x, b) solves normal equations.
T = [ norm(x)ˆ2 sum(x) ; sum(x) length(x) ];g = [ dot(x,b) ; sum(b) ];xls = inv(T) * g; %not always ok ...return
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Second approach
Code
function xls = lsq2(x, b)%LSQ2 Second approach to least squares.% xls = lsq2(x, b) exploits slash operator.
T = [ x(:) ones(length(x), 1) ];xls = T \ b(:);return
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Wrapper functionControl struct
Code
load lsqtests = struct(
'x', x, ...'b', y, ....'method', 'slash' );
Notes
Older functions rely on nargin.
Newer functions rely on struct.
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
struct handling
Functions
getfield
setfield
isfield
rmfield
fieldnames
Notes
z = struct('a', 1, 'b', 2);z = setfield(z, 'c', 3);
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Wrapper functionFunction
Code
function xls = lsqw(s)%LSQW Least squares wrapper function.% xls = lsqw(s)
switch s.methodcase 'slash'
xls = lsq2(s.x, s.b);case 'normal'
xls = lsq1(s.x, s.b);otherwise
warning('Incorrect method.');endreturn
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
PlotNoisy data
Code
xi = linspace(min(x), max(x), 10*length(x));yi = interp1(x, y, xi, 'spline');figurehold onplot(x, y, 'k'), plot(xi, yi, 'r'), plot(x, y, 'ro')xlabel('Variable x')ylabel('Variable y ')legend('Linear', 'Cubic', 'Discrete', −1)title('Least squares problem (linear and cubic data fit)')print(gcf, '−depsc2', 'ieee2013 matlab2 lsq00.eps')saveas(gcf, 'lsq00', 'fig')hold off
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
PlotNoisy data (figure)
0 5 10 15−10
0
10
20
30
40
50
60
70
80
Variable x
Variable
y
Least squares problem (linear and cubic data fit)
Linear
Cubic
Discrete
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
PlotProjected data
Code
yp = xls2(1) * x + xls2(2);plot(x, yc, 'k', x, y, 'r', x, yp, 'b')xlabel('Variable x')ylabel('Variable y = \alpha 0 x + \beta 0')title('Least squares problem ...
(argmin \{ | | y − A x | | 2ˆ2 \})')saveas(gcf, 'lsq01', 'fig')print(gcf, '−depsc2', 'fig01.eps')
Notes
Limited LATEX support in title and x/ylabel.
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
PlotProjected data (figure)
0 5 10 15−10
0
10
20
30
40
50
60
70
80
Variable x
Variable
y =
α0 x
+ β
0
Least squares problem (argmin { || y − A x||2
2 })
Original
Given
Projected
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Graphics
Handle
plot
hold
stem
figure
subplot
xlabel
title
legend
saveas
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Agenda
Topics
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Another (hello) worldFunction
Code
function dyn(s)%DYN Displays dynamic hello world.%% dyn(s) displays "s: Hello world!"
z = [ 'disp( [ '' ' ...char(s) ...' '' '': Hello world!''] )' ];
disp(z);eval(z);return
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Another (hello) worldAssessment
Plus
Shorter code.
Flexible code.
Easy to understand.
Minus
Overhead.
Difficult to understand.
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Agenda
Topics
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Code accelerationOverview
Topics
Critical issue.
Sometimes even for proof of concept.
MatLab JIT compiler slow code compensation.
Vectorization.
Column-based operations.
MatLab uses column-major storage format.
Memory preallocation.
Known operand sizes.
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Fast forward
Topics
mex connects C to MatLab.
matlab compiler compiles MatLab code.
Eclipse plugin for MatLab.
Package mcode for LATEX.
Alternatives
Octave.
ScalaLab.
SciLab.
NumPy.
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Final message
In brief
Join MatLab community.
Exploit MatLab flexibility.
Be careful though.
Utilize help, doc, and lookfor.
There is more than one way to do things.
Wrapper functions.
save intermediate data.
Optimize.
Have fun!
Programming is fun!
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Thank you!Questions?