User:Eml4500.f08.jamama.mueller/Matlabtutorial

Relations

Many of the relational operators used in MATLAB are used very similar to the operators used in C++ to compare variables. The advantage to using MATLAB for relational operators is when matrices are being used. In a programming language you would need to write a for loop when comparing matrices instead of just a single line of code.

When using these statements, the line of code will return a 0 or 1 representing false and true respectively.

Scalar Functions

Many functions in MATLAB use matrices but some operate on each element in the matrix called scalar functions.

Vector Functions

Vector functions in MATLAB operate on each column of the matrix. So if A is a 3X3 matrix min(A) would return a 1X3 matrix with each value the minimum of their column. If the matrix is only one row but multiple columns such as the return from the min(A) function, the minimum of the row is found and returned by the function. So if the user wanted to find the minimum of a matrix, min(min(a)) should be used so the matrix can be of any size and the desired effect is achieved.

M-files

Script Files

Script files consist of normal MATLAB commands in an m-file. To run a script in the working directory in MATLAB, simply type the name of the m-file and all of the lines in the script file will be run. Script files are very helpful when tedious data entry is required.

Function Files

This allows users to create their own custom functions in MATLAB if the existing functions are not enough or if the user would like to modularize their MATLAB program. All variables are local by default (only seen within the function) but can be made global (able to be seen by all functions) by using the global command.

Text string, error messages, and input

In MATLAB variables can be set to string values using single quotes. Also, display messages and error messages for MATLAB programs use the same way of defining string values. Input from users can also be taken in string format and saved in a string type variable.

Managing M-files

Comparing the efficiency of algorithms in MATLAB

When using MATLAB there are many different ways to get to the same result in your program. The program can be made more efficient by changing the way a certain operation is completed for the faster and more efficient way. This can be discovered using flops and the tic toc feature.

Output Format

All of the calculations in MATLAB are done in double precision, but sometimes for certain programs the data needs to be formatted. For instance, if the value returned be a function is an amount of money it only makes sense to show it as 2 decimal places.

17. Hardcopy

The list of commands can be written to a file using the diary command. Diary can be turned off by typing diary off and then typing diary on when done.