To middle

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

  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

  7. Use Good Programming Practices

  8. Matrix Operations
  9. Simultaneous Solution

To top

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

  Last revised: January 16, 2004 -- Copyright 1997-2004 ARMiller