matlab getting started
DESCRIPTION
TRANSCRIPT
Computer methods for structural analysis
MATLAB GETTING MATLAB GETTING STARTINGSTARTING
Computer methods for structural analysis014139
Computer methods for structural analysis
Outline of lecture
Starting MATLABM-filesVariablesVectors, Matrices, and Linear AlgebraSelection ProgrammingInput and OutputGetting Help
Computer methods for structural analysis
To start MATLAB: START PROGRAMS
MATLAB 7.0 MATLAB 7.0
StartingMATLAB
Computer methods for structural analysis
Standard MATLAB interface
Computer methods for structural analysis
M-Files
The M-file is a text file that consists a group of MATLAB commands.To run the M-files, just type the file name in the command window. (make sure the current working directory is set correctly)
All MATLAB commands are M-files.
So far, we have executed the commands in the command window. But a more practical way is to create a M-file.
Computer methods for structural analysis
Getting exist m-file
Open filePathHW1.m
Computer methods for structural analysis
To run M-file
Writing M-file:
can be used:
•Built-in MATLAB editor
•Any text editor
DebugRun
Push run button
Computer methods for structural analysis
Variables
Variable names:Must start with a letterMay contain only letters, digits, and the underscore “_”Matlab is case sensitive, i.e. one & OnE are different variables.Matlab only recognizes the first 31 characters in a variable name.
Assignment statement:Variable = number;Variable = expression;
Example:>> al = 1234;>> a = 1234a =
1234
NOTE: when a semi-colon ”;” is placed at the end of each command, the result is not displayed.
Computer methods for structural analysis
Vectors, Matrices and Linear Algebra
Entering MatricesMatrices can be
Entered manuallyGenerated by built-in functionsLoaded from a file
Computer methods for structural analysis
Manual entering
NOTE: MATLAB index starts at 1.
it begins with [, and end with ]spaces or commas are used to separate elements in a rowsemicolon or enter is used to separate rows.
Computer methods for structural analysis
Vectors and Matrices
Some useful commands:
zeros(n)zeros(m,n)
ones(n)ones(m,n)
size (A)
length(A)
returns a n x n matrix of zerosreturns a m x n matrix of zeros
returns a n x n matrix of onesreturns a m x n matrix of ones
for a m x n matrix A, returns the row vector [m,n] containing the number of rows and columns in matrix.
returns the larger of the number of rows or columns in A.
Computer methods for structural analysis
Matrix operation
Computer methods for structural analysis
The colon operator “:”
Computer methods for structural analysis
Transpose B = A’
Identity Matrix eye(n) returns an n x n identity matrixeye(m,n) returns an m x n matrix with ones on the main diagonal and zeros elsewhere.
Addition and subtraction C = A + BC = A – B
Scalar Multiplication B = αA, where α is a scalar.
Matrix Multiplication C = A*B
Matrix Inverse B = inv(A), A must be a square matrix in this case.rank (A) returns the rank of the matrix A.
Matrix Powers B = A.^2 squares each element in the matrixC = A * A computes A*A, and A must be a square matrix.
Determinant det (A), and A must be a square matrix.
more commands
A, B, C are matrices, and m, n, α are scalars.
Matrix operation
Computer methods for structural analysis
Selection Programming
Flow ControlIf statementFor loopsWhile loopsContinue statementBreak statement
FunctionsBuilt-in functionsUser-Defined functions
Computer methods for structural analysis
If statementsSimple if statement:
if logical expressioncommands
endExample: (Nested)
if d <50count = count + 1;disp(d);if b>d
b=0;end
endExample: (else and elseif clauses)
if temperature > 100disp (‘Too hot – equipment malfunctioning.’)
elseif temperature > 90disp (‘Normal operating range.’);
elseif (‘Below desired operating range.’)else
disp (‘Too cold – turn off equipment.’)end
Computer methods for structural analysis
If statements
Computer methods for structural analysis
Loops
for loopfor variable = expression
commandsend
while loopwhile expression
commandsend
•Example (for loop):for t = 1:5000
y(t) = sin (2*pi*t/10);end
•Example (while loop):EPS = 1;while ( 1+EPS) >1
EPS = EPS/2;endEPS = 2*EPS
the break statementbreak – is used to terminate the execution of the loop.
Computer methods for structural analysis
User-Defined Function
Add the following command in the beginning of your m-file:function [output variables] = function_name (input variables);
NOTE: the function_name should be the same as your file name to avoid confusion.
calling your function:a user-defined function is called by the name of the m-file, not
the name given in the function definition.type in the m-file name like other pre-defined commands.
Input and output variables, can be of any type
Computer methods for structural analysis
Data Input
Data Inputcan be done by:
• Loading each matrix separately (function “load”)
• Running a previously written m-file
Computer methods for structural analysis
Data Input
M-data file:
• Should be written by MATLAB’s syntactic
Computer methods for structural analysis
Output to file
Creating of a results file
“fid” is a scalar MATLAB integer,
called a file identifier.
Don’t forget to close the results file
Computer methods for structural analysis
Write to file (“fprintf”)
Printing a line of information:fprintf(fid,‘\n I am talking too much\n’)Gives>> I am talking too much\n>
New line
Computer methods for structural analysis
To give a number with a decimal point: floating point format
x = 42.2;fprintf(fid,‘Load = %12.2f N’,x)Gives:Load = 42.20 N Format for output
Fixed-point notation
For some more guidance about formats use “help fprintf “
Examples of “fprintf”
Computer methods for structural analysis
Print matrixB = [8.8 7.7; 8800 7700]fprintf(fid,'X is %6.2f m or %8.3f mm\n', B)display the lines:X is 8.80 m or 8800.000 mmX is 7.70 m or 7700.000 mm
Examples of “fprintf”
Computer methods for structural analysis
Getting Help
MATLAB HelpGoogle “MATLAB helpdesk”Go to the online HelpDesk provided by www.mathworks.com
You can find EVERYTHING you need to know about MATLAB from the online HelpDesk.
Computer methods for structural analysis
Good Luck!