Upload
ahmad
View
231
Download
0
Tags:
Embed Size (px)
DESCRIPTION
matlab
Citation preview
Engineering Computer Applications (0904 201)
Dr. Lubna BadriSecond Semester2013-2014
Course Overview
Lecturer: Dr. Lubna Badri
Office: 9108, Engineering Building Office Hours: Sun, Tue, & Thu. 11:00 – 12:00,
or by appointment Email: [email protected]
Course Website:http://lbadri.com/?page_id=355
Course Objectives
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:
o Recognize possibilities and limitations with Matlabo Solve simpler problems with Simulink and Matlabo Solve problems with the use of Least Square Methodo 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.
Examples:
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 Maple (symbolic)
Octave (free; GNU) Mathematica(symbolic) Lyme (free; Palm)
Matlab Desktop
Command Window
Launch Pad
History
Matlab Desktop
Command Window
Workspace
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
Don’t have to declare type Don’t 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 Matlab’s memory Can manipulate variables stored in the
workspace
>> b=10;>> c=a+bc = 22>>
Scalar Arithmetic Operations
Order of Precedence1. Parentheses, evaluated starting with the
innermost pair.
1. Exponentiation, evaluated from left to right.
2. Multiplication and division with equal precedence, evaluated from left to right.
4. Addition and subtraction with equal precedence, evaluated from left to right.
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 session
Command Description 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 session
Command Description
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.6000ans = 2.8232Use 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 MatricesIndex 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 ' % transpose>> B*A % matrix multiplication>> B.*A % element by element multiplication>> B/A % matrix division>> B./A % element by element division>> [B A] % Join matrices (horizontally)>> [B; A] % Join matrices (vertically)
A=
3 2 1
5 1 0
2 1 7
B=
1 3 1
4 9 5
2 7 2