matlab getting started

27
Computer methods for structural analysis MATLAB GETTING MATLAB GETTING STARTING STARTING Computer methods for structural analysis 014139

Upload: waleed-usman

Post on 05-Dec-2014

410 views

Category:

Education


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Matlab getting started

Computer methods for structural analysis

MATLAB GETTING MATLAB GETTING STARTINGSTARTING

Computer methods for structural analysis014139

Page 2: Matlab getting started

Computer methods for structural analysis

Outline of lecture

Starting MATLABM-filesVariablesVectors, Matrices, and Linear AlgebraSelection ProgrammingInput and OutputGetting Help

Page 3: Matlab getting started

Computer methods for structural analysis

To start MATLAB: START PROGRAMS

MATLAB 7.0 MATLAB 7.0

StartingMATLAB

Page 4: Matlab getting started

Computer methods for structural analysis

Standard MATLAB interface

Page 5: Matlab getting started

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.

Page 6: Matlab getting started

Computer methods for structural analysis

Getting exist m-file

Open filePathHW1.m

Page 7: Matlab getting started

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

Page 8: Matlab getting started

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.

Page 9: Matlab getting started

Computer methods for structural analysis

Vectors, Matrices and Linear Algebra

Entering MatricesMatrices can be

Entered manuallyGenerated by built-in functionsLoaded from a file

Page 10: Matlab getting started

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.

Page 11: Matlab getting started

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.

Page 12: Matlab getting started

Computer methods for structural analysis

Matrix operation

Page 13: Matlab getting started

Computer methods for structural analysis

The colon operator “:”

Page 14: Matlab getting started

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

Page 15: Matlab getting started

Computer methods for structural analysis

Selection Programming

Flow ControlIf statementFor loopsWhile loopsContinue statementBreak statement

FunctionsBuilt-in functionsUser-Defined functions

Page 16: Matlab getting started

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

Page 17: Matlab getting started

Computer methods for structural analysis

If statements

Page 18: Matlab getting started

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.

Page 19: Matlab getting started

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

Page 20: Matlab getting started

Computer methods for structural analysis

Data Input

Data Inputcan be done by:

• Loading each matrix separately (function “load”)

• Running a previously written m-file

Page 21: Matlab getting started

Computer methods for structural analysis

Data Input

M-data file:

• Should be written by MATLAB’s syntactic

Page 22: Matlab getting started

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

Page 23: Matlab getting started

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

Page 24: Matlab getting started

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”

Page 25: Matlab getting started

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”

Page 26: Matlab getting started

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.

Page 27: Matlab getting started

Computer methods for structural analysis

Good Luck!