numerical methods for engineers and scientists: an introduction with applications using matlab

38
This document is intended for internal use only and shall not be distributed outside of GUtech in Oman Numerical Methods for Engineers and Scientists Lecturer: Assistant Prof. Dr. AYDIN AZIZI

Upload: aydin-azizi

Post on 05-Apr-2017

146 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

This document is intended for internal use only and shall not be distributed outside of GUtech in Oman

Numerical Methods for Engineers and Scientists

Lecturer: Assistant Prof. Dr. AYDIN AZIZI

Page 2: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 2

Copyright © 2014 John Wiley & Sons, Inc. All rights reserved.

Third Edition

Amos Gilat • Vish Subramaniam

Numerical Methods for

Engineers and Scientists

Page 3: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 3

Lecturer: Assistant Prof. Dr. Aydin Azizi

Introduction to MATLAB

Lecturer: Assistant Prof. Dr. Aydin Azizi

Page 4: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 4

Lecturer: Assistant Prof. Dr. Aydin Azizi

Example of MATLAB Release 13 desktop

Lecturer: Assistant Prof. Dr. Aydin Azizi

Page 5: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 5

Lecturer: Assistant Prof. Dr. Aydin Azizi

Variables – Vectors and Matrices –

• ALL variables are matrices

Variables•They are case–sensitive i.e. x X•Their names can contain up to 31 characters•Must start with a letter•Variables are stored in workspace

e.g. 1 x 1 4 x 1 1 x 4 2 x 4

42396512 7123

3923 4

Lecturer: Assistant Prof. Dr. Aydin Azizi

Page 6: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 6

Lecturer: Assistant Prof. Dr. Aydin Azizi

How do we assign a value to a variable?

>>> v1=3

v1 =

3

>>> i1=4

i1 =

4

>>> R=v1/i1

R =

0.7500

>>>

>>> whos

Name Size Bytes Class

R 1x1 8 double array

i1 1x1 8 double array

v1 1x1 8 double array

Grand total is 3 elements using 24 bytes

>>> who

Your variables are:

R i1 v1

>>>

Lecturer: Assistant Prof. Dr. Aydin Azizi

Page 7: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 7

Lecturer: Assistant Prof. Dr. Aydin Azizi

How do we assign values to vectors?>>> A = [1 2 3 4 5]A = 1 2 3 4 5 >>>

>>> B = [10;12;14;16;18]B = 10 12 14 16 18>>>

A row vector values are separated by

spaces 54321A

1816141210

BA column vector

values are separated by semi–colon (;)

Lecturer: Assistant Prof. Dr. Aydin Azizi

Page 8: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 8

Lecturer: Assistant Prof. Dr. Aydin Azizi

How do we assign values to vectors?• If we want to construct a vector of, say, 100 elements between 0 and 2 – linspace

>>> c1 = linspace(0,(2*pi),100);

>>> whos

Name Size Bytes Class

c1 1x100 800 double array

Grand total is 100 elements using 800 bytes

>>>

Lecturer: Assistant Prof. Dr. Aydin Azizi

Page 9: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 9

Lecturer: Assistant Prof. Dr. Aydin Azizi

How do we assign values to vectors?

If we want to construct an array of, say, 100 elements between 0 and 2 – colon notation

>>> c2 = (0:0.0201:2)*pi;

>>> whos Name Size Bytes Class c1 1x100 800 double array c2 1x100 800 double arrayGrand total is 200 elements using 1600 bytes>>>

Lecturer: Assistant Prof. Dr. Aydin Azizi

Page 10: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 10

Lecturer: Assistant Prof. Dr. Aydin Azizi

How do we assign values to matrices ?

Columns separated by space or a comma

Rows separated by semi-colon

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

987654321

Lecturer: Assistant Prof. Dr. Aydin Azizi

Page 11: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 11

Lecturer: Assistant Prof. Dr. Aydin Azizi

How do we access elements in a matrix or a vector?

>>> A(2,3)ans = 6

>>> A(:,3)ans = 3 6 9

>>> A(1,:)ans = 1 2 3

>>> A(2,:)ans = 4 5 6

Lecturer: Assistant Prof. Dr. Aydin Azizi

Page 12: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 12

Lecturer: Assistant Prof. Dr. Aydin Azizi

Some special variables

pi ()

inf (e.g. 1/0)

i, j ( )1

>>> 1/0Warning: Divide by zero.ans = Inf>>> pians = 3.1416>>> ians = 0+ 1.0000i

Lecturer: Assistant Prof. Dr. Aydin Azizi

Page 13: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 13

Lecturer: Assistant Prof. Dr. Aydin Azizi

Arithmetic operations – Matrices

Performing operations to every entry in a matrix

Add and subtract>>> A=[1 2 3;4 5 6;7 8 9]A = 1 2 3 4 5 6 7 8 9>>>

>>> A+3ans = 4 5 6 7 8 9 10 11 12

>>> A-2ans = -1 0 1 2 3 4 5 6 7

Lecturer: Assistant Prof. Dr. Aydin Azizi

Page 14: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 14

Lecturer: Assistant Prof. Dr. Aydin Azizi

Arithmetic operations – Matrices

Performing operations to every entry in a matrix

Multiply and divide>>> A=[1 2 3;4 5 6;7 8 9]A = 1 2 3 4 5 6 7 8 9>>>

>>> A*2ans = 2 4 6 8 10 12 14 16 18

>>> A/3ans = 0.3333 0.6667 1.0000 1.3333 1.6667 2.0000 2.3333 2.6667 3.0000

Lecturer: Assistant Prof. Dr. Aydin Azizi

Page 15: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 15

Lecturer: Assistant Prof. Dr. Aydin Azizi

Arithmetic operations – Matrices

Performing operations to every entry in a matrixPower

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

A^2 = A * A

To square every element in A, use the element–wise operator .^

>>> A.^2ans = 1 4 9 16 25 36 49 64 81

>>> A^2ans = 30 36 42 66 81 96 102 126 150

Lecturer: Assistant Prof. Dr. Aydin Azizi

Page 16: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 16

Lecturer: Assistant Prof. Dr. Aydin Azizi

Performing operations between matrices>>> A=[1 2 3;4 5 6;7 8 9]A = 1 2 3 4 5 6 7 8 9

>>> B=[1 1 1;2 2 2;3 3 3]B = 1 1 1 2 2 2 3 3 3

A*B

333222111

987654321

A.*B

3x93x83x72x62x52x41x31x21x1

27242112108321

=

=

505050323232141414

Arithmetic operations – Matrices

Lecturer: Assistant Prof. Dr. Aydin Azizi

Page 17: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 17

Lecturer: Assistant Prof. Dr. Aydin Azizi

Roots- Logarithms

Page 18: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 18

Lecturer: Assistant Prof. Dr. Aydin Azizi

Random Number

Page 19: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 19

Lecturer: Assistant Prof. Dr. Aydin Azizi

Random Real Number

Page 20: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 20

Lecturer: Assistant Prof. Dr. Aydin Azizi

Random Integer Number

Page 21: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 21

Lecturer: Assistant Prof. Dr. Aydin Azizi

Practice

Page 22: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 22

Lecturer: Assistant Prof. Dr. Aydin Azizi

Characters and Encoding

Page 23: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 23

Lecturer: Assistant Prof. Dr. Aydin Azizi

Characters and Encoding

Page 24: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 24

Lecturer: Assistant Prof. Dr. Aydin Azizi

Characters and Encoding

Page 25: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 25

Lecturer: Assistant Prof. Dr. Aydin Azizi

Practice

Page 26: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 26

Lecturer: Assistant Prof. Dr. Aydin Azizi

Relational Expressions

Page 27: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 27

Lecturer: Assistant Prof. Dr. Aydin Azizi

Relational Expressions

Page 28: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 28

Lecturer: Assistant Prof. Dr. Aydin Azizi

Relational Expressions

Page 29: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 29

Lecturer: Assistant Prof. Dr. Aydin Azizi

Logical Operators

Page 30: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 30

Lecturer: Assistant Prof. Dr. Aydin Azizi

Logical Operators

Page 31: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 31

Lecturer: Assistant Prof. Dr. Aydin Azizi

Logical Operators

Page 32: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 32

Lecturer: Assistant Prof. Dr. Aydin Azizi

Truth Table for Logical Operators

Page 33: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 33

Lecturer: Assistant Prof. Dr. Aydin Azizi

Logical Error

Page 34: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 34

Lecturer: Assistant Prof. Dr. Aydin Azizi

Logical Error

Page 35: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 35

Lecturer: Assistant Prof. Dr. Aydin Azizi

Practice

Page 36: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 36

Lecturer: Assistant Prof. Dr. Aydin Azizi

While Loop

count = 0;number = 8;while number > 3number = number - 2count = count+1end

Page 37: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 37

Lecturer: Assistant Prof. Dr. Aydin Azizi

FOR Loop

mat=rand(5,6) [r c] = size(mat)for i = 1:r for j = 1:cmat(i,j) = mat(i,j) * 2endend

Page 38: Numerical Methods for Engineers and Scientists: An Introduction with Applications Using MATLAB

Slide 38

Lecturer: Assistant Prof. Dr. Aydin Azizi

IF Loop

x=rand(10,1);v = [ ]for i = 1:length(x)if x(i) > 0v = [v i]endend