Introduction to Mathematical Programming Ming Zhong Lecture 3 September 5, 2018 Ming Zhong (JHU) AMS Fall 2018 1 / 14
Programming with MATLAB Table of Contents 1 Programming with MATLAB 2 Logic, Loops and Iterations Ming Zhong (JHU) AMS Fall 2018 2 / 14
Programming with MATLAB What Is MATLAB MATrix LABoratory is: A multi-paradigm numerical computing environment and proprietary programming language developed by MathWorks. has interface to work with C, C++, C#, Java, Fortran, and Python. has support for symbolic computing via MuPAD symbolic engine. adds multi-domain simulation, and model-based design for dynamic and embedded systems via Simulink. Strong support on matrix manipulations, plotting, user interfaces. First developed by Cleve Moler in late 1970 s (wrapper of LINPACK and EISPACK); rewritten by Jack Little, Steve Banger and Moler in C in 1984, put into commercial use, the new libraries were called JACKPAC. In, 2000, a new set of libraries called LAPACK was put into MATLAB. Popular in the teaching of Linear Algebra, Numerical Analysis, and image processing. Ming Zhong (JHU) AMS Fall 2018 3 / 14
Programming with MATLAB MATLAB Files File Extensions:.m: code (function, script, or class);.mat :data;.mex*: executable files;.p: content-obscured file;.mlx: live script;.fig: figures;.mlapp: apps;.mlappinstall: package App Installer;.mlpkginstall: support package installer;.mltx,.mltbx: package custom toolbox;.prj: project files;.rpt: report setup file; Ming Zhong (JHU) AMS Fall 2018 4 / 14
Programming with MATLAB How to Install MATLAB Do the following: log onto my.jhu.edu; go to Technology, then mysoftware, click on https://itservices. johnshopkins.edu/serviceportal?id=evg_software. under Search the Software Catalog, click on Matlab for Students, find the activation key, and follows the steps there. Ming Zhong (JHU) AMS Fall 2018 5 / 14
Programming with MATLAB Variables in MATLAB Variables in MATLAB: no need to declare them, just assign values, variables can be re-assigned to become different types; >> x = [1 3 2] creates the row vector, x = ( 1 3 2 ). 1 >> x = [1; 3; 2] creates the column vector, x = 3. 2 >> x = 0 : 1 : 10 creates x = ( 0 1 2 10 ) (syntax: >> x = a : h : b or >> x = a : b). >> x = linspace(0, 10, 11) does the same. 1 3 2 >> A = [1 3 2; 5 6 7; 8 3 1] creates A = 5 6 7. 8 3 1 Ming Zhong (JHU) AMS Fall 2018 6 / 14
Programming with MATLAB Indexing of Matrices in MATLAB >> x = A(2, 3) to access the a 2,3 of A; >> x = A(2, :) to access the second row of A. >> x = A(:, 3) to access the third column of A. ( ) 6 >> x = A(2 : 3, 2) to have x =. 3 >> x = A(3, 2 : end) to have x = ( 3 1 ). ( ) 3 2 >> C = A(1 : end 1, 2 : 3) to have C =. 6 7 ( ) 3 2 >> D = [A(1, 2 : 3); A(1 : 2, 3). ] to have D =. 2 7 3 + 2i x = 1, y = x., so y = ( 3 + 2i 1 8 ) ; y = x, so 8 y = ( 3 2i 1 8 ). Ming Zhong (JHU) AMS Fall 2018 7 / 14
Programming with MATLAB Logical Indexing Let x = [ 1 2 3 5 2], y1 = (x > 0); y2 = (x > 0). x; y3 = (x > 0) 3; y 1 = ( 0 1 1 1 0 ) ; y 2 = ( 0 2 3 5 0 ) ; y 3 = ( 0 3 3 3 0 ) ; y4 = zeros(1, 5), y4(x > 0) = x(x > 0). y 4 = ( 0 2 3 5 0 ). Ming Zhong (JHU) AMS Fall 2018 8 / 14
Logic, Loops and Iterations Table of Contents 1 Programming with MATLAB 2 Logic, Loops and Iterations Ming Zhong (JHU) AMS Fall 2018 9 / 14
Logic, Loops and Iterations Logic Expressions Logic MATLAB expression equal to == not equal to = greater than > great than or equal to >= less than < less than or equal to <= AND & OR Ming Zhong (JHU) AMS Fall 2018 10 / 14
Logic, Loops and Iterations Loops The if -statement: if (logical statement) elseif (logical statement) else (logical statement) end Ming Zhong (JHU) AMS Fall 2018 11 / 14
Logic, Loops and Iterations Loops The switch -statement: switch (variable) case value1 case value2 otherwise end Ming Zhong (JHU) AMS Fall 2018 12 / 14
Logic, Loops and Iterations The for -statement: for (assign counters) end The while -statement: while (logical expression) end Ming Zhong (JHU) AMS Fall 2018 13 / 14
Logic, Loops and Iterations Finding a root of f (x) = exp(x) tan(x) Let us look at the graphs: Ming Zhong (JHU) AMS Fall 2018 14 / 14