Topic 1 - Introduction to MATLAB-Copy
Topic 1 - Introduction to MATLAB-Copy
Subject Tutor:
Tutor: Dr.Dr.
WailThamir
Sami Al-Dulaimi
Sarsam
Numerical method lab., lec.1 3rd year class By M.Sc. Hamza A.
Mech. Eng. Department University of Baghdad
Introduction to Matlab
Why Matlab:
1. MATLAB is a high-level computer language for scientific computing and data
visualization.
3. The great advantage of an interactive system is that programs can be tested and
debugged quickly, allowing the user to concentrate more on the principles behind the
program and less on programming itself.
1
Subject Tutor: Dr. Wail Sami Sarsam
Numerical method lab., lec.1 3rd year class By M.Sc. Hamza A.
Mech. Eng. Department University of Baghdad
Arithmetic Operations
Operation Description
+ Addition
- Subtraction
* Multiplication
/ Right divide
\ Left divide (matrix\vector)
inv Matrix inversion
' Vector and matrix transpose
^ power
Relational operators
eq - Equal ==
ne - Not equal ~=
lt - Less than <
gt - Greater than >
le - Less than or equal <=
ge - Greater than or equal >=
Logical operators
and - Element-wise logical AND &
or - Element-wise logical OR |
not - Logical NOT ~
The symbol >> is MATLAB’s prompt for input. The percent sign (%) marks the
beginning of a comment. A semicolon (;) has two functions: it suppresses printout of
intermediate results and separates the rows of a matrix. Without a terminating
semicolon, the result of a command would be displayed.
Exercise 1: Type x then type x=10; what is the difference?
Any variable used in matlab should has a value or it should be defined as a symbolic
objects, to define any variable as a symbolic objects we use the command syms .
Example
>> x
??? Undefined function or variable 'x'.
>> x=10
x=
10
2
Subject Tutor: Dr. Wail Sami Sarsam
Numerical method lab., lec.1 3rd year class By M.Sc. Hamza A.
Mech. Eng. Department University of Baghdad
or
>> syms x
>> x
x=
x
or
>> syms x y
>> x+y^2
ans =
x+y^2
x= [ 2 -4]
2 y = [8 0.3*pi]
Exercise 2: Find the result of (x +24y)-6 where z = (x.^2 +24*y)-6
a) x=2, y=8 b) x=-4, y= 0.3 π (pi = π = 3.1416) for x= [ 2 -4]
for y = [8 0.3*pi]
z = (x^2 +24*y)-6
Matrices and vectors: end
Input column vector end
>> b = [1; 2; 3]
b=
1
2
3
Row vector
>> b = [1 2 3]
b=
123
Input 3 x 3 matrix
>> A = [2 1 0; -1 2 2; 0 1 4]
A=
2 1 0
-1 2 2
0 1 4
can be accessed with the statement A(i,j), where i and j are the row and column
numbers, respectively. A section of an array can be extracted by the use of colon
notation. Here is an illustration:
>> A = [8 1 6; 3 5 7; 4 9 2]
A=
8 1 6
3 5 7
4 9 2
>> A(2,3) % Element in row 2, column 3
ans =
7
4
Subject Tutor: Dr. Wail Sami Sarsam
Numerical method lab., lec.1 3rd year class By M.Sc. Hamza A.
Mech. Eng. Department University of Baghdad
>> A(:,2) % Second column
ans =
1
5
9
>> A(2:3,2:3) % The 2 x 2 submatrix in lower right corner
ans =
57
92
Array elements can also be accessed with a single index. Thus A(i) extracts the ith
element of A, counting the elements down the columns. For example, A(7) and A(1,3)
would extract the same element from a 3 ×3 matrix.
e.g.
>> A = [8 1 6; 3 5 7; 4 9 2]
A=
8 1 6
3 5 7
4 9 2
>> A(7)
ans =
6
>> A(1,3)
ans =
6
Exercise 4: Modify the component in the second row and the first column of matrix A
to 10 instead of 3.
Complex number:
>> x = 1 + 3i >> i^2
i or j = −1 ans =
-1
Conditionals:
if, else, elseif
minVal = 2; 0
if condition Value is below minimum value.
maxVal = 6;
block for x = [0 2 20 1 34 3] 2
end disp (x) Value within specified range.
if (x >= minVal) & (x <= maxVal) 20
disp ('Value within specified range.') Value exceeds maximum value.
if condition elseif (x > maxVal) 1
block disp ('Value exceeds maximum value.') Value is below minimum value.
else 34
elseif condition disp ('Value is below minimum value.') Value exceeds maximum value.
block end 3
... end Value within specified range.
end
Loops:
For
for target = sequence
block
end
>> plot(nx,ny)
>> x=0:0.01*pi:2*pi;
>> length(x)
ans =
201
7
Subject Tutor: Dr. Wail Sami Sarsam
Numerical method lab., lec.1 3rd year class By M.Sc. Hamza A.
Mech. Eng. Department University of Baghdad
>> for i=1:201;
Since x is a vector and y is a function of x, no need to put y(i).
y=sin(x); This command is correct This command is wrong
>> for i=1:201; >> for i=1:201;
end y(i)=sin(x(i)); y(i)=sin(x);
end end
>>plot(x,y)
Exercises
2
1. Input the column vector V1= −7 , the row vector V2= 5 3 −1 and the vector
9
V3= −2 0 12 3 then find
a) V1-V2 what is the result? Is there an error? Instead find transpose V1'-V2
V1*V2 V2*V1
b) V1*V2 ans =
10 6 -2 ans = -20
c) V2*V1 -35 -21 7
45 27 -9
d) Modify the second component of V1 to 13 instead of -7
e) Add the third component of V3 to the first component of V2 and save the result in the
variable z.
1 0 7 3 6 11
2. Input the matrix A= 3 −9 13 and the matrix B= 22 7 2 then find
0 4 8 −1 9 5
a) A+B b) A-B c) inverse A d) transpose of B.
round ( 863178137 , -2 )
ans = 863178100