Professional Documents
Culture Documents
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
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
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
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
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 or j = −1
Conditionals:
if, else, elseif
if condition
block
end
if condition
block
elseif condition
block
...
end
Loops:
For
for target = sequence
block
end
Plotting:
>> x=3:3:60;
>> nx=1:100; >> length (x)
>> ny=2:2:200; >> for n=1:20
>> plot(nx,ny) y(n)=(x(n)^2)-4;
end
>> y
>> plot(nx,ny)
>> x=0:0.01*pi:2*pi;
>> length(x)
ans =
201
7
Numerical method lab., lec.1 3rd year class By M.Sc. Hamza A.
Mech. Eng. Department University of Baghdad
>> for i=1:201;
y=sin(x);
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
b) V1*V2
c) V2*V1
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.