Announcements | Syllabus | Schedule | Weekly lecture notes | Assignments | Links |

Matlab is a program that has been developed for the general solution of mathematical equations, data processing, and data display. MATLAB is an acronym for MATrix LABoratory and it is a highly optimized numerical computation and matrix manipulation package. It is an excellent tool for developing numerical models of problems, performing mathematical operations on large data sets, displaying data in one, two, three, or four dimensions, and solving simultaneous equations. In general, Matlab has been developed as a numerical tool and so best works on real numbers, rather than symbolic operations.

Matlab is a product of The Mathworks. Cleve Moler is the inventor of Matlab and a founder of Matlab.

In short, if you can write an equation that you think represents a relationship between two or more variables and need to graph your results, Matlab is the tool to use. First, we will go through a short introduction on Matlab concepts, entry formats, graphics, M-files, and functions.

Copy and paste this into the Matlab command line: `playbackdemo('GettingStartedwithMATLAB', 'toolbox/matlab/demos/html')`

Video demonstration:

Each version of Matlab (Mac/Windows/Linux) has a similar layout. There are only 3 elements in this window I use on a regular basis:

- the
**Command Window** - the
**Workspace**window - the
**Current Directory**line - the
**Editor**window.

NOTE:Formultiplication,division, andexponents, remember to use the dot (.) BEFORE the math symbol. This is not necessarily needed all the time, but is good practice to use it anyway to avoid problems with vector and matrix operations in the future.

>> 4 + 7 ans = 11

>> 48 - 8.2 ans = 39.8

>> 4.53 .* 6 ans = 27.18

>> 200 ./ 50 ans = 4

>> 4.1 .^ 3 ans = 68.921

>> sin(0.8) ans = 0.71736

>> sind(90) ans = 1

>> acos(0.567) ans = 0.96794

>> acosd(0.5) ans = 60

>> sqrt(16) ans = 4

>> exp(2.3) ans = 9.9742

>> log(456384) ans = 13.031

>> log10(10000) ans = 4

>> abs(-31.45) ans = 31.45There are a few special numbers that are important to know. Some you will use all the time, some not so much.

>> pi ans = 3.1416

>> i ans = 0 + 1i

>> NaN ans = NaN

>> inf ans = Inf

>> eps ans = 2.2204e-16

The next thing we need to do is start combining these operations within one line.

>> 2 .* pi ./ 360 .* 6371 .* sind(90 - 43.8) ans = 80.256

Here is how I would re-type the above equation:

>> (2 .* pi ./ 360 .* 6371 .* sind(90 - 43.8) ??? (2 .* pi ./ 360 .* 6371 .* sind(90 - 43.8) | Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.Oops! I forgot to close a parenthesis. Let's try it again.

If you type in a complex expression and make an error, you can edit the previous expression by pressing the Up arrow on your keyboard to recall the last line. Press the Up Arrow or Down Arrow several times to recall other recent command lines. Use the Left and Right Arrow to move through the command and insert numbers or text or you may also delete something.

>> ((2 .* pi) ./ 360) .* (6371 .* sind(90 - 43.8)) ans = 80.256This time I grouped the parts of the equation that make sense. For full disclosure, this particular equation is for converting degrees of Longitude to kilometers around a given line of Latitude.

ans = 80.256Matlab is assigning your answer to a default variable called ans. Everytime you do a calculation in Matlab, ans is replaced in the

Variables allow you to store information within the Matlab environment to use it at a later time. Here is how you can assign something to a variable:

>> kilometers = ((2 .* pi) ./ 360) .* (6371 .* sind(90 - 43.8))You will notice that Matlab has been echoing your output by saying:

kilometers = 80.256Now we can use this variable in another equation. Lets just say we want to convert the kilometers to miles.

>> miles = kilometers .* 0.62137 miles = 49.869

If you do not wish to see a confirmation of your assignment, include a semicolon after the operation. This rule holds for all assignments or operations-- if you do not want to see the result of the operation, but simply wish to store it in another variable, use a semicolon:

>> kilometers = ((2 .* pi) ./ 360) .* (6371 .* sind(90 - 43.8));

- HAS TO start with a letter (438km = 80.256 - illegal!)
- CAN use numbers WITHIN the name (km438 = 80.256 - ok, but not recommended)
- CANNOT have punctuation in a name. Periods have another use. (43.8km = 80.256 - doubley illegal!)
- DO NOT use function names or operation names as variable names (i, sin, pi, nan - all BAD choices)
- Choose a convention that works for you, and STICK WITH IT
- Here are some of my rules:
- Be Descriptive (Radius = 6371;)
- Use Mixed Case (NewVp = 9.7;)
- Avoid Underscores (New_Vp = 9.7;)
- More to come as we get into programming

>> RowVector = [1 2 3 4 5 6 7 8 9 10]; >> RowVector RowVector = 1 2 3 4 5 6 7 8 9 10

>> ColumnVector = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]; >> ColumnVector ColumnVector = 1 2 3 4 5 6 7 8 9 10

>> Matrix = [1 2; 3 4; 5 6; 7 8; 9 10]; >> Matrix Matrix = 1 2 3 4 5 6 7 8 9 10

>> Matrix = Matrix .* 2; >> Matrix Matrix = 2 4 6 8 10 12 14 16 18 20

Video demonstration of simple matrix math:

Video demonstration of how to build a vector with elements separated by a constant increment:

>> RowVector(3) ans = 3 >> ColumnVector(4) ans = 4

>> Matrix(3,2) ans = 6

NOTE:Rowsare referenced first, thencolumns.

>> Matrix(3,:) ans = 5 6

- ALL scalars, vectors, and matrices are considered
**arrays**of different sizes. - scalars: 1 row, 1 column
- vectors: 1 row, mutliple columns OR multiple rows, 1 column
- matrices: multiple rows, multiple columns
- Brackets, [], used on either end to denote an array
- Separate columns with "white space"
- Separate rows with semicolon, ;
- Rows before Columns
- Colon, :, used to reference entire row or column

>> Name = 'My name is Kevin'; >> Name Name = My name is KevinThis is a 16-element array.

- Save the file from the browser to the local hard disk as "SRDischarge.txt".
- Create a directory on MyFiles called "glg410_matlab".
- Move file from local hard disk to this directory on the MyFiles.

Video demonstration:

>> load('SRDischarge2008.txt','-ascii');The first argument in the

The file has 2 columns of data. It loads into Matlab as a 366 x 2 matrix with the name of the file as the new variable name. (NOTE: This only works if the text file has ONLY numeric data! No headers.)

Column 1: days of the year (1-366 for 2008)

Column 2: discharge (cubic feet per second)

>> min(SRDischarge2008(:,2)) ans = 0

>> max(SRDischarge2008(:,2)) ans = 5900

>> mean(SRDischarge2008(:,2)) ans = 5900

>> plot(SRDischarge2008(:,1),SRDischarge2008(:,2));The first argument is the

This plot isn't very interesting though. (1) There is no title, so you don't know what information it is trying to convey. (2) The axes are not labeled, so you have no idea what the numbers are telling you. (3) Points are connected by a line. Is this the best way to show the data?

A few more useful commands AFTER you have plotted your data can really give the graph a boost. Here are some examples:

>> title('Salt River Discharge at Priest Dr. - 2008');

>> xlabel('Day of Year');

>> ylabel('Daily Discharge (cfs)');Now what do we have.

Saving a graph to a JPEG is very helpful for placing this graph in the context of a report (or maybe your homework). To save a figure, you will use theNOTE:You can also play around with the plot using buttons and functions within theFigure Window.

>> print('discharge.jpg','-djpeg');The first argument is the

>> saveThe default filename for your saved workspace is "matlab.mat". If you want to save it under a different name, you add that name as an argument to the function.

>> save('lecture3.mat');

To load in a saved MAT-file, you will use theNOTE:This will ONLY save the variables in your workspace. It DOES NOT save figures or plots that you have made.

>> load('lecture3.mat','-mat');The first argument in the filename. The second argument is the format of the file you are wanting to load. For MAT-files, this is always

Last modified: September 22, 2011