Top Banner
Engineering Computer Applications (0904 201) Dr. Lubna Badri Second Semester 2013-2014
32
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 an Overview of MATLAB 1

Engineering Computer Applications (0904 201)

Dr. Lubna BadriSecond Semester2013-2014

Page 2: 1 an Overview of MATLAB 1

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

Page 3: 1 an Overview of MATLAB 1

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

Page 4: 1 an Overview of MATLAB 1

Text Book

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

Page 5: 1 an Overview of MATLAB 1

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.

Page 6: 1 an Overview of MATLAB 1

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

Page 7: 1 an Overview of MATLAB 1

Matlab used (on a daily basis) in many engineering companies

Page 8: 1 an Overview of MATLAB 1

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)

Page 9: 1 an Overview of MATLAB 1

Matlab Desktop

Command Window

Launch Pad

History

Page 10: 1 an Overview of MATLAB 1

Matlab Desktop

Command Window

Workspace

Current DIrectory

Page 11: 1 an Overview of MATLAB 1

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.

Page 12: 1 an Overview of MATLAB 1

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

Page 13: 1 an Overview of MATLAB 1

Interactive Calculations

Most elementary functions and constants are already defined>> cos(pi)>> abs(1+i)>> sin(pi)

Last call gives answer 1.2246e-016 !?

Page 14: 1 an Overview of MATLAB 1

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

Page 15: 1 an Overview of MATLAB 1

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

Page 16: 1 an Overview of MATLAB 1

Variables View variable contents by simply typing the variable

name at the command prompt >> a

a = 12 >> >> a*2

a = 24 >>

Page 17: 1 an Overview of MATLAB 1

Workspace The workspace is Matlab’s memory Can manipulate variables stored in the

workspace

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

Page 18: 1 an Overview of MATLAB 1

Scalar Arithmetic Operations

Page 19: 1 an Overview of MATLAB 1

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.

Page 20: 1 an Overview of MATLAB 1

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.

Page 21: 1 an Overview of MATLAB 1

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.

Page 22: 1 an Overview of MATLAB 1

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.

Page 23: 1 an Overview of MATLAB 1

Special Variables and Constants

Page 24: 1 an Overview of MATLAB 1

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.

Page 25: 1 an Overview of MATLAB 1

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]

Page 26: 1 an Overview of MATLAB 1

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.

Page 27: 1 an Overview of MATLAB 1

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

Page 28: 1 an Overview of MATLAB 1

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

Page 29: 1 an Overview of MATLAB 1

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)]

Page 30: 1 an Overview of MATLAB 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

Page 31: 1 an Overview of MATLAB 1

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

Page 32: 1 an Overview of MATLAB 1

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