1 Matlab Tutorial 1- What is Matlab? Matlab is a powerful tool for almost any kind of mathematical application. It enables one to develop programs with a high degree of functionality. The user can write programs, scripts or interface with the program directly calling various math functions. Matlab is more than a glorified calculator. It has powerful plotting capabilities that allow one to plot 2D and 3D graphs, contour plots, vector plots, slices etc. 2- Basic Matlab Matlab interfaces to the user through a scripting window. In essence, one types commands into the Command Window. The Matlab engine then interprets these commands and executes the appropriate function(s). For example, entering: >> var1 = 2 var1 = 2 >> var2 = var1 + var1^2 var2 = 6 In this case in the first line the user asks Matlab to create a new variable var1 and assign it a value of 2. Next, another variable is created var2 and an expression involving var1 is evaluated and its value is assigned to the newly created variable. The value assigned to a variable can be checked by simply typing in the variable name: >> var2 var2 = 6 Let s look at something a bit more useful. One of Matlab s strong points is its ability to deal with matrices of any dimension. Almost all fields of science use arrays. They are
2 very convenient and Matlab simplifies the handling of matrices greatly. For example, let s create three simple arrays: >> A = [ 1 5; 3 6] A = 1 5 3 6 >> B = [3 5.5 1.1; 4 1.2 1.33333] B = 3.0000 5.5000 1.1000 4.0000 1.2000 1.3333 >> X = [0 2; 4-1] X = 0 2 4-1 Three variables are created (A, B and X) which are arrays. Arrays can be created by the use of square brackets and semi-colons. The opening square bracket tells Matlab that one is defining an array. A space between numbers means that they are in adjacent columns, while a semi-colon means the end of a row. One can check the size of an array: >> size(b) ans = 2 3 This means that array B is an array with 2 rows and 3 columns. One can calculate the product of two matrices: >> C = A*B C = 23.0000 11.5000 7.7667 33.0000 23.7000 11.3000
3 In this case a variable C was created that was the product of the matrices A and B. Of course, if the inner dimensions of the two matrices don t agree then Matlab complains, for example: >> C = B*A??? Error using ==> * Inner matrix dimensions must agree. What if one wants to multiply each element in an array with the corresponding elements in another array: >> Y = A.*X Y = 0 10 12-6 The. in front of the operator tells Matlab to apply the operator * between corresponding elements. This holds for other operators. The transpose of a matrix is easily calculated: >> T = A' T = 1 3 5 6 As is the inverse: >> P = inv(t) P = -0.6667 0.3333 0.5556-0.1111 To access an element in an array, all one needs to do is designate the column and row index: >> A(1,2) ans = 5 To access several elements one uses the : operator. For example to extract the first row in array X and assign it to a new variable D one does: >> D = X(1,:)
4 D = 0 2 In this case all the columns in array X are called using the : operator. One can also access a set of indices, for example: >> D = C(1,1:2) D = 23.0000 11.5000 In this example only columns 1 and 2 of row 1 are retrieved from array C. The : operator is also useful in defining arrays. For example, to obtain a vector that starts at 0 and increases in 0.5 steps to a value of 5: >> t = [0:0.5:5] t = Columns 1 through 10 0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 Column 11 5.0000 The first value specifies the start value, the second is the increment and the third is the maximum value. By not entering an interval, Matlab assumes it to be equal to 1. One can reference the last element in and index by using the reserved word end, for example, to access elements 3 and on in vector t we use: t(3:end) This and many other basic features are well documented. To get started, check out the following website: http://math.ucsd.edu/~driver/21d-s99/matlab-primer.html Also, Matlab itself has a lot of documentation. Help on a specific function can be accessed by typing help and then the name of the function. Also by typing helpwin a dialog window pops up with a list of all the functions available. By typing helpdesk, Matlab will access the Mathworks website via the Netscape browser.
5 2.1 Plotting in Matlab Plotting in Matlab is extremely easy and at the same time offers a great deal of control to the user. One can alter any aspect of the plot by accessing its properties. In this section, the basic Matlab plotting tools are presented. Let s plot a sine wave. First, the values that we wish to plot must be generated. First the x-values must be created. >> x = [0:0.1:2*pi]; Here we are defining a vector with values that go from 0 to 2π (one period) with a step of 0.1. Note that arithmetic operations are allowed in the definition of arrays as long as they result in real scalars. Now let s calculate the value of the sine wave at each value of x: >> y = sin(x); We passed a vector to the sin function. This is possible with most Matlab functions, simplifying our task. Without this capability, one would have to loop through the vector as in regular programming languages. Now let s plot the function. For basic 2D graphs, the function plot is used: >> plot(x,y) The plot functions plots x vs. y. x and y must be vectors of the same length. The result is:
6 Let s label the graph: >> xlabel('x'); >> ylabel('y = sin(x)'); >> title('plot of sine wave'); If we again call the function plot, the old plot is destroyed, along with labels, title etc. and a new plot is created. To create a new figure one uses the command figure. At this point the new figure is now the focus and all plot command are directed towards it. One might want to have several plots on one pair of axes. For example, close all the figures and type: >> v = cos(x); >> plot(x,y); >> hold on; >> plot(x,v,'r'); In this instance, a sine wave is plotted as before and then Matlab is told to hold the figure. Then a cosine wave is plotted in red. This is accomplished by the third argument in the
7 plot command. This third command (if used) must be a string. It specifies the colour and/or the line type and/or the symbols to be used. The result is shown. See the documentation for the different available options. 3- Two Dimensional Data In Computational Thermofluids, post processing data properly is crucial. The first step is of course to set up a two dimensional field on which to process the data. First we need a mesh. We will be using the function meshgrid. >> [X Y] = meshgrid(-5:5:5,-5:5:5) X = -5 0 5-5 0 5-5 0 5 Y = -5-5 -5 0 0 0 5 5 5 Here we have created a simple 3x3 mesh. The array X holds the x-values for each node point while the array Y holds the y-values. We want our domain to stretch from x=-5 to x=5 in steps of 0.1 and from y=-5 to y=5 also in steps of 0.1: [X Y] = meshgrid(-5:0.1:5,-5:0.1:5); This returns two arrays (X and Y). Each one is 101 by 101 and contains the x and y coordinates of each node respectively. We can now use this domain to calcluate temperatures and fluxes etc, as in class and the first assignment.