1 / 109

Programming

Programming. Keywords. Matlab reserves following keywords for its language construction: break , case, catch, continue, else, else if, end, for, function, global, if, otherwise, persistent, return, switch, try, while. Variables. Types of Variables: Local, Global, and Persistent.

iniko
Download Presentation

Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming

  2. Keywords • Matlab reserves following keywords for its language construction: break, case, catch, continue, else, else if, end, for, function, global, if, otherwise, persistent, return, switch, try, while.

  3. Variables • Types of Variables: Local, Global, and Persistent Each Matlab function has its own local variables. These are separated from other functions and from those of the base work space. - Local variables do not remain in memory from one function call to the next, unless they are defined as global, or persistent. If several function and the base workspace, all declare a particular name as global, then they all share a single copy of that variable. - (lotka.m) function yp = lotka(t,y) %LOTKA Lotka-Volterra predator-pray model. global ALPHA BETA yp = [y(1) - ALPHA*y(1)*y(2); -y(2) + BETA*y(1)*y(2)]; - Then run the example.

  4. Operators Arithmetic Operators +, -, .*, ./, .\, +, -, :, .^, .’, ’, *, /, \, ^ Relational Operators <, <=, >, >=, ==, = Logical Operators &, |, , xor, any, all Short-Circuit Operators &&, || ^

  5. Operators Advantage of Short-Circuiting - Keeps the following code from generating an error: comp = (exist(’myfun.m’) == 2) && (myfun(x) >= y) - Avoid divide-by-zero error: x = (b = 0) && (a/b > 18.5) - in if and while statements: if (nargin >= 3) && (ischar(varargin{3})) ^

  6. Operator Precedence - Parenthesises () - Transpose(.’), power(.^), complex conjugate transpose(’), matrix power(^). - Unary plus(+), unary minus(-), logical negation(). - Multiplication(.*), right division(./), left division(.\), matrix multiplication(*), matrix right division(/), matrixleft division(\). - Addition(+), subtraction(-). - Colon operator(:) - Less than(<), less than or equal to (<=), greater than (>), greater than or equal to (>=), not equal to (=). - Element-wise AND (&). - Element-wise OR (|). - Short-circuit AND (&&), short-circuit OR (||).

  7. Program Control Statements Conditional Control - if, switch Loop Control - for, while, continue, break Error Control - try, catch Program Termination - return

  8. Data Types in MATLAB Array Logical Char Numeric Structure Others ‘a’ image.width = 120 image.name = ‘face1’ Uint8 (8 bit, from 0 to 255, e.g., pixel values) Double e.g., 3.2567 (8 bytes)

  9. Uint8 and Doubles • Double • almost all MATLAB functions • expect doubles as arguments • return doubles

  10. Uint8 and Doubles • Double • almost all MATLAB functions • expect doubles as arguments • return doubles • e.g., • need to convert uint8 todouble before performingany math operations » a = 1:10 a = 1 2 3 4 5 6 7 8 9 10 » b = uint8(a) b = 1 2 3 4 5 6 7 8 9 10 » whos Name Size Bytes Class a 1x10 80 double array b 1x10 10 uint8 array » b*2 ??? Error using ==> * Function '*' not defined for variables of class 'uint8'. » (double(b))*2 ans = 2 4 6 8 10 12 14 16 18 20

  11. Char Data Type » c = ['hello']; » whos Name Size Bytes Class c 1x5 10 char array Grand total is 5 elements using 10 bytes » c(1) ans = h

  12. Char Data Type » d = [c,' again']; » d d = hello again » » b = ['hello';'again']; » size(b) ans = 2 5 » b b = hello again » » c = ['hello']; » whos Name Size Bytes Class c 1x5 10 char array Grand total is 5 elements using 10 bytes » c(1) ans = h

  13. Char Data Type » d = [c,' again']; » d d = hello again » » b = ['hello';'again']; » size(b) ans = 2 5 » b b = hello again » » c = ['hello']; » whos Name Size Bytes Class c 1x5 10 char array Grand total is 5 elements using 10 bytes » c(1) ans = h Many string functions available, e.g., strcat, num2str, etc

  14. Struct Data Type image.name = 'Tom'; Field Name Structure Name

  15. Struct Data Type image.name = 'Tom'; » image.height = 3; » image.width = 3; » image.data = [8 10 2; 22 7 22; 2 4 7]; » whos Name Size Bytes Class image 1x1 590 struct array Grand total is 18 elements using 590 bytes

  16. Arrays of Structures » image(1) = image; » image(2).name = 'Mary' » image(2).width = 4; » image(2).height = 4; » whos Name Size Bytes Class image 1x2 894 struct array Grand total is 28 elements using 894 bytes » image image = 1x2 struct array with fields: name height width data

  17. Arrays of Structures » image(1) = image; » image(2).name = 'Mary' » image(2).width = 4; » image(2).height = 4; » whos Name Size Bytes Class image 1x2 894 struct array Grand total is 28 elements using 894 bytes » image image = 1x2 struct array with fields: name height width data » image(2) ans = name: 'Mary' height: 4 width: 4 data: [] » image(1) ans = name: 'Tom' height: 3 width: 3 data: [3x3 double]

  18. Operators • Arithmetic • numeric computations, e.g., 2^10 • Relational • quantitative comparison of operands • e.g., a < b • Logical • AND, OR, NOT, etc • result of type Logical, 1 (TRUE) or 0 (FALSE)

  19. Arithmetic Operators • Transpose, a’ • Power, a^2 • Addition, multiplication, division • a(1)*b(2) • a*b • works if a and b are matriceswith appropriate dimensions(columns(a) = rows(b)) • a.*b (element by element) • except for matrix operations, mostoperands must be of the same size,unless one is a scalar

  20. Arithmetic Operators • Transpose, a’ • Power, a^2 • Addition, multiplication, division • a(1)*b(2) • a*b • works if a and b are matriceswith appropriate dimensions(columns(a) = rows(b)) • a.*b (element by element) • except for matrix operations, mostoperands must be of the same size,unless one is a scalar » a = [2 3]; » b = [4 5]; » a(1)*b(2) ans = 10 » a*b ??? Error using ==> * Inner matrix dimensions must agree. » a*b' ans = 23 » a.*b ans = 8 15 » b/2 ans = 2.0000 2.5000

  21. Relational Operators • <, <=, >, >=, ==, ~= • compare corresponding elementsof arrays with same dimensions • if one is scalar, one is not, the scalaris compared with each element • result is of type Logical • element by element 1 or 0

  22. Relational Operators » a a = 2 3 » b b = 4 5 » a > b ans = 0 0 » b > a ans = 1 1 » a > 2 ans = 0 1 • <, <=, >, >=, ==, ~= • compare corresponding elementsof arrays with same dimensions • if one is scalar, one is not, the scalaris compared with each element • result is of type Logical • element by element 1 or 0

  23. Flow Control • If, else, endif if index<100 statementselse statementsend • For….. For i = 1:100 statementsend • Switch, while, case, etc

  24. Vectorization of Computation tic for i=1:100000 y(i) = log(i); end toc

  25. Vectorization of Computation tic for i=1:100000 y(i) = log(i); end toc elapsed_time = 168.78 seconds

  26. Vectorization of Computation tic for i=1:100000 y(i) = log(i); end toc tic i=1:100000; z = log(i); toc elapsed_time = 168.78 seconds

  27. Vectorization of Computation tic for i=1:100000 y(i) = log(i); end toc tic i=1:100000; z = log(i); toc elapsed_time = 168.78 seconds elapsed_time = 0.053 seconds First method calls the log function 100,000 times, Second method only calls it once (much faster)

  28. Memory Preallocation • What happens in the previous example if we preallocate memory to y and z? e.g., y = zeros(10000,1); z = zeros(10000,1);

  29. MATLAB Programming • “M File” = text file with MATLAB code, e.g., sort.m • Two kinds of M-files • scripts • no input arguments supplied • no output arguments returned • operates on data in workspace • functions • can accept input arguments and return output arguments • internal variables local to function by default • useful for extending functionality of MATLAB

  30. Example of a MATLAB script % script randscript % A simple script to generate a vector of n % random numbers. We calculate the numbers % using (a) for loops, and (b) a direct function call. % % Professor Smyth, Oct 2007 Comment text: it is always important to put comments in your code. Comments at the top should clearly explain what the script or function does

  31. Example of a MATLAB script % script randsum % A simple script to generate a vector of n % random numbers. We calculate the numbers % using (a) for loops, and (b) a direct function call. % % Professor Smyth, Oct 2007 n = 100000; % the number of points for the "for loop” y = zeros(n,1); % preallocate memory for y fprintf('Simulating %d random numbers.....\n\n',n); Initialize various variables Print out some information to the screen

  32. Example of a MATLAB script % script randsum % A simple script to generate a vector of n % random numbers. We calculate the numbers % using (a) for loops, and (b) a direct function call. % % Professor Smyth, Oct 2007 n = 100000; % the number of points for the "for loop” y = zeros(n,1); % preallocate memory for y fprintf('Simulating %d random numbers.....\n\n',n); % first do the calculation using a "for loop" fprintf('For loop calculations.....\n'); tic % set the timer for i=1:n y(i) = rand(1); end total = sum(y); fprintf('Sum of %d random numbers = %f\n',n,total); t1 = toc; % read the time elapsed since "tic" (in seconds) fprintf('Time taken, using for loop = %6.5f microseconds\n\n', (t1)*1000); …... (1) Calculate the n random numbers and their sum using a for loop, (2) record the time taken, and (3) print to the screen

  33. Example of a MATLAB script ……… … % now do the calculation using vectorization fprintf('Vectorization calculations.....\n'); tic % reset the timer z = rand(n,1); total = sum(z); fprintf('Sum of %d random numbers = %f\n',n,total); t2 = toc; % read the time elapsed since "tic" (in seconds) fprintf('Time taken, using vectorization = %6.5f microseconds\n', (t2)*1000); (1) Now calculate n random numbers and their sum using a direct function call (2) record the time taken, and (3) print to the screen

  34. MATLAB random number generator • Function rand.m • Generates a sequence (of length n) of pseudorandom numbers: • sequence generation: x(i) = mod(a * x(i-1), m) • initialized with a state (“seed”) value for x(0)

  35. » help rand RAND Uniformly distributed random numbers. RAND produces pseudo-random numbers. The sequence of numbers generated is determined by the state of the generator. Since MATLAB resets the state at start-up, the sequence of numbers generated will be the same unless the state is changed. S = RAND('state') is a 35-element vector containing the current state of the uniform generator. RAND('state',S) resets the state to S. RAND('state',0) resets the generator to its initial state. RAND('state',J), for integer J, resets the generator to its J-th state. RAND('state',sum(100*clock)) resets it to a different state each time. This generator can generate all the floating point numbers in the closed interval [2^(-53), 1-2^(-53)]. Theoretically, it can generate over 2^1492 values before repeating itself.

  36. Example of a MATLAB function function [sum, difference] = sumdiff(a, b); List of input argument values, comma delimited (any form of array data type) Name of the function List of output values returned (can be any form of array data type)

  37. Example of a MATLAB function function [sum, difference] = sumdiff(a, b); % function [sum, difference] = sumdiff(a, b); % % A simple function to compute the sum and difference % of two input arguments a and b % % Professor Smyth, Oct 2007 % % INPUTS: % a: array of size r x c % b: array of size r x c % % OUTPUTS: % sum: a + b % difference: a - b Clear comments in function headers are very useful Note the explicit statement explaining what the inputs and outputs are (including their dimensionality)

  38. Example of a MATLAB function function [sum, difference] = sumdiff(a, b); % function [sum, difference] = sumdiff(a, b); % % A simple function to compute the sum and difference % of two input arguments a and b % % Professor Smyth, Oct 2007 % …………….. % error checking [rowsa, colsa] = size(a); [rowsb, colsb] = size(b); if( rowsa ~= rowsb ) | ( colsa ~= colsb) error(‘sizes of a and b do not match’); end Error checking is always a good idea!

  39. Example of a MATLAB function function [sum, difference] = sumdiff(a, b); % function [sum, difference] = sumdiff(a, b); % % A simple function to compute the sum and difference % of two input arguments a and b % % Professor Smyth, Oct 2007 % …………….. % error checking [rowsa, colsa] = size(a); [rowsb, colsb] = size(b); if( rowsa ~= rowsb ) | ( colsa ~= colsb) error(‘sizes of a and b do not match’); end sum = a + b; difference = a – b; Finally, the actual computational part of the function

  40. MATLAB functions in general • Function line definition • required of all functions • List of inputs and outputs • comma delimited: [y, z] = average(a, b, c) • for more than one output, outputs are enclosed in square brackets • Input variables • variables within function are local to the function • input variables are readable to the function, but not writable • values of returned arguments are passed back • Search path • MATLAB searches in this order: • variable name, subfunction, current directory, MATLAB search path

  41. Example of a MATLAB function function [meanr, stdr, z] = simulate(n); List of input argument values, comma delimited (any form of array data type) Name of the function List of output values returned (can be any form of array data type) Tells MATLAB this is a function

  42. Example of a MATLAB function function [meanr, stdr, z] = simulate(n); % function [meanr, stdr, z] = simulate(n); % % A simple function to simulate a vector of n % uniformly distributed random numbers and return the % mean and standard deviation of the numbers. % % Professor Smyth, Oct 2007 % % INPUTS: % n: number of random numbers generated (positive integer) % % OUTPUTS: % meanr: mean of the n random numbers % stdr: standard deviation of the random numbers % z: an n x 1 array of random numbers

  43. Example of a MATLAB function function [meanr, stdr, z] = simulate(n); % function [meanr, stdr, z] = simulate(n); % % A simple function to simulate a vector of n % uniformly distributed random numbers and return the % mean and standard deviation of the numbers. % % Professor Smyth, Oct 2007 % % INPUTS: % n: number of random numbers generated (positive integer) % % OUTPUTS: % meanr: mean of the n random numbers % stdr: standard deviation of the random numbers % z: an n x 1 array of random numbers % simple error checking to check n is a positive integer if (rem(n,1)~=0) | n<=0 error('Input n must be a positive integer'); end

  44. Example of a MATLAB function …. … fprintf('Simulating %d random numbers.....\n\n',n); % generate the n random numbers z = rand(n,1); % calculate the mean and standard deviation meanr= mean(z); fprintf('Mean of the %d random numbers = %f\n',n,meanr); stdr= std(z); fprintf('Standard deviation of the %d random numbers = %f\n',n,stdr); Simulate the random numbers and print out the results. No need for an explicit return statement Any values not returned are known only locally to the function

  45. Creating a MATLAB function In-class illustration of how to create and use a MATLAB function

  46. Calling the MATLAB function » [m, s] = simulate(1000000); Simulating 1000000 random numbers..... Mean of the 1000000 random numbers = 0.499702 Standard deviation of the 1000000 random numbers = 0.499702 » [m, s] = simulate(1000000); Simulating 1000000 random numbers..... Mean of the 1000000 random numbers = 0.499684 Standard deviation of the 1000000 random numbers = 0.288456 » m m = 0.4997 » s s = 0.2885

  47. Another MATLAB function function [meanr, stdr, z] = simplot(n,plotflag); % function [meanr, stdr, z] = simplot(n,plotflag); % % A simple function to simulate a vector of n % uniformly distributed random numbers and return the % mean and standard deviation of the numbers. If % plotflag is 1 a histogram of the numbers is plotted % % Professor Smyth, Oct 2007 % % INPUTS: % n: number of random numbers generated (positive integer) % plotflag: if plotflag=1, a histogram of z is plotted, % otherwise no plotting % % OUTPUTS: % meanr: mean of the n random numbers % stdr: standard deviation of the random numbers % z: an n x 1 array of random numbers % simple error checking to check n is a positive integer if (rem(n,1)~=0) | n<=0 error('Input n must be a positive integer'); end

  48. Simplot.m continued fprintf('Simulating %d random numbers.....\n\n',n); % generate the n random numbers z = rand(n,1); % calculate the mean and standard deviation meanr= mean(z); fprintf('Mean of the %d random numbers = %f\n',n,meanr); stdr= std(z); fprintf('Standard deviation of the %d random numbers = %f\n',n,stdr); if nargin>1 & plotflag==1 figure hist(z, max(n/100,10)) end Here is the new code. Nargin is the number of input variables If we have a 2nd input and it equals 1, then we plot a histogram of the random numbers syntax: hist(data vector, number of bins)

  49. Demonstration: plotting of sample mean as a function of n • Extend simplot.m (call it simplot2.m) • for each value of i = 1 to n, calculate • mean(i) = [sum (x(i)…… x(i)) ]/I • use vectorization to do this • mean(i) should converge to the true mean 0.5 as i gets large • why? “law of large numbers” from statistics • illustration of the use of the online debugger • we can plot this to visualize it • various additional plotting features • grids, log axes, labels, titles

  50. Code that was added to simplot.m if nargin>1 & plotflag==1 figure % figure for a histogram to see how uniform the numbers are hist(z,max(n/100,10)) figure % figure to see visually how the sample mean converges to 0.5 cs = cumsum(z); % generate a vector of cumulative sums nvalues = 1:n; % generate a vector of sample sizes runningmean = cs./nvalues; % there’s an error here (on purpose) plot(nvaluess,runningmean); % and another error here grid; axis([1 n 0 1]); xlabel('Number of random numbers generated'); ylabel('Mean value'); title('Convergence of sample mean to true mean'); end

More Related