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.
- 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.
- 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.
- 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.
- 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
- 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.)
- 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)))
- Use Good Programming Practices
- 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.)
- 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
Last revised: January 16, 2004
-- Copyright © 1997-2004
ARMiller