numerical methods for engineers and scientists: an introduction with applications using matlab
TRANSCRIPT
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
Slide 2
Copyright © 2014 John Wiley & Sons, Inc. All rights reserved.
Third Edition
Amos Gilat • Vish Subramaniam
Numerical Methods for
Engineers and Scientists
Slide 3
Lecturer: Assistant Prof. Dr. Aydin Azizi
Introduction to MATLAB
Lecturer: Assistant Prof. Dr. Aydin Azizi
Slide 4
Lecturer: Assistant Prof. Dr. Aydin Azizi
Example of MATLAB Release 13 desktop
Lecturer: Assistant Prof. Dr. Aydin Azizi
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
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
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
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
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
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
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
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
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
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
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
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
Slide 17
Lecturer: Assistant Prof. Dr. Aydin Azizi
Roots- Logarithms
Slide 18
Lecturer: Assistant Prof. Dr. Aydin Azizi
Random Number
Slide 19
Lecturer: Assistant Prof. Dr. Aydin Azizi
Random Real Number
Slide 20
Lecturer: Assistant Prof. Dr. Aydin Azizi
Random Integer Number
Slide 21
Lecturer: Assistant Prof. Dr. Aydin Azizi
Practice
Slide 22
Lecturer: Assistant Prof. Dr. Aydin Azizi
Characters and Encoding
Slide 23
Lecturer: Assistant Prof. Dr. Aydin Azizi
Characters and Encoding
Slide 24
Lecturer: Assistant Prof. Dr. Aydin Azizi
Characters and Encoding
Slide 25
Lecturer: Assistant Prof. Dr. Aydin Azizi
Practice
Slide 26
Lecturer: Assistant Prof. Dr. Aydin Azizi
Relational Expressions
Slide 27
Lecturer: Assistant Prof. Dr. Aydin Azizi
Relational Expressions
Slide 28
Lecturer: Assistant Prof. Dr. Aydin Azizi
Relational Expressions
Slide 29
Lecturer: Assistant Prof. Dr. Aydin Azizi
Logical Operators
Slide 30
Lecturer: Assistant Prof. Dr. Aydin Azizi
Logical Operators
Slide 31
Lecturer: Assistant Prof. Dr. Aydin Azizi
Logical Operators
Slide 32
Lecturer: Assistant Prof. Dr. Aydin Azizi
Truth Table for Logical Operators
Slide 33
Lecturer: Assistant Prof. Dr. Aydin Azizi
Logical Error
Slide 34
Lecturer: Assistant Prof. Dr. Aydin Azizi
Logical Error
Slide 35
Lecturer: Assistant Prof. Dr. Aydin Azizi
Practice
Slide 36
Lecturer: Assistant Prof. Dr. Aydin Azizi
While Loop
count = 0;number = 8;while number > 3number = number - 2count = count+1end
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
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