You are on page 1of 8

Numerical method lab., lec.1 3rd year class By M.Sc. Hamza A.

Mech. Eng. Department University of Baghdad

Matlab
Why Matlab:
1. MATLAB is a high-level computer language for scientific computing and data
visualization.

2. It is becoming the premiere platform for scientific computing at educational


institutions and research establishments.

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.

4. MATLAB contains a large number of functions that access proven numerical


libraries, such as LINPACK and EISPACK. This means that many common tasks (e.g.,
solution of simultaneous equations) can be accomplished with a single function call.

5. There is extensive graphics support that allows the results of computations to be


plotted with a few statements.

Working with Matlab


The windows in matlab are: command window, editor window, current directory
window and history window.
To start working in matlab, specify the directory!
To clear the history commands, use the command clear.
To clean the command window, use the command clc.
MATLAB can be operated in the interactive mode through its command window, where
each command is executed immediately upon its entry. In this mode MATLAB acts like
an electronic calculator.

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

Exercise 2: Find the result of (x2+24y)-6 where

a) x=2, y=8 b) x=-4, y= 0.3 π (pi = π = 3.1416)

Matrices and vectors:


Input column vector
>> 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

Define vector by beginning, step size and ending:


>> b=1:2:8
b=
1 3 5 7
Exercise 3: Input x=3,6,9,12,…….60 y1= 5,10,15,20,….100
3
Numerical method lab., lec.1 3rd year class By M.Sc. Hamza A.
Mech. Eng. Department University of Baghdad

The transpose of any vector or matrix:


The transpose of the vector b is b' and for the matrix A is A'
>> b = [1 2 3]
b=123
>> b'
ans =
1
2
3
>> A = [2 1 0; -1 2 2; 0 1 4]
A=
2 1 0
-1 2 2
0 1 4
>> A'
ans =
2 -1 0
1 2 1
0 2 4

Accessing elements of a matrix:


The elements of a matrix, such as

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.

Matlab file (m-file):


All the commands and codes could be saved as m-file and run by calling the m-file in
the command window or execute the m-file from the run icon in the editor window.
1. Select the icon new in the editor window
2. Write the commands that you want in the editor window
e.g.
c=2;
d=5;
e=c*d
3. Select the save icon in the editor window
5
Numerical method lab., lec.1 3rd year class By M.Sc. Hamza A.
Mech. Eng. Department University of Baghdad

4. Select the directory to save the new .m file e.g. work


5. Give the .m file a name. e.g. add.m
6. Type the name of the .m file in the command window i.e. add then inter
7. The result in the command window is
e=
10

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

>> for n = 0:5 % n loops over the sequence 0 1 2 3 4 5


y(n+1) = cos(n*pi/10);
end
>> y
y=
1.0000 0.9511 0.8090 0.5878 0.3090 0.0000
6
Numerical method lab., lec.1 3rd year class By M.Sc. Hamza A.
Mech. Eng. Department University of Baghdad
Exercise 5: Input x = 3,6,9,12,…60 and y = x2 - 4

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

>> for i=1:100;


ny(i)=nx(i)^2;
end

>> 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

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.

3. input x=3,6,9,12,…….60 y1= 5,10,15,20,….100 y2=(x(1))2-4,( x(2))2-4 , (x(3))2-


4,…. (x(60))2-4

Then plot a) x and y1 b) x and y2


8

You might also like