Ieee2013_upgrading_knowledge_matlab_pt2
-
Upload
georgios-drakopoulos -
Category
Education
-
view
138 -
download
0
description
Transcript of Ieee2013_upgrading_knowledge_matlab_pt2
![Page 1: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/1.jpg)
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
![Page 2: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/2.jpg)
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
![Page 3: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/3.jpg)
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
![Page 4: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/4.jpg)
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.
![Page 5: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/5.jpg)
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
![Page 6: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/6.jpg)
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’.
![Page 7: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/7.jpg)
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
![Page 8: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/8.jpg)
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 =
![Page 9: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/9.jpg)
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
![Page 10: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/10.jpg)
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.
![Page 11: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/11.jpg)
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
![Page 12: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/12.jpg)
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.
![Page 13: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/13.jpg)
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.
![Page 14: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/14.jpg)
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.
![Page 15: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/15.jpg)
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.
![Page 16: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/16.jpg)
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
![Page 17: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/17.jpg)
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.
![Page 18: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/18.jpg)
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.
![Page 19: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/19.jpg)
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
![Page 20: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/20.jpg)
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
![Page 21: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/21.jpg)
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
![Page 22: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/22.jpg)
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.
![Page 23: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/23.jpg)
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
![Page 24: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/24.jpg)
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
![Page 25: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/25.jpg)
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
![Page 26: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/26.jpg)
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.
![Page 27: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/27.jpg)
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);
![Page 28: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/28.jpg)
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
![Page 29: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/29.jpg)
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
![Page 30: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/30.jpg)
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
![Page 31: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/31.jpg)
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.
![Page 32: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/32.jpg)
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
![Page 33: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/33.jpg)
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Graphics
Handle
plot
hold
stem
figure
subplot
xlabel
title
legend
saveas
![Page 34: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/34.jpg)
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
![Page 35: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/35.jpg)
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
![Page 36: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/36.jpg)
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.
![Page 37: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/37.jpg)
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
![Page 38: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/38.jpg)
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.
![Page 39: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/39.jpg)
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.
![Page 40: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/40.jpg)
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!
![Page 41: Ieee2013_upgrading_knowledge_matlab_pt2](https://reader033.fdocuments.in/reader033/viewer/2022052619/55622da2d8b42af6668b537b/html5/thumbnails/41.jpg)
IEEE upgradingknowledge
GeorgiosDrakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Thank you!Questions?