# ARMiller's Matlab Help

Example of Matlab programming are given here. Additional examples can be found on the Downloadable Files page for Finite Element Analysis, MENG 421. Also see the Carnegie Mellon help pages and Mathworks.
1. Required information: The output you submit for each Matlab program, including graphs, must have a title, your name, and the date. Matlab can automatically print the date with the date function.
```    fprintf('\n           Axial1 ')
fprintf('\n   Your name, MENG 421, ')
disp(date)```
Be careful not to define the symbol disp or you the date funtion will not work.
2. Layout: Copy the output from the Matlab and paste it into the bottom of your source program. Add spaces if necessary to align the decimal points of tables. Adjust the significant figures with a format something like %11.2e or %7.2f. The first form uses 11 spaces for the number with 2 digits to the right of the decimal point plus and e format. The second form uses 7 spaces and omits the e format.

3. Tables: Print tables with a heading to identify the columns. Don't use the Tab key. Print the values in a for loop using the fprintf command and include and the loop index. This will align the numbers on their decimal points. Set the length of the loop with one of the variables to be printed (length(df) here). For example:
```    fprintf('\n  Node   Displacement    Force\n')  % table heading
for i = 1:length(df)
fprintf('    %2.0f   %11.2e   %7.0f \n',i,displ(i),force(i))
end```
• The first part of the fprintf command specifies the form and spacing of the numbers for each column. There are two numbers that follow the % symbol. The left number sets the column width while the right number sets the number of significant digits past the decimal point. The e symbol shows the exponent in the number while the f symbol omits the exponent.
• The second part of the fprintf gives the symbols to be printed.

4. Don't use loops for defining vectors: Operations on all elements of a vector can be done is one statement without using a for loop.
```    a = 0:2:12
b = 25 * mu ./ a```

5. Careful of .* and ./: Don't use .* unless there is a vector on each side. Just use * otherwise. Use ./ when dividing by a vector. Don't use it when dividing by a scalar. (See previous example.)

6. Some Matlab Tips
• The cross product of vectors p and q is: cross(p,q)
• The dot product of vectors p and q is: sum(p .* q), but with Version 5 use dot(p,q)
• The magnitude of vector a is: sqrt(sum(a .* a)) or sqrt(sum(dot(a,a)))

7. Use Good Programming Practices
• Write your code in a file with the extension m
• Write only a little of the code at one time
• Run the file with Matlab to see how it works
• After you run your program, type some symbols names to see what their values are
• For the Windows 95 version, click the Workspace Browser button to see what your variables and vectors are. Alternatively, type the Matlab commands who and whos
• Don't change a program that works. Make a copy and then change the copy. If the copy won't work, delete it and then make another.
• If a program is working, make only one change at a time. Then if it doesn't work, you know where the problem is.
• Be careful of the exponent. Use the e symbol not 10^ for defining a number. For example, to define the elastic modulus of steel, use:
```         ex = 29e6    not
ex = 29*10^6```
The result is the same, but it takes the computer longer (by a factor of several hundred) to do the second version.
• Get extra lines my putting \n at the beginning of end of an fprintf statement. Dont use disp(' ').
• Use a symbol whenever a operation is use more than once. For example,
```   cos2 = cos(2*thetar);
sin2 = sin(2*thetar);
sigxr = sigav + sighalf*cos2 + tauxy*sin2;
sigyalt = sigav - sighalf*cos2 - tauxy*sin2;```

8. Matrix Operations
• Define a vector:   x = 1:9   gives:
`      x =  1   2   3   4   5   6   7   8   9`
• Reshape it to a matrix:   x = reshape(x, 3, 3)   gives:
```      x =   1     4     7
2     5     8
3     6     9```
• Take a piece of a matrix:   a = x(1:3, 1:2)   gives:
```      a = 1     4
2     5
3     6```
• The determinate of a matrix:   det(x)   gives:
`      ans = 0`
because the matrix x is singular. (All finite element analysis matrices must be singular initally.)
9. Simultaneous Solution
• Consider the two simultaneous equations
```       2X + 3Y = 12
4X -  Y = 10```
• Write the equation as f = kd where f is the constant vector d is the coefficient, and d is the solution for X and Y
• Define the coefficient matrix k
```     k = [2    3
4   -1]```
• Define the constant vector f
```     f = [12
10]```
• The solution is then
`     d = inv(k)*f`
where inv(k) in Matlab gives the inverse of k
• Matlab give the solution as
```      d = 3.0000
2.0000```

To Home, index | MENG 421 | ES Problems | Search