User:Eml4500.f08.Lulz.Layton.Eric/Hw1

=Homework 1= University of Florida

Department of Mechanical and Aerospace Engineering

Fall 2008

EML4500: Finite Element Analysis and Design

Team Name: Lulz

Group Members:
 * Aaron Fisher
 * Eric Layton (Group Coordinator)
 * Sam Miorelli
 * Ben Mitchell
 * John Saxon
 * Andrew Strack

Submitted: Friday, September 12, 2008

Part 1: Accessing MATLAB
The first step is to open MATLAB by selecting the MATLAB icon. This is done by installing MATLAB on the local drive.

Part 2: Entering matrices
MATLAB works with rectangular matrices. Matrices can be entered into MATLAB using several different methods. Below is one method in which a matrix can be entered:

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

A =

1    2     3     4     5     6     7     8     9

Below is another method in which a matrix can be entered:

>> A = [1 2 3 4 5 6       7 8 9]

A =

1    2     3     4     5     6     7     8     9

The elements of a matrix can be separated by a comma or a space. When entering a number in exponential format spaces should be avoided (e.g. 2.34e-9). Complex numbers can also be entered into MATLAB. Blank spaces should also be avoided when entering complex numbers, and either i or j can be entered as the imaginary number.

When entering large matrices, it is often easier to enter these numbers in a text editing program. Several built-in functions exist in MATLAB such as rand(n) which allows the user to create an n x n matrix with randomly generated entries that distributed between 0 and 1. Individual entries can be referenced with parentheses. Using an example A(2,3) means the entry in the second row, third column of matrix A. Another example would be x(3), which denotes the third coordinate of vector x.

Part 3: Matrix operations, array operations
MATLAB is capable of addition, subtraction, multiplication, powers, conjugate transposes, left division, and right division. These operations apply to scalars as well. An error message will occur if the sizes of the matrices do not match up. Division is a special case. When matrix A is an invertible square matrix and b is a compatible column, resp. row, vector, then the following is true:

x = A\b is the solution of A * x = b and x = b/A is the solution of x * A = b

Array operations are also relevant in MATLAB. Matrix operations, which include *, ^, \, and /, are made to operate by inserting a period directly afterwards. For example,

>> [1,2,3,4].* [1,2,3,4]

ans =

1    4     9    16

>> [1,2,3,4].^2

ans =

1    4     9    16

Part 4: Statements, expressions, variables; saving a session
MATLAB uses statements as you type since it is an expression language. This is in the form:

variable = expression, or just expression

Expressions are made of operators, functions, and variable names. Several statements can be placed on the command line if it is separated by commas or semicolons. Commands, functions, and variables are case sensitive. For example, solveUT is not the same as solveut. The command who or whos lists all the variables that are currently being used in the workspace. The command variablename can be issued to clear a variable from the workspace.

There is a permanent variable called eps (epsilon) that give a unit roundoff in MATLAB which is equal to about 10^-16 on most machines. A infinite loop can be stopped by CTRL-C on a laptop or CTRL-BREAK on a PC. When logging out or exiting MATLAB all variables are considered lost. A non-human-readable file can be created to save all variables written in the command window called matlab.mat. When restarting MATLAB, all variables saved before will automatically be loaded.

Part 5: Matrix building functions
There are many matrix building functions in MATLAB. These include eye, zeros, ones, diag, triu, tril, rand, hilb, magic, toeplitz.

Zeros(m,n) produces an m-by-n matrix of zeros and zeros(n) produces an n-by-n matrix.

>> zeros(5,4)

ans =

0    0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0

>> zeros(3)

ans =

0    0     0     0     0     0     0     0     0

Another example is if A is a matrix, then zeros(size(A)) will produce a matrix of zeros having the same size as A.

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

A =

1    2     3     4     5     6     7     8     9

>> zeros(size(A))

ans =

0    0     0     0     0     0     0     0     0

When x is a vector, the function diag(x) is a diagonal matrix with x down the diagonal of the matrix. When A is a square matrix, then the function diag(A) is vector that has A down the diagonal.

>> A = [ 1 2; 3 4]

A =

1    2     3     4

>> diag(diag(A))

ans =

1    0     0     4

In MATLAB, matrices can also be built from blocks. If A is a 3-by-3 matrix, then

B = [A, zeros(3,2); zeros(2,3), eye(2)]

should build a 5-by-5 matrix.

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

A =

1    2     3     4     5     6     7     8     9

>> B = [A, zeros(3,2); zeros(2,3), eye(2)]

B =

1    2     3     0     0     4     5     6     0     0     7     8     9     0     0     0     0     0     1     0     0     0     0     0     1

Part 6: For, while, if - and relations
MATLAB handles most common computer code concepts like any other modern computer programming language. This includes for and while loops, if statements, and relational conditions.

For
for loops can be used very effectively in MATLAB to create matrices.

Ex 1: If you wanted to create a series of vectors where each value in the vector is the square of the index value of the position of the value in the vector, you could use this for loop:  EDU>> x = []; for i = 1:3, x=[x,i^2], end x =

1 x =

1    4 x =

1    4     9 The expression x = []; i = 3:-1:1, x=[x,i^2], end will give the same thing, except in reverse.

Ex 2: for loops can also be used to produce the Hilbert matrix. (Coding note, the semicolon prevents an intermediate printing which would be undesirable and the final H tells MATLAB to print the resulting H matrix.)  for i = 1:3 for j = 1:3 H(i,j) = 1/(i+j-1); end end H H =

1.0000   0.5000    0.3333    0.5000    0.3333    0.2500    0.3333    0.2500    0.2000

While
A while loop executes some command repeatedly so long as the condition it checks remains true.

Ex: MATLAB can calculate for a given number a, the smallest value n such that 2^n is less than or equal to a:  n = 0; a = 12; while 2^n  if n<0 parity = 0; elseif rem(n,2) == 0 parity = 2; else parity = 1; end

Relations
MATLAB uses the following relational operators in its programming language: < less than > greater than <= less than or equal to >= greater than or equal to == equal ~= not equal & and ~ not
 * or

Part 7: Scalar functions
MATLAB functions that are designed to operate on scalars can also be applied to matrixes, they will simply do so element-wise on the matrix elements. These are some common MATLAB functions that work in such a manner: sin, cos, tan, asin, acos, atan, ex, log, rem, abs, sqrt, sign, round, floor, ceil

Part 8: Vector functions
MATLAB functions that are designed to operate on vectors can also be applied to M x N matrixes (M >= 2), they will simply do so column-by-column which will produce a row vector containing the results of their operation in each column. To get these functions to operate on a row-by-row basis, use the transpose of the matrix. Common MATLAB functions that work like this are: max, min, sort, sum, prod, median, mean, std, any, all

These functions can also be nested within each other. Ex: to get the maximum single value in matrix A, use max(max(A)).

Part 9: Matrix functions
MATLAB has many matrix functions automatically built in for ease of use. These dramatically simplify programming and creating useful results from the software. These are the most useful of the functions: eig - Eigenvalues and Eigenvectors chol - Cholesky factorization svd - single value decomposition inv - inverse lu - LU factorization qr - QR factorization hess - Hessenberg form schur - Schur decomposition rref - reduced row echelon form expm - matrix exponential sqrtm - matrix square root poly - characteristic polynomial det - determinant size - size norm - 1-norm, 2-norm, F-norm, infinity-norm cond - condition number in the 2-norm rank - rank

As in other MATLAB functions, these can be used several ways. Ex: y = eig(A) and simply inputting eig(A) produce the same results. The output can also go to another matrix, [U,D] = eig(A) will produce a matrix with columns as the eigenvalues of A and a diagonal matrix D with the eigenvalues on the diagonals. This all may seem elementary, the point is that if there is a desired form of an output in MATLAB for a function, it is likely possible given the proper syntax.

Part 10: Command line editing and recall
The command line in MATLAB can be edited like most other text editors (usin up and down arrows, home and end keys, etc.). Full details of how the command line can be edited will be printed from the help cedit command.

Previous commands can be accessed using the up and down arrows on the command line. This is frequently more efficient than producing a full M-file for small problems. Commands may also be placed sequentially on the same command line using semi-colons to separate the commands.

Part 11: Submatrices and colon notation
This section of the primer discusses more advanced matrix manipulation techniques.

 >> 0.2:0.2:1.2
 * The first example shows the use of non-integral increments and boundaries in matrix declaration:

ans =

0.2000   0.4000    0.6000    0.8000    1.0000    1.2000 >> 5:-1:1

ans =

5    4     3     2     1  >> x = [0.0:0.1:2.0]';
 * The next example employs a table of  values to demonstrate matrix generation from a set of vectors:

>> y = sin(x);

>> [x y]

ans =

0        0

0.1000   0.0998

0.2000   0.1987

0.3000   0.2955

0.4000   0.3894

0.5000   0.4794

0.6000   0.5646

0.7000   0.6442

0.8000   0.7174

0.9000   0.7833

1.0000   0.8415

1.1000   0.8912

1.2000   0.9320

1.3000   0.9636

1.4000   0.9854

1.5000   0.9975

1.6000   0.9996

1.7000   0.9917

1.8000   0.9738

1.9000   0.9463

2.0000   0.9093  >> A = rand(5)
 * The following set of examples display the various uses of the colon operator in MATLAB:

A =

0.8147   0.0975    0.1576    0.1419    0.6557

0.9058   0.2785    0.9706    0.4218    0.0357

0.1270   0.5469    0.9572    0.9157    0.8491

0.9134   0.9575    0.4854    0.7922    0.9340

0.6324   0.9649    0.8003    0.9595    0.6787

>> B = rand(5)

B =

0.7577   0.7060    0.8235    0.4387    0.4898

0.7431   0.0318    0.6948    0.3816    0.4456

0.3922   0.2769    0.3171    0.7655    0.6463

0.6555   0.0462    0.9502    0.7952    0.7094

0.1712   0.0971    0.0344    0.1869    0.7547 >> A(1:4,3)

ans =

0.1576

0.9706

0.9572

0.4854 >> A(:,3)

ans =

0.1576

0.9706

0.9572

0.4854

0.8003 >> A(1:4,:)

ans =

0.8147   0.0975    0.1576    0.1419    0.6557

0.9058   0.2785    0.9706    0.4218    0.0357

0.1270   0.5469    0.9572    0.9157    0.8491

0.9134   0.9575    0.4854    0.7922    0.9340 >> A(:,[2 4])

ans =

0.0975   0.1419

0.2785   0.4218

0.5469   0.9157

0.9575   0.7922

0.9649   0.9595 >> A(:,[2 4 5]) = B(:,1:3)

A =

0.8147   0.7577    0.1576    0.7060    0.8235

0.9058   0.7431    0.9706    0.0318    0.6948

0.1270   0.3922    0.9572    0.2769    0.3171

0.9134   0.6555    0.4854    0.0462    0.9502

0.6324   0.1712    0.8003    0.0971    0.0344 >> A(:,[2,4]) = A(:,[2,4])*[1 2;3 4]

A =

0.8147   0.5232    0.1576    0.7626    0.6557

0.9058   1.5438    0.9706    2.2440    0.0357

0.1270   3.2941    0.9572    4.7567    0.8491

0.9134   3.3341    0.4854    5.0838    0.9340

0.6324   3.8434    0.8003    5.7677    0.6787  >> x = A(:,1)
 * The final set of examples for this section explores reordering of vectors using both colon notation and the  and   commands.

x =

0.8147

0.9058

0.1270

0.9134

0.6324 >> x = x(5:-1:1)

x =

0.6324

0.9134

0.1270

0.9058

0.8147 >> y = fliplr(x)

y =

0.6324

0.9134

0.1270

0.9058

0.8147 > y = flipud(x')

y =

0.6324   0.9134    0.1270    0.9058    0.8147

Part 12: M-files: script files, function files
This section of the primer discusses MATLAB's M-file system for executing command sequences. MATLAB handles two types of M-files: scripts and functions.

Script files
 First, the MATLAB code saved as : A = [
 * The first example shows the use of a simple script for creating a matrix:

1234

5678

];

And then the execution of the script to create the matrix:

>> data

>> A

A =

1234

5678

Function files
 First, the MATLAB code saved as the function : function a = randint(m,n,a,b)
 * The next example employs a function to generate a n-by-m matrix of random numbers from 0 to 9:

%RANDINT Randomly generated integral matrix.

%   randint(m,n) returns and m-by-n such matrix with entries

%   between 0 and 9.

%   rand(m,n,a,b) return entries between integers a and b.

if nargin < 3, a = 0; b = 9; end

a = floor((b-a+1)*rand(m,n)) + a;

And the calling of the function to generate a 4-by-5 matrix: >> z = randint(4,5)

z =

2    1     5     5     5

6    4     2     6     1

6    9     7     8     1

1    3     2     9     2

 First, the MATLAB code saved as : function [mean, stdev] = stat(x)
 * The next function highlights the capability to have multiple outputs:

% STAT Mean and standard deviation

%   For a vector x, stat(x) returns the mean of x;

%   [mean, stdev] = stat(x) both the mean and standard deviation.

%   For a matrix x, stat(x) acts columnwise.

[m n] = size(x);

if m == 1

m = n; % handle case of a row vector

end

mean = sum(x)/m;

stdev = sqrt(sum(x.^2)/m - mean.^2); Next, two function calls are made to. The first displays the aforementioned multiple outputs: >> [xm, xd] = stat(x)

xm =

0.6786

xd =

0.2938 And the second displays a single output (no square bracket required): >> xm = stat(x)

xm =

0.6786 Lastly, the  command is used to display the header for  : >> help stat

STAT Mean and standard deviation

For a vector x, stat(x) returns the mean of x;

[mean, stdev] = stat(x) both the mean and standard deviation.

For a matrix x, stat(x) acts columnwise. 
 * The last function examples highlight the use of the  command to pass functions to other functions:

First, the MATLAB code for the function, which finds the zero of a passed function via bisection method: function [b, steps] = bisect(fun, x, tol)

%BISECT Zero of a function of one variable via the bisection method

%   bisect(fun,x) returns a zero of the function. fun is a string

%   containing the name of a real-valued MATLAB function of a

%   single real variable; ordinarily functions are defined in

%   M-files. x is a starting guess. The value returned is near

%   a point where fun changes sign. For example,

%   bisect('sin',3) is pi. Note the quotes around sin.

%

%   An optimal third input argument sets a tolerance for the

%   relative accuracy of the result. The default is eps.

%   An optional section output argument gives a matrix containing a

%   trace of the steps; the rots are of form [c f(c)].

% Initialization

if nargin < 3, tol = eps; end

trace = (nargout == 2);

if x ~= 0, dx = x/20; else, dx = 1/20; end

a = x - dx; fa = feval(fun,a);

b = x + dx; fb = feval(fun,b);

% Find change of sign.

while (fa > 0) == (fb > 0)

dx = 2.0*dx;

a = x - dx; fa = feval(fun,a);

if (fa > 0) ~= (fb > 0), break, end

b = x + dx; fb = feval(fun,b);

end

if trace, steps = [a fa; b fb]; end

%Main loop

while abs(b - a) > 2.0*tol*max(abs(b),1.0)

c = a + .5*(b-a); fc = feval(fun,c);

if trace, steps = [steps; [c fc]]; end

if (fb > 0) == (fc > 0)

b = c; fb = fc;

else

a = c; fa = fc;

end

end

And the function call to find the zero of the sin function near x = 2: >> bisect('sin',2)

ans =

3.1416  >> type eig
 * Finally, the primer notes the usefulness of the  command to identify the nature of functions:

'eig' is a built-in function. >> type vander

function A = vander(v)

%VANDER Vandermonde matrix.

%  A = VANDER(V) returns the Vandermonde matrix whose columns

%  are powers of the vector V, that is A(i,j) = v(i)^(n-j).

%

%  Class support for input V:

%     float: double, single

%  Copyright 1984-2004 The MathWorks, Inc.

%  $Revision: 5.9.4.1 $  $Date: 2004/07/05 17:01:29 $

n = length(v);

v = v;

A = ones(n,class(v));

for j = n-1:-1:1

A(:,j) = v.*A(:,j+1);

end >> type rank

function r = rank(A,tol)

%RANK  Matrix rank.

%  RANK(A) provides an estimate of the number of linearly

%  independent rows or columns of a matrix A.

%  RANK(A,tol) is the number of singular values of A

%  that are larger than tol.

%  RANK(A) uses the default tol = max(size(A)) * eps(norm(A)).

%

%  Class support for input A:

%     float: double, single

%  Copyright 1984-2004 The MathWorks, Inc.

%  $Revision: 5.11.4.3 $  $Date: 2004/08/20 19:50:33 $

s = svd(A);

if nargin==1

tol = max(size(A)') * eps(max(s));

end

r = sum(s > tol);

Part 13: Text strings, error messages, input
This section of the primer discusses the various methods of handling text in MATLAB.

 >> s = 'This is a test'
 * The first example shows the necessity of single quotes for defining a simple text string:

s =

This is a test  >> disp('this message is hereby displayed')
 * The next example features use of the  and   commands:

this message is hereby displayed >> error('Sorry, the matrix must be symmetric')

??? Sorry, the matrix must be symmetric Note that when the  command is used, the resulting output is displayed in red. <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); width: 500px; padding: 1em;"> >> iter = input('Enter the number of iterations:  ')
 * The final example features text-based input:

Enter the number of iterations:  12

iter =

12

Part 14: Managing M-files
This section of the primer explains how to manage M-files.

Creating an M-file is done in three ways.
 * 1) From the MATLAB menu bar, select File -> New -> M-File
 * 2) Pressing Ctrl + N from the keyboard
 * 3) Right clicking in the Current Directory window and selecting New -> M-File

Three sample M-files were created to test each method: mfile.m, petals.m, fact.m. The files petals.m and fact.m are a script file and a function file, respectively, taken from tutorials in MATLAB's Help section. The code below displays the data entered into the files.

% This is an M-file. % An M-file script to produce      % Comment lines % "flower petal" plots theta = -pi:0.01:pi;               % Computations rho(1,:) = 2 * sin(5 * theta) .^ 2; rho(2,:) = cos(10 * theta) .^ 3; rho(3,:) = sin(theta) .^ 2; rho(4,:) = 5 * cos(3.5 * theta) .^ 3; for k = 1:4 polar(theta, rho(k,:))         % Graphics output pause end function f = fact(n) % Compute a factorial value. % FACT(N) returns the factorial of N, % usually denoted by N!
 * mfile.m:
 * petals.m:
 * fact.m:

% Put simply, FACT(N) is PROD(1:N). f = prod(1:n);

In order to edit an M-file after it has been created, the primer uses non-current commands to call the editor from the command line. An example of the correct command edit is shown below.

>> edit mfile

After this command has run, MATLAB opens Editor and displays the current version of the file where it can now be edited.

To simply display the contents of the file in the current window, the type and dbtype commands are used.

Commands:
 * type mfile - displays entire M-file
 * dbtype mfile - displays entire M-file with line numbers
 * dbtype mfile #:# - displays lines # through # of the M-file

Examples: >> type mfile

% This is an M-file. >> dbtype petals

1    % An M-file script to produce       % Comment lines 2    % "flower petal" plots 3    theta = -pi:0.01:pi;                % Computations 4    rho(1,:) = 2 * sin(5 * theta) .^ 2; 5    rho(2,:) = cos(10 * theta) .^ 3; 6    rho(3,:) = sin(theta) .^ 2; 7    rho(4,:) = 5 * cos(3.5 * theta) .^ 3; 8    for k = 1:4 9        polar(theta, rho(k,:))          % Graphics output 10       pause 11   end >> dbtype fact 1:3

1    function f = fact(n) 2    % Compute a factorial value. 3    % FACT(N) returns the factorial of N,

Working with directories
The current directory is the folder location on the hard drive where current files are being initialized from. The current directory should be displayed in a window in MATLAB.

Commands:
 * pwd OR cd - displays the current directory
 * cd .. - navigates the current directory to the one above it
 * cd ( 'directory ') - navigate the current directory to the specified one
 * dir OR ls - lists the files in the current directory in an array
 * dir *.m OR what - lists the M-files in the current directory in an array
 * delete filename - deletes a file from the directory

Examples: >> pwd

ans =

C:\Documents and Settings\...

>> cd

C:\Documents and Settings\...

>> cd .. >> cd ('C:\Documents and Settings\...') After navigating back to the original directory... >> dir

.        ..        fact.m    mfile.m   petals.m

>> ls

.        ..        fact.m    mfile.m   petals.m

>> dir *.m

fact.m   mfile.m   petals.m

>> what

M-files in the current directory C:\Documents and Settings\...

fact     mfile     petals

>> delete mfile.m >> what

M-files in the current directory C:\Documents and Settings\...

fact     petals

Part 15: Comparing efficiency of algorithms: flops, tic, toc
The first section of this part discusses flops. However, MATLAB no longer supports the counting of flops. Therefore, no commands with flops are able to be performed. Below shows the script of the flop fuunction provided by MATLAB.

function f = flops(x) %FLOPS Obsolete floating point operation count. %  Earlier versions of MATLAB counted the number of floating point %  operations. With the incorporation of LAPACK in MATLAB 6, this %  is no longer practical.

%  Copyright 1984-2004 The MathWorks, Inc. %   $Revision: 5.12.4.1 $  $Date: 2004/01/24 09:21:21 $

if (nargin < 1) || (x ~= 0) warning('MATLAB:flops:UnavailableFunction', ...       'Flop counts are no longer available.') end if nargout > 0 f = 0; end

The command including tic and toc are used to measure the elapsed time of an execution. They are timers that start and stop the clock before and after a statement.

Command: tic, statement, toc - displays the time required to execute the statement

Example: >> A = 1

A =

1

>> B = 1

B =

1

>> tic, x = A / B, toc

x =

1

Elapsed time is 0.000038 seconds.

The last command was computed multiple times. Elapsed time varied and was never less than 0.000035 seconds. Measuring efficiency with tic and toc are not reliable measurement of efficiency because elapsed time depends on how busy the computer is during execution.

Part 16: Output format
Displaying variables can be done in many ways. Output depends on how precise the variable needs to be. The format command adjusts this setting. The commands and examples are as follows.

Commands:
 * format - changes the output format to the default type of short
 * format type - changes the output format to the type specified

MATLAB provides the following information in tables in Help for the different types of formatting:

For floating-point variables
 * short - scaled fixed point format with 5 digits (default)
 * long - scaled fixed point format with 15 digits for double; 7 digits for single
 * short e - floating point format with 5 digits
 * long e - floating point format with 15 digits for double; 7 digits for single
 * short g - best of fixed or floating point with 5 digits
 * long g - best of fixed or floating point with 15 digits for double; 7 digits for single
 * short eng - engineering format that has at least 5 digits and a power that is a multiple of three
 * long eng - engineering format that has exactly 16 significant digits and a power that is a multiple of three

For all numeric variables
 * + - +, -, blank
 * bank - fixed dollars and cents
 * hex - hexadecimal (hexadecimal representation of a binary double-precision number)
 * rat - ratio of small integers

For all variables
 * compact - suppresses excess line feeds to show more output in a single screen*loose - adds linefeeds to make output more readable

Examples: >> A = 1.2345678910111213

A =

1.2346

>> format long; A

A =

1.23456789101112

>> format; A

A =

1.2346

>> format short e; A

A =

1.2346e+000

>> format long e; A

A =

1.234567891011121e+000

>> format short g; A

A =

1.2346

>> format long g; A

A =

1.23456789101112

>> format short eng; A

A =

1.2346e+000

>> format long eng; A

A =

1.23456789101112e+000

>> format +; A

A =

+

>> format bank; A

A =

1.23

>> format hex; A

A =

3ff3c0ca42c959f1

>> format rat; A

A =

100/81

>> format compact; A A = 100/81   >> format loose; A

A =

100/81

Part 17: Hardcopy
In order to obtain a copy of the commands and output of a session in MATLAB, MATLAB has a function called diary that directly copies the information displayed in the Workspace window into a file.

Commands:
 * diary - creates a file named diary that stores the output of the Workspace if no file name was specified
 * diary filename - creates a file with the name filename that stores the output of the Workspace
 * diary off - stops recording the output

Example:

What MATLAB outputs to Workspace: >> ls

.             fact.m         petals.m       ..             homework1.txt

>> diary record >> A = 1

A =

1

>> ls

.             fact.m         petals.m       ..             homework1.txt  record

>> diary off >> B = 2

B =

2

What the file record saves: A = 1

A =

1

ls

.             fact.m         petals.m       ..             homework1.txt  record

diary off

Part 18: Graphics
MATLAB can creat multi-dimensioned plots of functions, points, and other forms of data. In order to display these plots, certain commands must be called in order to output the correct display. The plots allow for great versatility with different formats of plots in a variety of colors and from two to three dimensions.

Planar plots
Planar plots graph functions and point in the x-y plane. Below is the basic form of the command and an example of how to implement it.

Command:

plot(x,y,string) - graphs vector y against vector x; string is a string of characters

The string may be omitted from the command so that the plot is a solid black line (plot(x,y)). Incorporating a string allows for dotted, dashed, and other forms of lines available in many colors. By typing in the command below, MATLAB displays the available formats as shown.

>> help plot

Available string possibilities (one element from any or all three columns): b    blue. point             -     solid g    green         o     circle             :     dotted r    red           x     x-mark             -. dashdot c    cyan          +     plus               --    dashed m    magenta       *     star             (none)  no line y    yellow        s     square k    black         d     diamond v    triangle (down) ^    triangle (up) <    triangle (left) >    triangle (right) p    pentagram h    hexagram

Examples:

Graphed as plots: >> figure(1); x = -4:0.01:4; y = sin(x); plot(x,y) >> figure(2); x = -1.5:0.01:1.5; y = exp(-x.^2); plot(x,y)



Graphed as functions function y = expnormal(x) y = exp(-x.^2); >> fplot('expnormal', [-1.5,1.5])
 * Created an M-file titled expnormal.m
 * Entered the data as shown below
 * Entered the command as shown below



Changing plot title and axis names and adding text to spots >> t = 0:0.001:2*pi; x = cos(3*t); y = sin(2*t); plot(x,y) >> title('Best Least Squares Fit') >> xlabel('X-axis') >> ylabel('Y-axis') >> gtext('The Spot')



Adding gridlines and changing the scale (continued from previous section) >> grid >> axis([-2,2,-2,2])



Multiple plots on on graph >> x = 0:0.01:2*pi; y1 = sin(x); y2 = sin(2*x); y3 = sin(4*x); plot(x,y1,x,y2,x,y3)

OR

>> x = 0:0.01:2*pi; Y = [sin(x)', sin(2*x)', sin(4*x)']; plot(x,Y) EDU>



Other types of planar plots include polar, bar, hist, quiver, compass, feather, rose, stairs, and fill.

Graphics hardcopy
To obtain a hardcopy of the current graphic the command print is used. This command will send the image to the default printer using predefined settings. These settings can be changed by using the printopt command.

The command print followed by a name can be used to print and save the graphic. If the name following the print command does not include a post script the appropriate post script will be assigned to the file. Several graphics can be saved to a single file and printed using the print -append filename command. If the append command is not used on an existing file, the existing file will be overwritten with the new graphic.

Graphics other then the current graphic can be printed and saved by use of the command print -deps -graphic filename. An example of this command is print -deps -f4 Lulz. The previous command will save figure 4 in file Lulz.eps.

3-D line plots
Matlab will produce plots in 3 dimensions using the command plot3. If the X, Y and Z coordinates are the desired coordinates the the command would be as follows, plot3(X,Y,Z). An example of a plot using this command is as follows, t=.01:.01:20*pi; x=cos(t); y=sin(t); z=t.^3; plot3(x,y,z), which creates the following helix:



The axis limits can then be determined using the axis command:

EDU>> axis

ans =

-1          1          -1           1           0      250000

3-D mesh and surface plots
Mesh plots can be created using the meshcommand. For example the command mesh(x) will create a mesh plot of the predetermined matrix x. Surface plots are created in a similar fashion using the surf command. Just as before the surf(x) command will create a surface plot of the predetermined matrix x.

In order to draw a graph of a function z=f(x,y) it is important to first define xx and yy. These will give partitions of the sides of the rectangle in which the graph is on. Using the command [x,y] = meshgrid(xx,yy); a matrix x and a matrix y are created. Each row of matrix x is equal to xx and it has a column length equal to the length of yy. Each column of matrix y is equal to yy. For example if one wanted to graph z=e-x 2-y2 over the square [-2,2]X[-2,2] the code would be as follows: [x,y]=meshgrid(-2:.2:2,-2:.2:2); z=exp(-x.^2-y.^2); mesh(z)



The shading of the surface plot can be changed with the shading command. The three choices are shading faceted, shading interp, or shading flat. The following is an example of the shading flat command:



The color of the shading can also be changed using one of the following preset color scheme commands: hsv, hot, cool, jet, pink, copper, flag, grey, or bone. For example colormap(hot)  will color the surface plot with the hot color scheme.



The command view allows one to set how the plot is to be viewed.

Matlab has several preset functions that demonstrate the surface plot feature. I few examples are: peaks, sphere, and cylinder. The following is an example of peaks:



Handle graphics
To see the properties of figure 1 enter the command set(1)  and gca,set(ans) . These properties describe almost all aspects of the plots. This system is called the handle graphics and is further described in the matlab user’s manual.

Part 19: Sparse matrix computations
Matlab is set to assume that any number in a matrix may be a number other then zero. Unfortunately, this assumption cost memory and computing time when it comes to numbers in a matrix that are known to be zero. This is why Matlab has the function sparse available. This function only saves the nonzero components of the matrix saving computing time and memory. For example:

EDU>> f=eye(3)

f =

1    0     0     0     1     0     0     0     1

EDU>> sparse(f)

ans =

(1,1)       1   (2,2)        1   (3,3)        1

EDU>> full(f)

ans =

1    0     0     0     1     0     0     0     1

Notice how the sparse command only shows the nonzero components and the full command shows the entire matrix. One can check the mode of a matrix my entering the command issparse(matrixname) . Typically sparse matrix are generated directly rather then applying the command sparse. To do this one would use the command spdiags. For example:

EDU>> m =6; EDU>> n=6; EDU>> e=ones(n,1); EDU>> d=-2*e; EDU>> t=spdiags([e,d,e],[-1,0,1],m,n)

t =

(1,1)      -2   (2,1)        1   (1,2)        1   (2,2)       -2   (3,2)        1   (2,3)        1   (3,3)       -2   (4,3)        1   (3,4)        1   (4,4)       -2   (5,4)        1   (4,5)        1   (5,5)       -2   (6,5)        1   (5,6)        1   (6,6)       -2

The sparse commands for eye, zeros, one, and randn are speye, sparse, spones, and sprandn respectively. Here is an example of each of these commands:

EDU>> speye(3)

ans =

(1,1)       1   (2,2)        1   (3,3)        1

EDU>> sparse(3)

ans =

(1,1)       3

EDU>> spones(3)

ans =

(1,1)       1

EDU>> sprandn(3)

ans =

(1,1)     -0.4326

Using the sparse command one can create a matrix listing only the nonzero components. An example of this is as follows:

EDU>> i=[1 2 3 4 4 4]; EDU>> j=[1 2 3 1 2 3]; EDU>> s=[5 6 7 8 9 10]; EDU>> S=sparse(i,j,s,4,3), full(S)

S =

(1,1)       5   (4,1)        8   (2,2)        6   (4,2)        9   (3,3)        7   (4,3)       10

ans =

5    0     0     0     6     0     0     0     7     8     9    10

Notice that the matrix is displayed both in the sparse mode and the full mode. The following is another example displayed only in the sparse mode.

EDU>> n=6; EDU>> e=floor(10*rand(n-1,1)); EDU>> E=sparse(2:n,1:n-1,e,n,n)

E =

(2,1)       9   (3,2)        2   (4,3)        6   (5,4)        4   (6,5)        8

It is important to note that any operation done on a full matrix will create a full matrix result while any operation done on a sparse matrix will create a sparse matrix result.

Class Introduction
The first week of lecture was used as an introduction to the class. Dr. Loc Vu-Quoc introduced himself, his wiki page, the course website and the course syllabus.

The course is structured in a way to encourage a collaborative setting to prepare students for similar situations in industry by having the students form groups of 5 or 6. Dr. Loc Vu-Quoc has also stated that he believes that students learn better by teaching, so he has encouraged the group members to try to teach parts of the material to each other. The first assignment for each group was to turn in a paper listing the group name, which should be 4-6 characters in length; the names of the group members, their UFID numbers and their newly created wiki user names, as described in the course syllabus.

First Reading Assignment
The following was the first reading assignment, assigned on Sep. 5, 2008.

Ch 4 (Trusses, Beams, Frame) Ch 1 (Big Picture): 1.1 Discretization 1.1.1 Plane Truss elements 1.2 Assembly of Element equations (Ex: 1.4 Five-bar trusses) 1.4 Element solution of model ralidity 1.4.1 Plane Truss elemnets

Trusses, Matrix Method
Lecture material for the class began with chapter 4 of the book.

Notation:
 * Global Node Number - a number inside a circle; designates a node for the whole structure.
 * Local Node Number - a number inside a square; designates a node for a specific element.
 * Element Number - a number inside a triangle; designates a specific element of the entire structure.

Here we see a truss with 2 elastic, deformable bars

Here we see the global free body diagram for the truss system.

The R's represent unknown reaction forces. The P is a known applied force. This FBD has 4 unknowns and 3 equations of equilibrium which makes it statically indeterminate.

2 FBDs of 2 bar elements:





Notation: fi(e) Ex: f2(1) represents the 2nd internal force in the 1st element.
 * i - ith internal force
 * (e) - element number
 * displacements are written in the same manner: di(e)

Since corresponding forces and displacements act along the same line, they can be represented by the same arrow.

Ex: f1(2),d1(2)

The next big step was to come up with Force-Displacement (FD) relationships.

Below we see the FD relationship of a 1 dimensional spring element with one end fixed followed by the FD relationship for a 1 dimensional spring element with both ends free.



Note: An elastic bar inclined in a plane is a 2 dimensional spring.

The FD relationship for the system can be written in matrix form with forces and displacements arranged as column matrices. The k matrix is called the stiffness matrix.

$$\binom {f1}{f2}=\begin{vmatrix} k & -k\\ -k &k \end{vmatrix} \binom{d1}{d2}$$

Some special cases to consider:

Case 1: An observer sits on node 1.

The observer would move with node 1 and the spring would seem to stretch by d2-d1.

f2 = k(d1-d2)

Case 2: An observer sits on node 2.

The observer would move with node 2.

f1= -f2 = -k(d2-d1)

Steps to Solve a Simple Truss
1. The global picture (description); at structure level are divided into: (1) global dofs (disp. dofs); disp. unknowns, and (2) global forces which usually known except for reactions.

Disp. dofs are partitioned into (1) a known part; e.g. fixed dofs, constraints, and (2) an unknown part; solved using FEM.

similarly for global forces:
 * a known part; applied forces
 * an unknown part; reaction forces

2. element picture
 * element dofs (either local or global coordinates)
 * element forces (either local or global coordinates)

3. global force-displacement
 * element stiffness matrices in global coordinates
 * element force matrices in global coordinates
 * assembly of element stiffness matrices and element force matrices into global force-displacement relationship: kd=F
 * k is NxN;d is Nx1;F is Nx1
 * N = # of known and unknown displacement dofs
 * "free-free" system, unconstrained, singular, determinate is 0

4. elimination of known dops to reduce the global force-displacement relationship
 * stiffness matrix is non-singular; therefore invertible
 * can solve the system of equations - kd=F
 * only known dofs left
 * k is MxM; d is Mx1;F is Mx1
 * M = # of unknown displacement dofs
 * k is non-singular; therefore k-1 exists
 * therefore d=k-1F

5.compute element forces from now known d
 * gives element stresses

6.compute reactions (unknown forces)

Specific Example


Element Length:
 * L(1)=4
 * L(2)=2

Young's Modulus:
 * E(1)=3
 * E(2)=5

X-sectional Area:
 * A(1)=1
 * A(2)=2

Inclination Angle:
 * o (1)=30 deg
 * o (2)=-45 deg

1)Global Picture:

-global dofs



To number the disp. dofs and global forces:
 * follow order of nodes
 * for each node: label x then y

It is possible to arrange global forces and disp. dofs in matrix form:

$$\begin{Bmatrix} f1\\ \vdots\\ f2 \end{Bmatrix}=\begin{vmatrix} k \end{vmatrix}\begin{Bmatrix} d1\\ \vdots\\ d2 \end{Bmatrix}$$

In this case k is a 6x6 matrix and f and d are 6x1 matrices.

This problem was not completed as of Sep. 5, 2008.

Contributing Team Members
The following students contributed to this report:

Aaron Fisher - Eml4500.f08.Lulz.fisher 11:46, 12 September 2008 (UTC)

Eric Layton -

Sam Miorelli - Eml4500.f08.lulz.abcd 08:03, 19 September 2008 (UTC)

Ben Mitchell - Eml4500.f08.Lulz.mitchell.bm 12:15, 19 September 2008 (UTC)

John Saxon - Eml4500.f08.Lulz.js 03:36, 19 September 2008 (UTC)

Andrew Strack - Eml4500.f08.lulz.strack 07:57, 19 September 2008 (UTC)