# MATLAB Tutorial: Part1

Finite Element Analysis

## MatLab Tutorial Summary/Explanation

MatLab is developed by the MathWorks Company. Below is their link for further information outside the scope of the document below.
About MatLab: MatLab is a matrix-based enginering computation program that is user-interactive. By using matrices to enter and solve complex equations, MatLab can do in a fraction of the time it would take older programming languages to do. When dealing with finite element analysis, depending on the number of nodes, there can be numerous partial differential equations that need to be solved.
Starting Matlab: MatLab can be started 2 ways. The first is using the command window and typing run/matlab. The second is through the start menu >> programs >> matlab.
Matrices: MatLab works solely with matrices to handle all computations. These matrices can range in size from 1 by 1 all the way up to thousands of elements. Entering a matrix into matlab is easy and is done with the following notation:
Additionally, matrices do not have to be entered with just real numbers. Complex numbers (i) can also be entered into matrices. For example:
When it comes to large matrices, it may be very time consuming to enter in all the matrix values by hand. In such a case, the values can be read in from an ASCII file. As long as the file has a rectangular matrix in it, simply type: >> load numbers.ext This will load the matrix in the file numbers into the workspace.
Matrix Operations:
Statements, Expressions, and Variables:
Matlab is an expression language meaning the user types an expression, usually composed of variables, functions, and operators, and Matlab interprets it and performs the expression. After the expression is evaluated, the output is put into a matrix. If no matrix name is specified by the user, matlab puts the result in a matrix called ans. Normally after each expression is evaluated, Matlab outputs the result of the expression. To supress(hold) the output, put a semi-colon at the end of the expression. Variables can have virtually any name you choose, as long as it is not a reserved variable or function name. Matlab is case sensitive but has no restriction on variable name length.
Also, there are come commands that can be used to help the user build matrices
Loops:
Loops are used to repeat a series of operations until a certain condition is met. Below are the two types of loops that are used frequently in Matlab.
For:
For loops execute continually once a condition is met up until the condition is no longer met.
While:
While loops are similar to for loops except for while loops are guaranteed to execute at least once. It will exectue the statement, then check it against the while statement. If it is true, it will run back through the loop. If it is false, it will exit the loop.
In addition to loops, one can have an operation performed only IF a condition is met. That type of operator is an if statement. Below is an example of an IF statement.
NOTE: All code was taken from my previous homework assignments in Numerical Methods
Relations:
The relational operators are used to compare the value of a variable to another variable or to a constant value. Below are some common relational operators:
Relational operators are always used in for, while,and if statements to see when to execute, or stop executing expressions within the loop.
Scalar Functions:
Certain matlab functions operate on scalar numbers and when applied to a matrix, they act on 1 matrix element at a time. Some scalar Matlab functions are listed below: sin, cos, tan, asin, acos, atan exp, log, rem, abs, sqrt, sin, round, floor, ceil
Vector Functions:
Vector functions act on a vector matrix column by column to produce a row matrix as the solution. These functions can only be used when the matrix is larger than 1 by 1. Below are some examples of vector functions: max, min, sort, sum, prod, median, mean, std, any, all
Command Line Editing:
The command line in Matlab is denoted by 2 arrows (>>). The command line is the line that the user uses to type in commands. To keep the user from having to re-enter previously used commands, Matlab allows the user to scroll through the old commands used during the session by hitting the up-arrow and down-arrow keys.
Also, it is important to remember that instead of entering a series of commands by hand every time, create an m-file to execute. This will save loads of time and needless typing.
M-Files
M-files are special Matlab files that have a .m extension. These files contain a series of statements that can be exectued at one time, thus saving the user time from having to exectue expressions 1 at a time. There are two types of m-files, script files and function files.
Script files contain a series of normal Matlab scripts. They are traditionally used to enter large amounts of data into matrices. Any variable or matrix is global, meaning the parameter can be used in any other portion of the workspace.
Function files are a type of m-file that allow users to essentially create new Matlab functions. The functions can be specific to a particuar problem that the user is working on.
Text Strings
You can assign text strings to variables by using single quotes around the text to be entered into the variable. Additionally, text can be displayed to the screen using the disp function. The error function also outputs a message to the screen but also aborts the code in an m-file when it is executed. Below are examples of the 2 functions: disp('Hello World') error('Warning!!!')