You are on page 1of 32

Engineering Computer

Applications
(0904 201)
Dr. Lubna
Badri
Second
Semester
2013-2014

Lecturer: Dr. Lubna Badri

Course
Overview
Office: 9108,
Engineering Building
Office Hours: Sun, Tue, & Thu. 11:00 12:00,
or by appointment
Email: Lbadri@zuj.edu.jo

Course Website:
http://lbadri.com/?page_id=355

Course Objectives

o
o
o
o

The students should be able to use an advanced


mathematical tool.
The students should be able to adopt an applied problem
and solve it with Matlab.
Upon completion of the course the students should be
able to:
Recognize possibilities and limitations with Matlab
Solve simpler problems with Simulink and Matlab
Solve problems with the use of Least Square Method
use simpler programming techniques like Decision
making Structures

Text Book
Introduction to MATLAB for Engineers William J.
Palm
.III, 2010

What is MATLAB?

A
software
environment
for
interactive
numerical computations.
MATLAB allows:
Matrix manipulations,
Plotting of functions and data,
Implementation of algorithms,
Creation of user interfaces, and
Interfacing with programs written in other
languages, including C, C++, Java, and Fortran.

Matrix computations and linear algebra


Solving nonlinear equations
Numerical solution of differential equations
Mathematical optimization
Statistics and data analysis
Signal processing
Modelling of dynamical systems
Solving partial differential equations
Simulation of engineering systems

Matlab used (on a daily basis) in many


engineering companies

Matlab Background
Matlab = Matrix Laboratory
Originally a user interface for numerical
linear
algebra routines
Commercialized 1984 by The Mathworks
Alternatives
Complements
Matrix-X
Octave
(symbolic)
Lyme

(free; GNU)
(free; Palm)

Maple (symbolic)
Mathematica

Matlab Desktop

Launch Pad
Command
Window

History

Matlab Desktop

Workspace
Command
Window

Current
DIrectory

MATLAB Demo
Demonstrations are invaluable
since they give an indication of the
MATLAB capabilities.
A comprehensive set are available
by typing the command >>demo in
MATLAB prompt.

Interactive Calculations

Matlab is interactive, no need to declare


variables
>> 2+9/3
ans =
5
>> a=5e-3; b=1; a+b
ans =
1.0050

Interactive Calculations
Most elementary functions and constants are
already defined
>> cos(pi)
>> abs(1+i)
>> sin(pi)
Last call gives answer 1.2246e-016 !?

Variable and Memory


Management

Matlab uses double precision (approx. 16


significant digits)
>> format long
>> format compact

All variables are shown with


>> who
>> whos

Variables can be stored on file


>> save filename
>> clear
>> load filename

Variables
Dont have to declare type
Dont even have to initialise
Just assign in command window
>>
>> a=12; % variable a is assigned 12

Variables

View variable contents by simply typing the variable


name at the command prompt
>> a
a=
12
>>
>> a*2
a=
24
>>

Workspace

The workspace is Matlabs memory


Can manipulate variables stored in the
workspace

>> b=10;
>> c=a+b
c=
22
>>

Scalar Arithmetic Operations

Order
of Precedence
1.
Parentheses,
evaluated starting
innermost pair.

with

the

1. Exponentiation, evaluated from left to right.


2. Multiplication and division with
precedence, evaluated from left to right.

equal

4. Addition and subtraction with


precedence, evaluated from left to right.

equal

=The
Assignment
Operator
Typing x = 3 assigns the value 3 to the variable

x.
We can then type x = x + 2.
This assigns the value This assigns the value 3 + 2
= 5 to. x. But in algebra this implies that 0 =
2.
In algebra we can write x + 2 = 20, but in
MATLAB we cannot.
In MATLAB the left side of the = operator must
be a single variable.
The right side must be a computable value.

Commands for managing the work


Command
Description
session
who
Lists the variables currently in
memory.
whos
Lists the current variables and sizes,
and
indicates if they have imaginary parts.
:
Colon; generates an array
having regularly spaced elements.
,
Comma; separates elements of
an array.
;
Semicolon; suppresses screen
printing; also denotes a new row in an array.
...
Ellipsis; continues a line.

Commands for managing the work


Command
Description
session
clc
Clears the Command
window.
clear
Removes all variables from
memory.
clear v1 v2
Removes the variables v1
and v2 from memory.
exist(var)
Determines if a file or
variable exists having the name var.
quit
Stops MATLAB.

Special Variables and Constants

Complex Number Operations

The number c1= 1 2i is entered as follows: c1 = 1-2i.


An asterisk is not needed between i or j and a number,
although it is required with a variable, such as c2 = 5 i*c1.
Be careful. The expressions
y = 7/2*i
and
x = 7/2i
give two different results: y = (7/2)i = 3.5i
and x = 7/(2i) = 3.5i.

Vectors and Matrices

Vectors (arrays) are defined as


>> v = [1, 2, 4, 5]
>> w = [1; 2; 4; 5]

Matrices (2D arrays) defined similarly


>> A = [1, 2, 3 ; 4, -5, 6 ; 5, -6, 7]

Arrays

The numbers 0, 0.1, 0.2, , 10 can be assigned to


the variable u by typing u = [0 : 0.1 : 10].

To compute w = 5 sin u for u = 0, 0.1, 0.2, , 10,


the variable u, the session is:
>>w = 5*sin(u);
>>u = [0:0.1:10];
The single line, w = 5*sin(u), computed the formula w =
5 sin u 101 times.

Array Index
>>u(7)
ans =
0.6000
ans =
2.8232
Use the lengthfunction to determine how many values are in
an array.
>>m = length(w)
m= 101

Matrix Operators

All common operators are overloaded

>> v + 2

Common operators are available

>> B = A
>> A*B
>> A+B

Note: Matlab is case-sensitive


A and a are two different variables

Indexing Matrices

Indexing using parentheses


>> A(2,3)

Index submatrices using vectors of row and column


indices
>> A([2 3],[1 2])

Ordering of indices is important!


>> B=A([3 2],[2 1])
>> B=[A(3,2),A(3,1);A(2,2),A(2,1)]

Indexing Matrices
Index complete row or column using
the colon operator
>> A(1,:)
Can also add limit index range
>> A(1:2,:)
>> A([1 2],:)
General notation for colon operator
>> v=1:5
>> w=1:2:5

Matrix Functions
Many elementary matrices predefined
>> help elmat;
>> I=eye(3) % EYE Identity matrix.

Specialized matrix functions and operators


>> As=sqrtm(A)
>> As^2
>> A.*A

Note: in general, .<operator> is elementwise operation

Manipulating Matrices
>>
>>
>>
>>
>>
>>
>>

A'
B*A
B.*A
B/A
B./A
[B A]
[B; A]

%
%
%
%
%
%

=A
1
0
7

3
5
2

5
2

1
4
2

% transpose
2
1
matrix multiplication
1
element by element multiplication
matrix division
element by element division
Join matrices (horizontally)
=B
1 3
Join matrices (vertically)
9
7

You might also like