Download Lecture 2.1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to Programming for
Mechanical Engineers (ME 319)
Lecture 2
Quote of the day
“He who has overcome his
fears will truly be free.”
-
Aristotle
(384 BC – 322 BC)
Quick review of Lecture 1
1.
Work on the Command window by creating variables
and performing simple mathematical expressions.
2.
Assign proper variables and files names.
3.
Assigning a value to a variable.
4.
Writing your commands inside a script file.
5.
Properly comment words or any other text.
6.
Run the script file.
Order of Precedence

A scalar variable is a variable that contains a single number. (1×1 matrix)

MATLAB uses the symbols +
-
*
/
^
for addition, subtraction,
multiplication, right division and exponentiation respectively.

\
is used for left division in which denominator comes first.
Example: 2\7 = 7/2 = 3.5
Scalar arithmetic operations
Creating Arrays and vectors
Command Syntax
Description
vector _ name  [type vector elements]
The simplest way to create simple vectors,
not convenient for complex vectors
array _ name  xi : d : x f
Where xi = the initial element in the array.
xf = the destination element in the
array.
d = the step size (Default value=1)
xi could be bigger than xf, but d should be
–ve in this case
array _ name  linspace ( xi , x f , n)
Where xi = the initial element in the array.
xf = the destination element in the
array.
n = the number of elements in
between
If n is not provided, the default value is
100
Example
line = [1 3 5]
line =
1 3 5
line = 1:0.4:5
line =
Columns 1 through 9
1.00 1.40 1.80 2.20 2.60 3.00 3.40
3.8 4.20
Columns 10 through 11
4.60 5.00
line = linspace (1,5,11)
line =
Columns 1 through 9
1.00 1.40 1.80 2.20 2.60
3.40 3.80 4.20
Columns 10 through 11
4.6000 5.0000
3.00
Creating Arrays and vectors (contd..)
zr  zeros(i, j )
on  ones(k , l )
idn  eye(m)
Creates zeros matrix of dimensions i x j
zr = zeros(2,2)
zr =
0
0
0
0
Creates ones matrix of dimensions k x l
on = ones(2,3)
on =
1
1
1
1
Creates an identity matrix of
dimensions m x m
idn = eye(2)
idn =
1
0
0
1
1
1
Creating Arrays and vectors
1.
All variables in MATLAB are arrays. A scalar variable is an array of
single element.
2.
The array variable is defined by the input when it is assigned.
Therefore there is no need to define the size of an array, since it will
take the size of the input automatically.
3.
You can still perform any operation on any array to change its name,
contents, dimensions, or type.
4.
You can fill in an array with scalar values, predefined variables, or
expressions.
Examples
>> A = 5; B = power (A,3);
% define two variables A and B
>> C = [A, 27, sqrt (B), B - A^2]
C=
5.0000 27.0000 11.1803 100.0000
% define an array C of 4 elements
>> C(4) = 99
C=
5.0000 27.0000 11.1803 99.0000
>> C(6) = 4
% assign a different value for element 4
C=
5.0000 27.0000 11.1803 99.0000
>> C (2:5)
ans =
27.0000 11.1803 99.0000
>> C (1:2:5)
ans =
5.0000 11.1803
% add two elements to the existing C array, the 5th is zero by
default
0
4.0000
% chose elements 2, 3, 4 and 5
0
% chose elements 1, 3 and 5
0
Creating Arrays and vectors (Examples
continued)
>> D = [linspace(A,B,5); 100:-20:20;
1 2 3 4 5]
D=
5 35 65 95 125
100
80
60
40
20
1
2
3
4
5
>> E = [D(2:3,2:4)]
E=
80 60 40
2
3
% define a matrix D of 5 elements between A and B
% and another 5 elements between 100 and 20
% and another 5 elements 1 2 3 4 5
% define E that is a submatrix of D
4
>> F = [C(1:3);E(1,:)]
% define matrix F that’s a combination of the 1st three elements of C
F=
5.0000 27.0000 11.1803
% as its 1st row, and the complete 1st row of E as its 2nd row
80.0000 60.0000 40.0000
>> F(2,2) = F(2,1) - F(2,3) + 10*E(2,3)
F=
5.0000 27.0000 11.1803
80.0000 80.0000 40.0000
% change element (2,2) of matrix F using the expression to the right
Creating Arrays and vectors (Examples
continued)
>> G = F‘
G=
5.0000 80.0000
27.0000 80.0000
11.1803 40.0000
% define matrix G as the transpose of F
>> G (1,:)
% Choose the 1st row in matrix G
ans =
5 80
>> H = G(1:2,:)
% define matrix H as the complete 1st two rows of G
H=
5 80
27 80
>> I = inv (H)
% define matrix I as the inverse of H
I=
-0.0455 0.0455
0.0153 -0.0028
>> J = eig (I)
J=
-0.0581
0.0098
% define matrix J as the eigen values of the square matrix I
Creating Arrays and vectors
Examples (continue):
% add complete 2nd and 3rd columns to the existing matrix J
>> J (:,2:3) = [1 2;3 4]
J=
-0.0581
1.0000
2.0000
0.0098
3.0000
4.0000
>> K = [J F]
% define matrix K as the combination of J and F
K=
% setting next to each others
-0.0581
1.0000
2.0000
5.0000 27.0000 11.1803
0.0098
3.0000
4.0000 80.0000 80.0000 40.0000
>> L = [F ; J]
% define matrix L as the combination of J and F
L=
% setting over each others
5.0000 27.0000 11.1803
80.0000 80.0000 40.0000
-0.0581
1.0000
2.0000
0.0098
3.0000
4.0000
>> L (1:3,:) = [ ]
% delete the complete 1st three rows of L by replacing the
L=
% rows with the empty matrix notation [ ]
0.0098
3.0000
4.0000
>> L (1) = [ ]
% delete the 1st element of L by replacing its content with
L=
% the empty matrix notation [ ]
3
4
Built-in functions for handling arrays
Examples (continue):
>> length_J = length (J)
length_J =
3
% check the maximum number of elements in a row or a column
>> [row_J , column_J] = size (J)% return the size of J
row_J =
2
column_J =
3
>>M = rand (3)
% create a random matrix M of dimensions 3x3
M=
0.9218
0.4057
0.4103
0.7382
0.1763
0.9355
0.9169
0.8936
0.0579
>> N = diag(M)
N=
0.9218
0.9355
0.0579
>> O = [6 -12 20]
O=
6 -12
% create a vector of diagonal elements in M
20
>> P = diag (O) % create a matrix of diagonal
P=
% elements in O
6 0 0
0 -12 0
0 0 20
Creating Strings and Strings as variables
Examples (continue):
SYNTAX:
Variable _ name ' string1'
>> student_name = 'Robert Smith'
student_name =
Robert Smith
>> student_name (8)
ans =
S
SYNTAX:
string _ name  char (' string1' , ' string 2' , ' string 3' ,...)
>> student_info = char ('student info=’ , student_name , ‘Grade’, 'B+')
student_info =
student name
Robert Smith
Grade
B+
Thank you for the attention
Any Questions and
Suggestions please…