# 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:

A = [1 2 3; 4 5 6; 7 8 9]
This creates a 3 by 3 matrix with the values shown and a new row started after each semi-colon.

Additionally, matrices do not have to be entered with just real numbers. Complex numbers (i) can also be entered into matrices. For example:

A = [1+3i; 2-4i; 3]

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:

MatLab uses the same basic mathematical operators plus a few additional ones. Below are the operators:
* - subtraction
* * multiplication
* / right division
* \ left division
* ^ power
* ` conjugate
The left division operator is special in that it allows for division of matrices that are not square. If you use the right
division operator, MatLab will give an error saying that the matrices are not square.

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.

* whos - displays a list of variables currently in the workspace

Also, there are come commands that can be used to help the user build matrices

Matrix Building Commands:
* eye - identity matrix
* zeros - matrix of zeros
* ones - matrix of ones
Each of the commands needs to be followed by the following notation: (# of rows, # on columns)
Example: zeros(3,3) yeilds a square 3 by 3 matrix of zeros.

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.

function problem_1_37

% This function will plot 3 conditional functions

dx = 0.1;
x = [-2:dx:6];
for k = 1:length(x)
if x(k) >= 5
y(k) = 10*(x(k)-5) + 1;
elseif x(k) >= -1
y(k) = 2 + cos(pi*x(k));
else
y(k) = exp(x(k)+1);
end
end

plot(x,y), xlabel('Time x (seconds)'), ylabel('Height y (kilometers)')''

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.

function e = macheps()

% This function determines the smallest number that can be added to 1 in
% Matlab and still get a number greater than 1.

e = 1;
while (1+e >1)
e = (e/2);
end

e = e*2;''

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.

function y = problem_1_36(x)
% This function will check conditional statements to evaluate a function given different input values.
if x >= 5
y = 10*(x-5)+1;
elseif x >= -1
y = 2+cos(pi*x);
else
y = exp(x+1);
end

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:

* == equal
* ~= not equal
* < less than
* > greater than
* >= greater than or equal to
* <= less than or equal to
* & and
* | or
* ~ not

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!!!')