Lesson 1 (Getting Started) Here is a simple tutorial to get you started on the version of Matlab R12 installed in our labs. Use the editor to create a new file. then on M file. Give it some name – say abhi.m Then go to the command window. First click on File, then new and It will look like this: >> Type the name of your program file without the .m extension. In other words, type: >>abhi And the program will be compiled and executed (basically, MATLAB will give you the results you are looking for). Lesson 2 (Variables & Assignment Statements) Variables in MATLAB: When we write programs, we deal with variables (just as when we speak we use words). For example, if you are writing a program to calculate the area of a circle, you will need to supply the program, the radius of the circle and the program will calculate the area. Consider the following program (prog1): %%%%%%%%Matlab program (prog1) to calculate area of a circle r=10; %declares the radius to be 10 pi=22/7; % declares a variable pi to be 22/7 area=pi*(r^2); % calculates the area of a circle area % displays the area of a circle %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% In the above program, notice the following: 1. % indicates a comment and whatever follows % on that line is for you to read, the compiler ignores it. 2. Notice that r is a variable and that r=10 means r10 . In other words, r gets a value of 10. Remember the equality sign is like a left arrow and is also known as an assignment operator since it assigns the value on its right hand side to the variable on the left-hand side. So it is okay to write a statement like a=b; What this will achieve is as follows: the value of b (if it has one) will be assigned to a. What is NOT okay is a statement like: 10=d; The problem with this is that 10 is a constant and no value can be assigned to it. 3. Now, pi is another variable which gets the value of 22/7 4. area is a variable that gets the value of whatever is on the right hand side of the assignment state on line 4. On the right hand side, there are two operators: * and ^. Now a*b means a is multiplied by b and a^b means a is raised to the power of b. Similarly you can use +, - , / to mean addition, subtraction and division. All variables on the right hand side must already have values (pi has a value of 22/7 and r is 10 in the program, given above). This is important, as otherwise the compiler will complain. 5. The last line is a statement written to display the value of the variable area. Notice that this statement does not have a delimiter (;). Whenever this delimiter is missing in an assignment statement, the variable on the left-hand side of the statement will be displayed on the screen. An interesting feature of MATLAB is that the type of the variables (integer, float, double) does not have to be defined unlike the case in C or FORTRAN. If you are not familiar with these languages, don’t worry about this last statement. MATLAB has its own grammar and we need to learn some rules before we start writing programs in it. All the statements that we saw above, in the MATLAB program, are the socalled assignment statements. Let us formalize the rules for writing these statements: Assignment statements: 1. In an assignment, there has to be an equality sign, which means 2. The quantity on the right hand side should either be a constant (such as 10 or 4.567) or a variable whose value has been assigned in some previous statement or else a mathematical expression containing variables ( a*(b-c)^2+(x/y) ), whose variables have values assigned in preceding statements. 3. Each statement is terminated with a delimiter (;) else the value of the variable in the left-hand side gets displayed on the screen when the program is run. Lesson 3 (Arrays, Matrices, Loops and if-statements) You are familiar with vectors (row-vectors and column vectors) and matrices. In MATLAB, a one-dimensional vector or a multi-dimensional matrix is referred to by a single name, such as a. A particular element of a vector may be referred to by a(index). For example consider the row vector A =[1 2 3]. In MATLAB we may define this by: A(1)=1; A(2)=2; A(3)=3; Notice the round brackets above. An alternative, more convenient way to define this vector (array) Would be to use the statement: A=[1,2,3]; Notice the square brackets above. Now let us consider a two-dimensional matrix: A= 1 3 4 4 6 7 This can be defined as: A=[1,3,4;4,6,7]; Notice that elements of the same row are separated by “,” and the whole row is separated from the other by “;” Also we can use an element by element definition as follows: A(1,1)=1; A(1,2)=3; A(1,3)=4; A(2,1)=4; A(2,2)=6; A(2,3)=7; Also you need to remember that any element of this matrix may be accessed by the following format: matrix_name(row_number,column_number) For example A(2,3) will access the element in the 2nd row and third column of A The if-statement Sometimes, we wish to perform an operation only if a certain condition is satisfied. Such as: let’s say if an array has any negative element, that element should be converted to 0. Consider the program below: a=[1,-3,4]; for count=1:3 % line 0 if a(count)<0 %line 1 a(count)=0; %line 2 end % line 3 end (Notice the round brackets in a(count)) You will notice above that there is an if statement, in line 1. The if statement also has an end-statement. The general format of the if statement is as follows: if condition body of the if statement end The condition could be as given in the example above : a(count)<0 which tests if the variable a(count) is less than 0. If the condition is satisfied, then control passes into the body of the if-statement. If it is not then the control passes to the line after the end statement of the if-statement. Control does not pass back to the first line of the if-statement, unlike the for or while loops. Let’s see what exactly happens in if-statement in the example program above. Loops: For-loop Often we are required to tasks in a repetitive style. For example: You want to double the value of each element in a vector. We can then use a so-called for-loop. Take a look at the program below: %%%%%%%%MATLAB Program for a “for-loop” a=[4,6,-3,5,6.7]; %line 0 for count=1:5 % line 1 % for loop to double each element of the array a(count)=a(count)*2; % line 2 end % line 3 ******End of for loop Notice that in line 0, a is assigned. In line 1, you see a statement that is not an assignment statement. It is the first statement in the so-called for loop. It sets the variable count to 1 (since count=1:5). Thereafter it is checked whether the value of the variable count, (which is 1 at this stage) exceeds 5. If it does not, the next line (line 2) is then executed. If it does, then the control passes to the line after line 3 ie control exits from the for-loop. What happens in the line 2 is actually as follows: a(1) gets a value of a(1)* 2 ie 4*2=8. In other words, a(1) is now 8. The control then passes to the next line (line 3) where there is an end statement. This statement ie the end statement passes the control back to the first line in the for-loop (ie line 1). This is the way the for- loop works. Now automatically in line 1 of the for-loop, the variable count is incremented by 1. Once again, it is checked if the variable count (currently 2) exceeds 5. If it does, control passes to the line after line 3, else control goes into the loop at line 2. What happens in line 2 is actually as follows: a(2) gets a value of a(2)*2 ie 6*2=12. In other words, a(2) is now 12. The control then passes to the next line (line 3) where there is an end statement. The end statement passes the control back to the first line in the for loop (ie line 1). Here the variable count is again incremented by 1 and the checking process described above is carried out again. In this way, the for-loop causes the operation in line 2 to be carried out for the entire array. When count becomes 6 in the line 1, the control passes to the line after line 3. While-loop In a for-loop, as we saw above, the indexing variable (count in the example above) is incremented by 1 every time the control goes to the first statement in the for-loop, except the first time. However, if we wish to increment the indexing variable by 2, we have to use another variable inside the loop and define the variable as having a value equal to two times the indexing variable. See the program below that achieves this: a=[8,16,-3,5,6,17]; for count=1:3 % for loop to double every alternate element of the array count1=count*2; a(count1)=a(count1)*2; end An alternative way to do this is to use a loop that lets us control the increment directly. The “while loop” provides us with that mechanism. Also sometimes, we are not sure how many times a loop needs to run before we are done with our desired operations. Under such circumstances the while-loop becomes really useful. a=[8,16,-3,5,6,17]; count=0; while count<=4 count=count+2; a(count)=a(count)*2; end % line 4 Lesson 3 A (Matrix manipulation) Two matrices can be multiplied by using the following syntax: C=A*B An inverse of a matrix can be found as follows: D=inv(A) Lesson 4(Using MATLAB functions) Matlab has several function such as sin, cos, sqrt etc. To know more about these type >>more on >>help And the different toolboxes that MATLAB contains will be displayed. matlab/general - General purpose commands. matlab/ops - Operators and special characters. matlab/lang - Programming language constructs. matlab/elmat - Elementary matrices and matrix manipulation. matlab/elfun - Elementary math functions. matlab/specfun - Specialized math functions. matlab/matfun - Matrix functions - numerical linear algebra. matlab/datafun - Data analysis and Fourier transforms. matlab/polyfun - Interpolation and polynomials. Then you may type >>help elfun and the different functions in elfun toolbox will be displayed. You may also type help and then the name of some particular function. >>help sin And the format for use of that function will be displayed. SO in your program you could type: A=1.2; B=sin(A); And B will get the value of the sine of A (A is in radians). There are several other functions in MATLAB and this is the most attractive feature of MATLAB. You could 3x + 5y – 4x + 3y + -x + 4y + solve a linear equation of the form Ax=b: 9z = 12 2z = 65 7z = -13 by defining the two matrices A and b and then typing x=A\b; The required Matlab code is as follows: A=[3,5,-9;4,3,2;-1,4,7]; b=[12;65;-13]; x=A\b; x Lesson 5 (Writing your own functions) You can create your own functions by creating an M-file ie a file with a .m extension, which is actually a function. Let’s say you want to write a function that takes an array of 10 elements and creates two new arrays The first new array’s elements have values which are two times those in the original array. The other new array has elements that have values, which are 3 times those in the original array. Then your main file should look something like this: a=[1,2,3,4]; [b,c]=multiplier(a); Notice that multiplier is a function that you are going to write. It has one input (a function can have more than one input, the different inputs are separated by commas) and two outputs. The inputs are within round brackets and the outputs in square brackets. The input, here, is the array a. The name of the array is sufficient to pass the whole array into the function. The outputs are two arrays: b and c. If you want to pass a scalar quantity as input, the method is similar. The function has to be written in a different file the name of which should be multiplier.m (ie whatever name you’ve given to your function). And the file multiplier.m is as follows: function [b,c]=multiplier(a) for count=1:3 % creates an array b and c as per requirement b(count)=a(count)*2; c(count)=a(count)*3; end Notice the first line has the reserved word function. Any function file must begin in this way. And the syntax of the function definition (function [b,c]=multiplier(a)) should be identical to that in the main file where the function is called ([b,c]=multiplier(a)). Notice a,b,c in the function file are local variables and you may use different names if you like.