A brief introduction to MATLAB for students of Science, Engineering and Mathematics Jorge LemagneBindura University of Science Education Zimbabwe 0. ABSTRACT MATLAB is a high-level language and interactive environment for numerical computation, visualization, and programming. The language, tools, and built-in math functions enable us to explore multiple approaches and reach a solution faster than with spreadsheets or traditional programming languages, such as C/C++ or Java.MATLAB can be integrated as a supplement to the traditional classroom teaching and learning. It has been shown that the students can benefit from the software, whose use enhances students' conceptual understanding despite their weak mathematical skills. It has been noticed that students' attitudes are positive and their performance in the courses improves.In the bibliography there exist several good tutorials on MATLAB. However, a student would need considerable time to assimilate any of them, and so would a lecturer for the explanation. The brief introduction that is presented in this contribution can be explained in two hours and has been exposed as a lecture by the author at Bindura University.This introduction includes Desktop Basics (in particular, the Command Window), Matrices and Arrays, Array Indexing, Workspace Variables, Calling Functions, 2-D Plots and Programming and Scripts.Some of these elements are illustrated by examples in Math courses. After learning this brief introduction the students will be able to solve simple problems, or to study a tutorial or to pass a course on MATLAB more easily. Keywords: MATLAB, Mathematics Teaching, Computer Programming, Programming languages, Numerical computation 1. INTRODUCTION MATLAB® is a high-level language and interactive environment for numerical computation, visualization, and programming. Using it, you can analyse data, develop algorithms, and create models and applications. The language, tools, and built-in math functions enable you to explore multiple approaches. You can use MATLAB for a range of applications, including signal processing and communications, image and video processing, control systems, test and measurement, computational finance, and computational biology. More than a million engineers and scientists in industry and academia use MATLAB, the language of technical computing (MathWorks, 2013). Moreover, MATLAB is a teaching and learning aid for mathematics students. MATLAB has been integrated as a supplement to the traditional classroom teaching and learning. For example, at University of Ha'il, Saudi Arabia, a study was made. Its aims were to assess the effects of the use of the software on students' motivation and attitudes towards the utilization of technology in the teaching and learning of engineering mathematics, and its impact on learning. Qualitative and quantitative data collected from the study revealed that the students have benefitted from the software. The use of the software has enhanced students' conceptual understanding despite their weak mathematical skills. It has been noticed that students' attitudes have been positive and their performance in the course has improved (Majid, M.A., Huneiti, Z.A., Al-Naafa, M.A., & Balachandran, W., 2012). Students taking some mathematics courses are expected to acquire a basic working knowledge of MATLAB. MATLAB may be used to help complete some of the homework assignments and might be necessary for the completion of designated computer assignments (University of Utah). Students need to have access to a computer with MATLAB installed. Which version matters little as the course will be using very basic functionality which hardly changes between versions.The students should have a way to access their files whenever they start working. If no other solution is possible, a USB "stick" can easily hold the student's files. The students need to have access to MATLAB outside of class hours (Massachusetts Institute of Technology, 2015). The author of this paper supports the ideas expressed in the preceding paragraphs. In the bibliography there exist several good tutorials on MATLAB. However, a student would need considerable time to assimilate any of them, and so would a lecturer for the explanation. The brief introduction that is 1|Page presented in this contribution can be explained in two hours and has been exposed as a lecture by the author at Bindura University. Preferably, this lecture should be given in a computer laboratory so that the students can verify in practice all explanations. After learning this brief introduction the students will be able to solve simple problems, or to study a tutorial or to pass a course on MATLAB more easily. 2.GETTING STARTED WITH MATLAB (Some material in this section was adapted from MathWorks(2013) and York University) 2.1 Preliminaries The basic data element in MATLAB is an array that does not require dimensioning. This allows you to solve many technical computing problems, especially those with matrix and vector formulations, in a fraction of the time it would take to write a program in a scalar non-interactive language such as C or FORTRAN. The name MATLAB stands for MATrixLABoratory. Starting MATLAB On Windows platforms, to start MATLAB, double click the MATLAB shortcut icon on your Windows desktop.Then, you will see something like this: Figure 1: MATLAB Home Taskbar and prompt For the time being we only need this window, the Command Window; if there are others open, you can close them. 2.2 Vectors Let us start to use the Command Window, manipulating some vectors (i.e. arrays of one dimension). Where other programming languages work with numbers one at a time, MATLAB allows you to work with entire arrays quickly and easily. We can enter arrays into MATLAB in several different ways. One of them is by entering an explicit list of elements. Example 1 At the command line, after MATLAB prompt “>>”, type this statement: >> a = [6 1 4 2]; >> This instruction or statement assigns the vector [6 1 4 2] to the variable “a”. Instead of “a”, we may specify any letter, followed by any number of letters, digits or underscores. MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. “A” and “a” are not the same variable. To view the matrix assigned to any variable, simply enter the variable name: >>a 2|Page a= 6 1 4 2 >> “=” does not have the same meaning as in Mathematics. In this case, it is not a relational operator but an assignment operator. If “;” is not typed, the content of “a” will also appear on the screen: >> a = [6 1 4 2] a= 6 1 4 2 >> Once you have entered the array, it is automatically remembered in the MATLAB Workspace: Figure 2: MATLAB Workspace Note: To open Workspace, click “Layout” and then check “Workspace”. You can refer to the array, simply by its name, in this case “a”. Previous commands can be recalled by pressing the up- and down-arrow keys, ↑ and ↓. The Colon Operator The colon “:” is one of the most important MATLAB operators. It occurs in several different forms: Example 2 The expression1:7is a row vector containing the integers from 1 to 7 1 2 3 4 5 6 7 To obtain non-unit spacing, specify an increment. Example 3 >> 100:-7:50 ans = 100 93 86 79 72 65 58 51 Note: When you do not specify an output variable, MATLAB uses the variable “ans”, short for answer, to store the results of your calculation. Example 4 >> 0:pi/4:pi ans = 0 0.7854 1.5708 2.3562 3.1416 Example 5 3|Page It is required to plot the “sin” function between 0 and , step 0.1, with blue * markers: >> x = 0:0.1:2*pi; >> y = sin(x); >>plot(x,y,'*') >> The following figure is obtained: 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 0 1 2 3 4 Figure 3: Sin function between 0 and 5 6 7 and step 0.1 MATLAB has hundreds of available functions. To display documentation of one, for example, sin, type >>doc sin For a briefer documentation in the Command Window, type >> help sin 2.3 Mfiles M files are text files containing MATLAB code. To create an M file we can use the MATLAB Editor or another text editor (to open MATLAB Editor, click “NewScript”). The M file will contain the same statements you would type at the MATLAB command line. Save the file under a name that ends in “.m”. Example 6 It is required to plot the sin function between 0 and user. , with blue * markers, with a given step, specified by the The M file may content the following: % This very simple program plots sin(x) for % different values between 0 and 2*pi. Answer = inputdlg(‘Step size=‘); Step = str2double(Answer); x = 0:Step:2*pi; 4|Page y = sin(x); plot(x,y,'*') The two lines starting with “%” are comments. The instruction of the following linecreates and opens an input dialog box: Figure 4: Input dialog box “Answer” receives a string, for example, ‘0.1’,and then “str2double” (string to double) transforms the string into numeric value representation (double precision). The M file corresponding to the preceding program is called script. Scripts are the simplest kind of Mfiles because they have no input or output arguments. 2.4 Matrices and Subscripts Let us create the matrix >> A = [16 5 10 11 2 13 6 7 12 8 9 4 15 14 3 1]; >> To separate the matrix rows we also can hit “;” instead of “enter”.This table is known as Dürer's magic square. (Albrecht Dürer (1471–1528) was a painter, printmaker and theorist of the German Renaissance.) Figure 5: Dürer’s Self-Portrait at 28 (a), Melencolia I (b) and Detail of Melencolia I (c) (Taken from http://www.taliscope.com/Durer_28.jpeg&imgrefurl, http://dackel.tripod.com/art/melencla.gif&imgrefurland Wikimedia Foundation, Inc., (2015), respectively) The order-4 magic square that appears in Dürer's engraving Melencolia I is believed to be the first seen in European art.The sum 34 can be found in the rows, columns, diagonals, each of the quadrants, the center four squares, and the corner squares(of the 4 4 as well as the four contained 3 3 grids). This sum can also be found in the four outer numbers clockwise from the corners (3+8+14+9) and likewise the four counter-clockwise, the two sets of four symmetrical numbers (2+8+9+15 and 3+5+12+14), the sum of the middle two entries of the two outer columns and rows (5+9+8+12 and 3+2+15+14), and in four kite or cross shaped quartets (3+5+11+15, 2+10+8+14, 3+9+7+15, and 2+6+12+14). The two numbers in the middle of the bottom row give the date of the engraving: 1514. The numbers 1 and 4 at either side of the date correspond to the letters 'A' and 'D' which are the initials of the artist (Wikimedia Foundation, 2015). 5|Page Let us verify a few of these features. The first instruction to try is >>sum(A) ans = 34 34 34 34 >> These are the sums by columns. To obtain the sums by rows, we consider first A’, the conjugate transpose of A (in this particular example, it is simply the transpose, because A is real). Then, we type: >>sum(A')' ans = 34 34 34 34 >> The sum of the elements on the main diagonal is obtained with “sum” and “diag” functions: >>sum(diag(A)) ans = 34 >> The element in row i and column j of A is denoted by A(i, j). For example, A(4, 2) is the number in the fourth row and second column. For our magic square, A(4, 2) is 15. Subscript expressions involving colons refer to portions of a matrix. For example, A(1:k, j) is the first k elements of the j-th column of A. So sum(A(1:4,4)) computes the sum of the fourth column. But there is a better way. The colon by itself refers to all the elements in a row or column of a matrix and the keyword “end” refers to the last row or column. So sum(A(:, end)) computes the sum of the elements in the last column of A: >>sum(A(:, end)) ans = 34 >> Another way for creating matrices On the example of order-4 magic square, we created a matrix by entering an explicit list of elements. Another way is by “load” command. Let us create again a variable Acontaining order-4 magic square, but on this occasion by load command. 1. 2. 3. Open MATLAB Editor (again, click “NewScript”). Type the 16 elements of the square, arranged as a table (or copy and paste, if it is available). Store the file under some name, for example A.dat 6|Page 4. In the Command Window type “load A.dat”. This instruction reads the file and creates a variable, “A”, containing our example matrix. 3. ILLUSTRATION IN MATH COURSES The author of this paper is introducing MATLAB in his courses. Here are two examples: 3.1 Multivariate Methods Example 7 The following table shows the heights (in inches) and the weights (in pounds) for 10 employees of a firm (Taken from Makridakis& Wheelwright(1983)). 61 163 59 114 63 161 63 144 64 145 60 118 65 156 68 160 69 167 61 141 Examine the extent of the relationship between the two measures. To solve this problem, we create an M file with the preceding table and save it as Height_Weight.m. Then, in the Command Window we type: >> load Height_Weight.m >>mean(Height_Weight) ans = 63.3000 146.9000 >> % These are the corresponding means. >>cov(Height_Weight) ans = 10.9000 44.4778 44.4778 342.3222 >> % This is the covariance matrix. >>corrcoef(Height_Weight) 7|Page ans = 1.0000 0.7281 0.7281 1.0000 >> % This is the correlation coefficient matrix. >> The sample is not large, but we can say there is quite a strong relationship between the two variables. “mean”, “cov” and “corrcoef” are only three of the many functions that can be used in MATLAB. 3.2 Optimization Example 8 Consider the following linear programming problem: Maximize subject to , , , In this paper, we are only concerned with the graphical solution. Applying the graphical facilities of MATLAB we obtain the following figure: Figure 6: Graphical solution of Example 8 problem Therefore, the solution is . 4. REFERENCES 8|Page Majid, M.A., Huneiti, Z.A., Al-Naafa, M.A. & Balachandran, W., (2012), A study of the effects of using MATLAB as a pedagogical tool for engineering mathematics students, Interactive Collaborative Learning (ICL) 15th International Conference on,http://ieeexplore.ieee.org/xpl/ Makridakis, S., & Wheelwright, S (1983), Forecasting methods and Applications, 2nd edition, New York Wiley Massachusetts Institute of Technology (2015), Syllabus, MIT OpenCourseWare,http://ocw.mit.edu/courses/mathematics/18-s997-introduction-to-matlab-programmingfall-2011/Syllabus/ MathWorks, The, Inc., MATLAB R2013a University of Utah, The, Department of Mathematics,(n. d.),MATLAB Information,http://www.math.utah.edu/ Wikimedia Foundation, Inc., (2015), Magic square, Wikipedia,https://en.wikipedia.org/wiki/ York University (n. d.), MATLAB LESSON 1, http://www.yorku.ca/jdc/Matlab/Lesson1.htm 9|Page