You are on page 1of 29

Advanced Structural Analysis

ACES 410
MATLAB OVERVIEW

Spring 2010
Dr Petros Christou
p.christou@frederick.ac.cy
http://staff.fit.ac.cy/eng.cp/
MATLAB
MATLAB is a software package for numerical computation.
It offers a powerful programming language, excellent
graphics, and a wide range of expert knowledge.

•MATLAB stands for “MAThematics LABoratory” and it is


published by and a trademark of The MathWorks, Inc.
•The results are numerical and inexact, thanks to the
rounding errors inherent in computer arithmetic.

MATLAB OVERVIEW ACES 410 : SPRING 2010 2


MATLAB DESKTOP
Command Window: This is where we enter
commands at the prompt: >>
– Workspace: Shows the variables that are
currently defined and some information about
their contents.
– Current Directory: Shows the files which
MATLAB can see.
– Command History: Shows the commands
that were previously used.

MATLAB OVERVIEW ACES 410 : SPRING 2010 3


MATLAB DESKTOP cont
Set Current Directory

MATLAB DESKTOP
Set
Current Workspace/ Prompt: Enter Commands
Tab Current Directory Here
Window

Command
Window

Command
History
Window

Activate Launch Pad

MATLAB OVERVIEW ACES 410 : SPRING 2010 4


• Arithmetic Operators
+ Addition
- Subtraction
* Multiplication
/ Division
^ Raise to Power
• Assignments
– Use the Equal Sign to Assign Values to
Variables
• a=4, b=3
• Comments
– Use % to add comments
MATLAB OVERVIEW ACES 410 : SPRING 2010 5
• Example
– (2+3-7)*5
– 4*3/2+7
– (2^4+3)/4^2
– a=2, c=4
– d=a+b-2
– % This is a comment
– e=c^a+7/4
• Suppress the Output of a Command with ;
– c+d+e;
– a=2*a-c;
– b=c/d-4^3+50;
MATLAB OVERVIEW ACES 410 : SPRING 2010 6
• Built in Variables
– ans : default value of expression
– pi : π
– Inf : ∞
– NaN : Not a Number (0/0)
– realmax : largest positive number
– realmin : smallest positive number
• Getting Help
– help functionName

MATLAB OVERVIEW ACES 410 : SPRING 2010 7


• Scalars, Vectors and Matrices
– All MATLAB variables are arrays. An array is
a collection of values referred to by a single
variable name. The individual elements of an
array are stored and extracted by specifying
the index of the element from the beginning of
the array.
– A Scalar is a one by one array.
– A Vector is a one dimensional array. A row
vector consists of only one row and a column
vector of only one column.
– A Matrix is a two dimensional array.
MATLAB OVERVIEW ACES 410 : SPRING 2010 8
• Define a row vector in MATLAB
>> v=[2 4 5]

v =

2 4 5

• Define a column vector in MATLAB


>> k=[2;4;3]

k =

2
4
3

MATLAB OVERVIEW ACES 410 : SPRING 2010 9


• Define a matrix in MATLAB
>> m1=[1 2 3; 4 5 6; 7 8 9]

m1 =

1 2 3
4 5 6
7 8 9

>> m2=[1 2 3
4 5 6
7 8 9]

m2 =

1 2 3
4 5 6
7 8 9

MATLAB OVERVIEW ACES 410 : SPRING 2010 10


• Built In Functions for Vectors/Matrices
– diag : matrix with specified diagonal entries or
extracts the diagonal entries of a matrix
– eye : Identity matrix
– ones : Matrix filled with ones
– rand : Matrix filled with random numbers
– zeros: Matrix filled with zeroes
– length: Number of elements in a vector
– linspace: Row vector with linearly spaced
elements
– size : Number of rows and columns in a
matrix
MATLAB OVERVIEW ACES 410 : SPRING 2010 11
• Example
>> d=eye(2,3) >> v=[1 2 3];
d = >> c=diag(v)
1 0 0 c =
0 1 0 1 0 0
0 2 0
>> a=ones(2,2) 0 0 3
a =
1 1 >> e=linspace(0,.2,3)
1 1 e =
0 0.1000 0.2000
>> b=zeros(2,2)
b = >> length (e)
0 0 ans =
0 0
3

MATLAB OVERVIEW ACES 410 : SPRING 2010 12


• Colon Notation (:)
– The colon notation can be used to create vectors or to
extract ranges of matrix elements. There are two
forms of notation.
• Vector = startValue:endValue
• Vector = startValue:increment:endValue
>> v1=1:5
v1 =
1 2 3 4 5

>> v2=0.0:0.1:0.7
v2 =
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000

>> a=[1 2 3; 4 5 6; 7 8 9]
a =
1 2 3
4 5 6
7 8 9

>> a(2:3,1)
ans =
4
7

MATLAB OVERVIEW ACES 410 : SPRING 2010 13


• Mathematical Operations With Matrices
– The basic arithmetic operations of addition,
subtraction and multiplication may be applied directly
to matrix variables, provided that the particular
operation is legal under the rules of linear algebra.

>> u=[10 9 8];


>> v=[1 2 3];
>> u+v
ans =
11 11 11

>> u-v
ans =
9 7 5

>> a=ones(2,3);b=[1 2;3 4;5 6];


>> a*b
ans =
9 12
9 12

MATLAB OVERVIEW ACES 410 : SPRING 2010 14


• Strings
– Strings are matrices with character elements.

>> first = 'Petros';


>> last= 'Christou';
>> name=[first,' ',last]
name =
Petros Christou

>> sentence='The red ball is on the table';


>> subject = sentence(9:12)
subject =
ball

>> color=sentence(5:8)
color =
red

MATLAB OVERVIEW ACES 410 : SPRING 2010 15


• Plotting in MATLAB
– Line Plots (y=f(x), or a set of points)
– Contour Plots
– Surface Plots
• Line Plots
– ezplot : plots a funtion
– plot : plots a set of points
>> v1
v1 =
1 2 3 4 5

>> v2
v2 =
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000

>> v2=v2(1:5)
v2 =
0 0.1000 0.2000 0.3000 0.4000

>> plot(v1,v2)

MATLAB OVERVIEW ACES 410 : SPRING 2010 16


• Use plot
>> v1
v1 =
1 2 3 4 5
>> v2
v2 =
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000
>> v2=v2(1:5)
v2 =
0 0.1000 0.2000 0.3000 0.4000
>> plot(v1,v2)

• Use ezplot
>> ezplot sin(x)

MATLAB OVERVIEW ACES 410 : SPRING 2010 17


MATLAB Programming cont
• MATLAB m-files
– Script m-files: Useful for automating simple
tasks. They have no input or output
parameters, and they share variables with the
command workspace.
– Function m-files: They are programs with
input and output parameters.
• General on m-files
– m-files must be stored in plain text with an
extension “.m”.
MATLAB OVERVIEW ACES 410 : SPRING 2010 18
• Script m-files
– The following code is saved to the file task1.m
% Remove all variable definitions
clear all
% Remove old graphics windows
close all
% Display the command lines in the command window
echo on
%Turn on 15 digit display
format long
% Define the vector of values of the independent variable
x=[0.1, 0.01, 0.001];
% Compute the desired values
y=sin(x)./x
% These values illustrate the fact that the limit of
% sin(x)/x as x approaches 0 is equal to 1
echo off

MATLAB OVERVIEW ACES 410 : SPRING 2010 19


– Type task1 at the prompt to obtain the result
>> task1
%Turn on 15 digit display
format long
% Define the vector of values of the independent variable
x=[0.1, 0.01, 0.001];
% Compute the desired values
y=sin(x)./x
y =
0.99833416646828 0.99998333341667 0.99999983333334
% These values illustrate the fact that the limit of
% sin(x)/x as x approaches 0 is equal to 1
echo off
>>

– Note that x and y have the values assigned in


the script file
>> x,y
x =
0.10000000000000 0.01000000000000 0.00100000000000
y =
0.99833416646828 0.99998333341667 0.99999983333334

MATLAB OVERVIEW ACES 410 : SPRING 2010 20


• Function m-files
– They are the main tool to extend the
capabilities of MATLAB. They are subprograms
analogous to those of other programming
languages.
– Syntax:
Function [outputParameterList]=functionName(inputParameterList)

Where: inputParameterList and outputParameterList are comma


separated lists of variables used to pass data into and out of
the function.

• Example
function s = threesum(x,y,z)
% Add three variables and return the result
s=x+y+z;

MATLAB OVERVIEW ACES 410 : SPRING 2010 21


• Inline Functions
– Inline can be used to define functions.
– Syntax:
• f = inline(‘myFunction’, ’independentVariable’)
where :‘myFunction’ is the function to be evaluated
(i.e. f(x)=x^2+x+1)
‘independentVariable’ is optional and it
represents the independent variable of the
function (i.e. ‘x’). In the case when it is not
specified then MATLAB makes a guess
for the variable.
>> f=inline('x^2+x+1','x')
f =
Inline function:
f(x) = x^2+x+1

>> f(4)
ans =
21
MATLAB OVERVIEW ACES 410 : SPRING 2010 22
• Flow controls
– if statements
– switch statements
– for loops
– while loops
– continue statements
– break statements

MATLAB OVERVIEW ACES 410 : SPRING 2010 23


• if statement
– The if statement evaluates a logical
expression and executes a group of
statements when the expression is true. The
optional elseif and else keywords provide for
the execution of alternate groups of
statements.
– Syntax:
if expression
block of statements
elseif expression
block of statements
else
block of statements
end

MATLAB OVERVIEW ACES 410 : SPRING 2010 24


• Switch statement
– The switch statement executes groups of
statements based on the value of a variable
or expression. The keywords case and
otherwise delineate the groups.
– Syntax:
switch expression
case 0
block of statements
case 1
block of statements
otherwise
block of statements
end

MATLAB OVERVIEW ACES 410 : SPRING 2010 25


• for loop
– The for loop repeats a group of statements a
fixed, predetermined number of times.
– Syntax:
for index=startValue:endValue
block of statements
end

MATLAB OVERVIEW ACES 410 : SPRING 2010 26


• while loop
– The while loop repeats a group of statements
an indefinite number of times under the
control of a logical condition.
– Syntax:
while logicalCondition
block of statements
end

MATLAB OVERVIEW ACES 410 : SPRING 2010 27


• Continue statement
– The continue statement passes control to the
next iteration of the for or while loop in which
it appears, skipping any remaining statements
in the body of the loop.
• Break statement
– The break statement allows you to exit early
from a for or a while loop.

MATLAB OVERVIEW ACES 410 : SPRING 2010 28


• Example
– Create the following m-file and call it counter
a=[1 3 4 5 0 5 6 0 0 2];
icount=0;
for i=1:length(a);
if a(i)~0;
icount=icount+1;
else
continue;
end
end
icount

– Call counter from MATLAB


>> counter

icount =

MATLAB OVERVIEW ACES 410 : SPRING 2010 29

You might also like