User:Eml 4500.f08.delta 6.specht/HW1

=Homework Assignment 1=

MATLAB
MATLAB Primer, Third Edition Kermit Sigmon Department of Mathematics University of Florida

Section 1: Accessing Matlab
Once logged into the system being used, Matlab can be entered and exited using the matlab and quit system commands. When using certain systems, such as Unix systems and MS Windows, it is suggested that the user keep both the Matlab and local editor active.

Section 2: Entering Matrices
Matlab is a matrix manipulator. Matrices can be 1x1 matrices, scalars, matrices with one column or one row, vectors, or any variation of columns and rows. The inputs can be integers, variables or complex numbers.

Inputting matrices into Matlab can be done in many different ways. They can be input with an explicit list of elements, built in statements or functions, diskfile from local editor or from external data files or applications.

The following are examples of matrix inputs.

Complex Numbers can be put into matrices in the following ways.

If an external data file is used. The file can be loaded into Matlab using the load function. For example, if the file is named data.ext. The file can be uploaded into Matlab using the function load data.ext.

There are many built in function in Matlab that can be used to generate matrices. This section will look at three common matrix generators, rand, magic and hilb.

rand(n) generates a matrix with random entries between 0 and 1 into a nxn matrix. rand(m,n) generates a matrix with rando0m entries between 0 and 1 into an mxn matrix Below is an example of the rand function.

magic(n) will create an integral nxn matrix which is a magic square. This means that the rows, columns and diagonals have common sums. The following is an example of the magic(n) function.

hilb(n) will create a nxn hilbert matrix (m and n denote positive integers).

The following is an example of the hilb(n) function.

Matrix and vector entries can be referenced using A(3,2). This pulls the entry from the 3rd row and the 2nd column from matrix A.

The following is an example of how to reference an entry within a matrix.

Section 3: Matrix Operation, Array Operations
Section three references the basic matrix operators. These operators are addition,+, subtraction,-, Multiplication,*, power,^, transpose,', and left and right division,/ and \. These operators should be known by anyone with a programming background.

While using the multiplication, power and division operators, a period can be used that will make the operators operate entry-wise.

The following is an example of entry-wise operations.

Section 4: Statements, Expressions and Variables; Saving a Session
Matlab is an expression language. It evaluates and interprets expressions entered and produces matrices. Expressions are composed of operators, functions and variable names.

A Statement is normally terminated with the return button. Several statemnet can be place on the same line using commas or semicolons following the statements. Semicolons suppress the printing of the statement.

A session can be saved be using the save command and reloaded using the load command. The following are examples of the save and load commands. EDU>> save

Saving to: matlab.mat

EDU>> load

Loading from: matlab.mat

Section 5: Matrix Building Functions
This section references convenient matrix building functions.

The eye function give an identity matrix of a given matrix. The following is an example of the eye functiion. This example shows the identity matrix of matrix A. EDU>> eye(size(A))

ans =

1    0

0    1

The zeros function gives a matrix of zeros. The following is an example of the zero function.

EDU>> zeros(3,3)

ans =

0    0     0     0     0     0     0     0     0

The diag function creates or extracts diagonals. The following is an example of the diag function.

EDU>> diag(5)

ans =

5

Using two diag functions together a diagonal of a matrix can be retrieved. The following is an example of the diag(diag(A)) function that will retrieve the diagonal of the A matrix.

EDU>> diag(diag(A))

ans =

1.0000 + 5.0000i       0 0            4.0000 + 8.0000i

Matrices can also be built from blocks. The following is an example of a matrix built from matrix A, two zeros matices and one eye matrix. This function builds a 5x5 matrix.

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

B =

1    0     0     0     0     0     1     0     0     0     0     0     1     0     0     0     0     0     1     0     0     0     0     0     1

Section 6: For, While, and If Statements
The following MATLAB flow control statements operate similarly to most computer languages.

For a given n, the statement below will produce a certain n-vector, which in this case is given a value of 10. EDU>> x=[];

EDU>> for i = 1:10, x=[x,i^2],end

x =

1

x =

1    4

x =

1    4     9

x =

1    4     9    16

x =

1    4     9    16    25

x =

1    4     9    16    25    36

x =

1    4     9    16    25    36    49

x =

1    4     9    16    25    36    49    64

x =

1    4     9    16    25    36    49    64    81

x =

1    4     9    16    25    36    49    64    81   100

The next code produces another n-vector using different parameters.

EDU>> x=[];

EDU>> for i = 1:5

x=[x,i^2]

end

x =

1

x =

1    4

x =

1    4     9

x =

1    4     9    16

x =

1    4     9    16    25

This next code produces the result as above, but in reverse order.

EDU>> x=[];for i = 5:-1:1, x=[x,i^2], end

x =

25

x =

25   16

x =

25   16     9

x =

25   16     9     4

x =

25   16     9     4     1

The statement below will produce the mxn hilbert matrix. For this example, m and n are given a value of 5. This allows for any matrix to be formed, unlike the 1:n statement.

EDU>> for i=1:5

for j= 1:5

H(i,j)=1/(i*j-1);

end

end

EDU>> H

H =

Inf   1.0000    0.5000    0.3333    0.2500

1.0000   0.3333    0.2000    0.1429    0.1111

0.5000   0.2000    0.1250    0.0909    0.0714

0.3333   0.1429    0.0909    0.0667    0.0526

0.2500   0.1111    0.0714    0.0526    0.0417

Once a matrix is defined, the following statements compute the sum of matrix A by adding all of the column sums. The only difference between the two statements are the amount of time taken to complete these computations.

EDU>> A=[1 2 3 4;5 6 7 8]; EDU>> s=0; EDU>> for c=A s=s+sum(c); end EDU>> s

s =

36

EDU>> sum(sum(A))

ans =

36

The next type of MATLAB function is the WHILE statement. For a given number a, the following statement will produce the smallest nonnegative number n such that 2^n>=a.

EDU>> n=0;

EDU>> while 2^n> n

n =

4

The final MATLAB statement is the IF statement. This type of statement will only work if the statement is true. The following example illustrates multiple branching.

EDU>> if n<0

parity = 0;

elseif rem(n,2)==0

parity = 2;

else

parity = 1;

end

EDU>> parity

parity =

2

EDU>> n

n =

4

The remainder function produces the amount left over when using division. The example below shows the remainder of four divided by two.

EDU>> rem(4,2)

ans =

0

This example illustrates the result of using the remainder function without fully completing the argument.

Many relational operators are available for use in MATLAB. A chart has been formed listing the most common operations, and a short description of each.

The following statements are examples of the various relational operators in use. An answer of 1 signifies a true statement, while an answer of 0 illustrates a false statement.

EDU>> 3<5

ans =

1

EDU>> 3>5

ans =

0

EDU>> 3==5

ans =

0

EDU>> 3==3

ans =

1

When applying relational operators to matrices, the true statements are illustrated by a 1 or nonzero. The following examples depict three scenarios of using relational operators on matrices.

EDU>> a=rand(5)

a =

0.0357   0.7431    0.0318    0.6948    0.3816

0.8491   0.3922    0.2769    0.3171    0.7655

0.9340   0.6555    0.0462    0.9502    0.7952

0.6787   0.1712    0.0971    0.0344    0.1869

0.7577   0.7060    0.8235    0.4387    0.4898

EDU>> b=triu(a)

b =

0.0357   0.7431    0.0318    0.6948    0.3816

0   0.3922    0.2769    0.3171    0.7655

0        0    0.0462    0.9502    0.7952

0        0         0    0.0344    0.1869

0        0         0         0    0.4898

EDU>> a==b

ans =

1    1     1     1     1

0    1     1     1     1

0    0     1     1     1

0    0     0     1     1

0    0     0     0     1

Section 7: Scalar Functions
Some functions in MATLAB are scalar in nature, but operate element-wise when used in a matrix. The most common of these functions are found below.

Section 8: Vector Functions
MATLAB functions operate essentially on a vector (row or column). A row by row application can be obtained by using the transpose, i.e. mean(A’)’. As shown the mean(A’)’ function determines the mean of the matrix in a row by row fashion.

EDU>> mean(A')'

ans =

5    5     5

Moreover, to find the maximum entry in a matrix the max(max(A)) function should be used rather than max(A). Here it is shown that the maximum value for the matrix A is equal to 9.

EDU>> max(max(A))

ans =

9

Section 9: Matrix Functions
The matrix functions in MATLAB offer the programs greatest asset. Some of the most useful functions are tabulated below.

Once a matrix is defined, the eig(A) function is used to find the eigenvalues of a given matrix.

EDU>> A

A = 8    1     6

3    5     7

4    9     2

EDU>> eig(A)

ans =

15.0000

4.8990

-4.8990

The [U,D] = eig(A) function, on the other hand, produces a matrix U where each column signifies an eigenvector for matrix A and a matrix D containing the eigenvalues of matrix A along its diagonal. The matrix A that is used for these computations is the same as above. EDU>> [U,D]= eig(A)

U =

-0.5774  -0.8131   -0.3416

-0.5774   0.4714   -0.4714

-0.5774   0.3416    0.8131

D =

15.0000        0         0

0   4.8990         0

0        0   -4.8990

Section 10: Command Line Editing and Recall
MATLAB's line editing and recall are extremely helpful when the related commands are needed repeatedly. These commands are activated by using the arrow keys. Therefore an earlier command can be recalled, edited, and then executed as follows: a=rand(4)

a =

0.7948   0.1730    0.8757    0.8939

0.9568   0.9797    0.7373    0.1991

0.5226   0.2714    0.1365    0.2987

0.8801   0.2523    0.0118    0.6614

Rather than creating an M-file, this convenient feature allows recalling the same command and modifying it before it is executed. In this case the command was modified to generate 9 random numbers instead of the original 3. a=rand(9)

a = 0.2844   0.2259    0.4611    0.0150    0.3200    0.2126    0.4514    0.6124    0.7176

0.4692   0.5798    0.5678    0.7680    0.9601    0.8392    0.0439    0.6085    0.6927

0.0648   0.7604    0.7942    0.9708    0.7266    0.6288    0.0272    0.0158    0.0841

0.9883   0.5298    0.0592    0.9901    0.4120    0.1338    0.3127    0.0164    0.4544

0.5828   0.6405    0.6029    0.7889    0.7446    0.2071    0.0129    0.1901    0.4418

0.4235   0.2091    0.0503    0.4387    0.2679    0.6072    0.3840    0.5869    0.3533

0.5155   0.3798    0.4154    0.4983    0.4399    0.6299    0.6831    0.0576    0.1536

0.3340   0.7833    0.3050    0.2140    0.9334    0.3705    0.0928    0.3676    0.6756

0.4329   0.6808    0.8744    0.6435    0.6833    0.5751    0.0353    0.6315    0.6992

Section 11: Submatrices and Colon Notation
In order to manipulate fairly complex data, vectors and submatrices are often use. The effective use of these diminishes the time length, the number of loops and the quantity of code.

x = [0.0:0.1:2.0]';

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

Colon notation can be used to retrieve submatrices from the main matrix. By using x(1:7,1) the first 7 elements of the matrix will be obtained.

ans =

0

0.1000

0.2000

0.3000

0.4000

0.5000

0.6000

Section 12: M-Files
MATLAB can operate with M-files, which are a set of script files that are executable within MATLAB. The M-file can contain a matrix to be assigned to a certain variable, which is known as a script file. A matrix can be entered into a script file named data.m as follows:

A = [ 1 2 3 4 5 6 7 8 ]; Once the name of the file intended for use is entered into the MATLAB prompt, the assignment will be carried out.

EDU>> data

EDU>> A

A = 1    2     3     4

5    6     7     8

When a long function is intended for use in MATLAB it can also be written as an M-file, which would be called a function file. This allows for editing of the function without having to retype the whole function within the MATLAB prompt. In order to write a function file, the name of the function, and the arguments for the inputs and outputs of the function. Also, any lines of the code that are intended solely for commenting on the intents of the function should have % included at the beginning of the line. This informs MATLAB that the rest of the line does not contain any information necessary to the programs intended computations. Function files would be written as:

function a = randint(m,n) %RANDINT Randomly generated integral matrix. %  randint(m,n) returns an m-by-n such matrix with entries %  between 0 and 9. a=floor(10*rand(4,5));

The same function can be stated more generally.

function a = randint(m,n,a,b) %RANDINT Randomly generated integral matrix. %  randint(m,n) returns an m-by-n suc 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(3,4)) + a;

The use of the command nargin in this example allows the user to omit values for input variables, and then set them to a default value after using the command. Multiple output variables can also be obtained and equated to a value obtained by a function. An example of this is presented below.

function [mean,stdev]=stat(x) %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. x=[1 2 3 4]; [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);

For this example, the vector x has been declared within the M-file, but it can also be declared within the MATLAB prompt, and then the function can be run without the line declaring x. If this line is left within the function, the value of x would be changed during the function, and the answer obtained may not be correct.

MATLAB can also run more efficiently if the matrix in use is predefined. The matrix, therefore, would not have to be resized every iteration. An example of a for loop that demonstrates this is as follows:

EDU>> M=magic(6);

EDU>> E=zeros(6,50);

EDU>> for j=1:50

E(:,j)=eig(M^i);

end

This function has already declared a size for the matrix E, thus saving MATLAB and the user time that would otherwise be performing calculations.

The next function demonstrates the use of nargin, nargout, and feval. The first two allow some of the input variables and output variables to be optional. The use of feval allows an input variable to be named as a string.

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 function 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 optional third input argument sets a tolerance for the relative %  accuracy of the result. The default is eps. An optional second output %  argument gives a matrix containing a trace of the steps; the rows are %  of form [c f(c)]. %Initialization fun=0.5; x=[1 2 3 4]; 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+0.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

Section 13: Text Strings, Error Messages, and Input
A string of text can be written in MATLAB with a set of single quotes. The string can also be equated to a variable:

EDU>> s='This is a test' s =

This is a test

This string would be equated to the variable s, and restated by the program. If desired, the string can be displayed without equating it to any variable by using: EDU>> disp('this message is hereby displayed')

this message is hereby displayed An error message, which will also end the execution of a function, can be displayed within a function by using the code in a manner as follows: EDU>> error('Sorry, the matrix must be symmetric')

??? Sorry, the matrix must be symmetric

An interactive message can also be used in a MATLAB function. This would ask for a value from the user and equate this to a variable, declared by the function. In this case, the variable iter is equated to the input, and the message displayed is contained within single quotes.

EDU>> iter = input('Enter the number of iterations: ')

Enter the number of iterations: 5

iter =

5

Section 14: Managing M-Files
M-files must be stored in a directory that is available to MATLAB, which is termed the working directory. M-files stored in this location are available to be manipulated by MATLAB, including running the program to return a value. While working in MATLAB there are commands that are available to make working with M-files simpler.

Section 15: Comparing Efficiency of Algorithms: flops, tic and toc
Efficiency within MATLAB is determined by using two main values. The first is the number of floating point operations performed (flops). Entering this before and after running a function will return the flop count for the function. The value can also be reset by entering flops(0) immediately before running the function. If the user is working in MATLAB Version 6 or later, this command is not available. The commands tic and toc, when used immediately before and after executing a statement, will return the amount of time elapsed while performing the operation. Two statements that perform the same operation are shown below, along with the elapsed time.

EDU>> A=[1 2;3 4]

A = 1    2

3    4

EDU>> b=eye(2)

b =

1    0

0    1 EDU>> tic,x=A\b;toc

Elapsed time is 0.048711 seconds.

EDU>> tic, x=inv(A)*b; toc

Elapsed time is 0.266468 seconds.

From this it can be seen that although the results of the two functions are the same, by adding the inverse function to the second declaration, more time is needed to perform the same calculation.

Section 16: Output Format
All computations in MATLAB are executed while maintaining double type precision. The output format can be changed by using the following functions, and will remain in this format until changed by the user.

Section 17: Hardcopy
If a copy of the commands and the results are needed, MATLAB can write these to a file, excluding any graphics that are obtained. By using the command diary followed by the filename that results are to be written to, this action can be initiated. This can be turned on and off by using the commands diary on and diary off.

Section 18: Graphics
Below is the basic list of commands for displaying a graph in MATLAB.

A sine command is being used below to show the use of the command plot(x,y). This is drawn over the interval -4 to 4. The input used in MATLAB is displayed first, followed by the plot that is displayed in a separate window.

EDU>> x=-4:.01:4; y=sin(x); plot(x,y)



The table given below shows many commands that can be used to manipulate aspects of the graph, including the titles and the axes. It should be noted that any command consisting of the prefix axis should be stated after the plot command.

The command fplot is used as a demonstration below. The interval that is to be graphed is displayed in the matrix. The m-file that contains the computations is listed first, followed by the command in the MATLAB prompt.

function y=expnormal(x) y=exp(-x.^2);

fplot('expnormal',[-1.5,1.5])



A set of parametrically designed curves can be displayed on the same graph with a set of commands that are similar to those shown below.

t=0:.001:2*pi; x=cos(3*t); y=sin(2*t); plot(x,y)



The following two sets of commands produce the same graph. Both are manners in which multiple plots can be displayed on the same graph. The first declares each variable individually. The second example inputs each of the Y variables in a matrix form.

x=0:.01:2*pi;y1=sin(x);y2=sin(2*x);y3=sin(4*x);plot(x,y1,x,y2,x,y3)

x=0:.01:2*pi;Y=[sin(x)', sin(2*x)', sin(4*x)'];plot(x,Y)



Linetypes and the colors of the lines can also be changed. The commands for these are listed in a table.

Commands such as those shown in the table above can be applied in a manner as follows:

x=0:.01:2*pi; y1=sin(x);  y2=sin(2*x);  y3=sin(4*x);

plot(x,y1,'--',x,y2,':',x,y3,'+')



MATLAB provides a set of commands in order to print graphs without leaving the command prompt. Some of these basic commands are listed in the table shown below.

Three dimensional plots can be generated in a manner similar to the x-y plots generated earlier. t=.01:.01:20*pi; x=cos(t);  y=sin(t);  z=t.^3;plot3(x,y,z)



A mesh plot can be created by using the command mesh, as shown in the table at the beginning of this section. An identity matrix is used to display this command. mesh(eye(10))

A surface plot similar to the mesh plot above is shown below. surf(eye(10))



A meshgrid can be created using the command meshgrid. The following set of code creates a matrix x which have rows that equal xx and has a column length of yy, as well as a matrix y which has columns that equal yy and has a row length of xx. These values are then used to evaluate a chosen function, in this case being the exponential function. The same code could also be written in a shorter form, which immediately follows the first set of code. xx=-2:0.2:2;

yy=xx;

[x,y]=meshgrid(xx,yy);

z=exp(-x.^2 - y.^2);

mesh(z)



[x,y]=meshgrid(-2:.2:2, -2:.2:2);

z=exp(-x.^2 - y.^2);

surf(z)



The set of commands used to edit axes and titles that were presented for planar plots are also applicable for all of the three dimensional plots that have been presented.

The use of one of these commands is demonstrated below to demonstrate how it is declared. colormap(cool)



Some surface plots are already programmed into MATLAB, and are easily plotted. These include sphere, cylinder, and torus. An m-file used to generate a torus plot is shown below, followed by the plot that is generated. function [x,y,z]=torus(r,n,a) %TORUS Generate a torus %  torus(r,n,a) generates a plot of a torus with central radius a and %  lateral radius r.  n controls the number of facets on the surface. %  These input variables are optional with defaults r=0.5, n=30, a=1. % %  [x,y,z] = torus(r,n,a) generates three (n+1)-by(n+1) matrices so that %  surf(x,y,z) will produce the torus. % %  See also SPHERE, CYLINDER

if nargin <3, a=1; end if nargin <2, n=30; end if nargin <1, r=0.5; end theta = pi*(0:2:2*n)/n; phi = 2*pi*(0:2:n)'/n; xx=(a + r*cos(phi))*cos(theta); yy=(a + r*cos(phi))*sin(theta); zz=r*sin(phi)*ones(size(theta)); if nargout ==0 surf(xx,yy,zz) ar = (a+r)/sqrt(2); axis([-ar,ar,-ar,ar,-ar,ar]) else x=xx;y=yy;z=zz; end



Section 19: Sparse Matrix Computations
A sparse matrix can be used to increase efficiency and storage space by storing only the nonzero elements that are contained within the matrix. Some of the commands to manipulate matrices in this manner are listed below.

A full tridiagonal matrix is created below, and then converted to sparse mode.

EDU>> F=floor(10*rand(6));F=triu(tril(F,1),-1)

F =

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

EDU>> S= sparse(F)

S =

(1,1)       7

(2,1)       2

(1,2)       5

(2,2)       1

(3,2)       1

(2,3)       2

(3,3)       9

(4,3)       3

(3,4)       3

(4,4)       8

(5,4)       5

(4,5)       7

(5,5)       3

(6,5)       5

(5,6)       9

(6,6)       1

Another example of a sparse matrix is shown below.

EDU>> m=6;n=6;e=ones(n,1);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

Another example of a matrix in this same format is shown below. The full matrix is shown afterwards. The difference between the two is the values of m and n that are input into the function.

EDU>> T=spdiags([e,d,e],[-3,0,2],m,n)

T =

(1,1)      -2

(4,1)       1

(2,2)      -2

(5,2)       1

(1,3)       1

(3,3)      -2

(6,3)       1

(2,4)       1

(4,4)      -2

(3,5)       1

(5,5)      -2

(4,6)       1

(6,6)      -2

EDU>> S=full(T)

S =

-2    0     1     0     0     0

0   -2     0     1     0     0

0    0    -2     0     1     0

1    0     0    -2     0     1

0    1     0     0    -2     0

0    0     1     0     0    -2

A sparse matrix can also be entered by including only the nonzero entries by using code that resembles the following:

EDU>> i=[1 2 3 4 4 4];j=[1 2 3 1 2 3]; 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

In the commands above, the matrix s gives all the nonzero values contained in the matrix, while the matrices i and j give their row and column values respectively.

Another method of forming a sparse matrix is shown below. In this instance the matrix is constructed using two variables to declare the size of the matrix.

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

E =

(2,1)       5

(3,2)       4

(5,4)       3

(6,5)       1

The amount of time that an operation takes to compute can be compared for the situations when the matrix is sparse (S) or full (F). This can be used to compare for efficiency purposes. The matrices are constructed once again from variables that are declared in the prompt. The output that follows is labeled sparsetime and fulltime, which are equal to the time taken to complete each operation.

EDU>> n=20;e=ones(n,1);d=-2*e; T=spdiags([e,d,e],[-1,0,1],n,n);A=full(T);

EDU>> b=ones(n,1);s=sparse(b);tic,T\s;sparsetime=toc, tic,A\b;fulltime=toc

sparsetime =

0.0134

fulltime =

0.0809

Other operations that can take place by using sparse (S) or full (F) matrices are listed in the table below.

Eig(S) can also be used to manipulate a sparse matrix, but only if the matrix is symmetric. If it is not symmetric, then this command will return that the computation is undefined.

History

 * Wikipedia was launched in 2001 by Jimmy Wales and Larry Sanger
 * The name Wikipedia breaks down into "wiki" (a collaboration of websites) and encyclopedia
 * The purpose of Wikipedia is to collect and summarize all human knowledge in every major language
 * All members of society can contribute to Wikipedia

Creating an Account
1. In order to create an account, the user must first choose a major language 2. The user must then click on the log in/ create account link in the upper right hand corner 3. If the user does not have an existing account they should click on the create one link 4. The user must enter the word(s) in the given text box that appears in order to protect against automated account creation 5. Next, the user must fill the appropriate information such as user name, password, and an e-mail address which is optional
 * Once the account is created the user may give his/her contribution by writing their own wiki-articles or by editing the articles of other users
 * All edits to wiki-articles are permanently recorded in archives, and publicly visible in the history link of any page *(The Wikipedia contribution system is a great asset yet its own greatest flaw)

Criticism

 * Wikipedia allows its users to edit each others pages which brings a mutual responsibility to all users not to abuse the right to edit. The open nature of the system to many critics make it unreliable as a reference. On top of unreliability, the system allows for strongly opinionated editors to attempt to dominate articles. Exposure to vandalism by users who edit articles is also a critical argument against Wikipedia. Wikipedia vandalism ranges from page lengthening, blanking, spamming, user name vandalism, etc.

Vision

 * Media Wiki is an important program in that it allows its users to collaborate each others work.
 * The course will employ the Wikiversity tool. This will allow the students, professor, and teaching assistants to communicate with ease and to review each team's work so as to maximize the learning experience during the semester.
 * MIT's Open Course Wear is a prime example of knowledge shearing that is Wiki-like in that MIT students only, since access faculty is not provided, can share information with each other about their course work. MIT's OCW has proven to be a very powerful tool for students campus-wide.

Method of Work
Course Website: EML4500


 * Teams consist of six members that share the course workload. One member will be selected as the team leader. Each of the six members will complete parts of the homework assignments. Then, the team leader will compile the information into one article. That final article will be graded by the teaching assistant and assigned a grade.
 * Each member of the team receives the grade that is assigned. Team members will evaluate each others contribution and effort at each exam by giving an evaluation score based on a scale ranging from one to eight.

E-Learning

 * This tool is not like MediaWiki since it does not allow for collaboration between team members, teams, and the professor. However, E-Learning excels in passing announcements from professor to students.

Old Approach/New Approach

 * Prior to using the Wikipedia/Wikiversity system, professor Dr. L. Vu-Quoc taught EML4500: Finite Element Analysis based on a system where homework criteria accounted for 10% of a students final grade. The other 90% of the grade was based on 3 exams, each worth 30%.
 * Dr. L. Vu-Quoc's new approach allows for a greater emphasis on the students homework contribution. The Wikipedia/Wikiversity system of posting class notes/homework problems keeps students more involved and also allows other fellow classmates to benefit from having access to this information. Hopefully, in the near future students will be able to save money by not having to purchase expensive course text books. The new approach weighs a students homework criteria at 30% while 3 exams (equal in worth) make up the remainding 70%.

Trusses
Trusses are commonly known as structural members that resist axial forces. In order to facilitate the analysis of simple truss systems the matrix method should be utilized.

Matrix Method
The matrix method provides basic steps that are use as a recipe while solving truss systems helping solve problems more efficiently. This is achieved because a common labeling system for the diagrams is used.

Notation

Consider a truss system with two elastic bars, meaning that they are deformable since no materials are perfectly rigid, and both supported ends fixed and constrained to zero displacement in the x and y, as well as the x̃ and ỹ directions. Additionally, a force P loads the truss at the vertex.



Notice the global picture of the above truss system. It is composed of two bar elements that are joined and fixed at both ends. This calls for the need to identify three global nodes, and their reactions; it has been chosen from this point forth to circle the global node numbers for easy notation. Developing a free-body diagram (fbd) will result in the analysis above for the whole truss system (global picture).



We will analyze this system by choosing to use the y-x coordinate system. This global fbd yields 4 unknowns and 3 equations, making this system statically indeterminate. However, we can separate each bar element and build its own fbd.



New notation had to be used to number the bar element number with numbers inside a triangle. The diagram above is, thus, for bar element number 1. Also, local notation for element nodes have also been identified with a square around its node number, each corresponding to a global node (circled). Forces and reactions at each node have also been shown. The x and y directions of the reactions at the supports have also been separated, These have been labeled with an R with a subscript and a superscript that reflect the degree of freedom and the element number, respectively. The labeling of these forces is governed by these rules, in order: The directions of displacement of each node can also be represented by the red arrows in the diagram above, and both represent the degrees of freedom of the element. To save time, it has been decided to also label these arrows as being the direction of movement, each serving a dual purpose. The distances are represented by a d with the labels being the same for its force counterpart. We have done the same for the second bar element as for element 1, shown in the figure below.
 * Follow the order of the global node number
 * For each global node number, label the x-direction reaction first, then the y-direction reaction.



Notice that the number in the triangle has been changed to 2 to represent the element number. Also, the rules described above to label nodes has been followed. Global node 2 (circled) is now local node 1 (in square) and the global node 3 is now the local node 2. There is also another choice of local node numbering, where global node 2 can be local node 2 and the global node 3 is the local node 1. Both modes of numbering will yield the same outcome once the problem is solved, and the second is illustrated below.



Force-Displacement Relation

Recall a 1-D spring:

The above model is for a 1-D spring element with one end fixed. The following force-displacement (FD) relationship for this element is described below.


 * $$f=k*d$$

Where $$f$$ is the force on the free end, $$d$$ the distance the spring is deformed because of the force, and $$k$$ the stiffness of the spring and it depends on the material the spring is made out of.

If we were to free the left end of the above spring then the spring would be affects by the FD relationship as seen below.

The FD relation for this element is more complex as the displacements depend on each other, and each depends on both forces. We model this relationship with the aid of matrices, where each term is represented by a matrix.


 * $$\begin{pmatrix}

f_1\\ f_2\end{pmatrix}$$ = $$\begin{bmatrix} k & -k\\ -k & k\end{bmatrix}$$ $$\begin{pmatrix} d_1\\ d_2\end{pmatrix}$$

The first is a 2x1 column matrix that represents the forces on the spring, the second is a 2x2 matrix that represents the stiffness of the spring, and the third is also a 2x1 column matrix that represents the displacements of both free ends or nodes. This matrix equation is product of two cases that superimpose, each representing the behavior of both nodes as the spring stretches or compresses.


 * Case 1: The observer sits on node 1 and watches how the spring stretches out, represented by:
 * $$f_2=k(d_2-d_1)$$


 * Case 2: The observer sits on node 2 and watches how the spring stretches out, represented by:
 * $$f_1=k(d_1-d_2)=-f_2=-k(d_2-d_1)$$

Steps for Solving Truss Systems

 * 1) Global picture.
 * 2) Element picture.
 * 3) Global Free Diagram.
 * 4) Elimination of known degrees of freedom.
 * 5) Computation of element forces.
 * 6) Computation of reactions.

Global Picture
The global picture is the description of the system as one entity. This diagram includes known reactions such as applied forces, fixed degrees of freedom (dofs), and constrains. The global picture also illustrates the unknown part of the system, the reactions.


 * Redraw the truss system.
 * Label the global nodes.
 * Label the global forces.





Element Picture
The element picture is the description of each element forming the whole structure. In this diagram the known internal forces and reactions that are acting on that element are illustrated.


 * Redraw the truss system by parts.
 * Label the element degrees of freedom (dofs).
 * Label the element forces.

Note: The element dofs and forces can be either placed in the global coordinate systems or in the local coordinate system.





Global Free Diagram Relation
In order to apply the matrix method it is necessary to develop a relationship between the applied forces and the resulting displacement. The relationship can be formed by treating the trusses as deforming springs and using the mass-spring principle. The mass-spring principle is defined by the equation F=Kd, where F is the force matrix (n x 1), d  is the displacement matrix (n x 1), and K is the spring constant matrix (n x n). Where n is the number of unknown and known displacement degrees of freedom (dofs). These matrices define the linear relationship between the element forces and the element stiffness matrix.

Elimination of known degrees of freedom
It is necessary to reduce the global free body diagram relation in order to solve the stiffness matrix. Due to the non-singularity of the stiffness matrix the mass-spring principle equation is invertible. Resulting in the following new relationship: d = K-1F, where d  is the displacement matrix (m x 1), K-1 is the spring constant matrix (m x m), and F  is the force matrix (m x 1). Where m is the number of unknown displacement degrees of freedom (dofs).

Computation of element forces
As a result of now knowing the displacement matrix d , the element forces can be calculated.

Computation of reactions
The last step is the computation of reactions of the unknown forces in the truss system. Since d and F have the same matrix size the matrix K is symmetric. By counting the support’s constrains the unknowns can be solved by using a system of linear equations.

Contributing Team Members
Iain Specht Eml_4500.f08.delta_6.specht September 20, 2008 Diana Guzman eml4500.f08.delta_6.guzman September 20, 2008 Nicolas Castrillon eml4500.f08.delta_6.castrillon September 20, 2008 Sean Lopezeml4500.f08.delta_6.lopez September 20, 2008 Eric Ramirez eml4500.f08.delta_6.ramirez September 20, 2008 Trent Kruegereml4500.f08.delta_6.krueger September 20, 2008