You are on page 1of 16

UNIVERSITY OF KIRKUK

MECHANICAL ENGINEERING DEPARTMENT

Second stage – morning study

Programming report

Prepared by:Bahar Salahalden


Supervised by:Dr. Ahmad yashar
2020

1
Introduction

MATLAB is a high-performance language for technical computing. It integrates


computation, visualization, and programming in an easy-to-use environment
where problems and solutions are expressed in familiar mathematical notation.
Typical uses include:

1. Math and computation.


2. Algorithm development.
3. Modeling, simulation, and prototyping.
4. Data analysis, exploration, and visualization.
5. Scientific and engineering graphics.
6. Application development, including Graphical User Interface building.

The name MATLAB stands for matrix laboratory. MATLAB was originally written
to provide easy access to matrix software developed by the LINPACK and
EISPACK projects, which together represent the state-of-the-art in software for
matrix computation.

The MATLAB language.

This is a high-level matrix/array language with control flow statements,


functions, data structures, input/output, and object-oriented programming
features. It allows both "programming in the small" to rapidly create quick and
dirty throw-away programs, and "programming in the large" to create complete
large and complex application programs.

2
Part (1)

1- Using MATLAB as a calculator

>> 1+2*3 ans = 7

>>x=1+2*3 x= 7

>>4*x ans= 28

Table 1.1: Basic arithmetic operators

Symbol Operation Example


+ Addition 9 + 12
- Subtraction 10 - 3
* Multiplication 12 * 3
/ Division 10 / 2

2 - Controlling the hierarchy of operations or precedence.

>> (1+2)*3 ans = 9

3 - Controlling the appearance of floating point number.

>> format short >> x=163.6667

>> format long >> x= -1.636666666666667e+002

4- Error messages.

>> x = 10; >> 5x

??? 5x
|
Error: Unexpected MATLAB expression.

3
5- Mathematical functions.

Table 1.2 Elementary functions

Table 1.3 : Predefined constant values

6- Basic plotting.

>> x = [1 2 3 4 5 6];
>> y = [3 -1 2 4 5 1];
>> plot(x,y)

4
Plot for the vectors x and y.

7- Multiple data sets in one plot.

>> x = 0:pi/100:2*pi;

>> y1 = 2*cos(x);

>> y2 = cos(x); >>

y3 = 0.5*cos(x);

>> plot(x,y1,’--’,x,y2,’-’,x,y3,’:’)

>> xlabel(’0 \leq x \leq 2\pi’)

>> ylabel(’Cosine functions’)

>> legend(’2*cos(x)’,’cos(x)’,’0.5*cos(x)’)

>> title(’Typical example of multiple plots’)

>> axis([0 2*pi -3

5
8- Specifying line styles and colors.

plot(x,y,’style_color_marker’)

Table 1.4 : Attributes for plot

9- Matrix generation.

6
>> v = [1 4 7 10 13]

v = 1 4 7 10 13

>> w = [1;4;7;10;13]

w=1

7
10

13

>> A = [1 2 3; 4 5 6; 7 8 9]

A= 1 2 3

4 5 6

7 8 9

10 - Matrix indexing.

>> A(3,3) = 0

A= 1 2 3
4 5 6
7 8 0

10- Deleting row or column.

>> A(3,:) = []

A= 1 2 3

4 5 6

11– Dimension.

7
>> size(A)

ans= 3 3

12 - Matrix generators.

Table 1.5 : Elementary matrices

eye(m,n) Returns an m-by-n matrix


with 1 on the main diagonal
eye(n) Returns an n-by-n square
identity matrix
zeros(m,n) Returns an m-by-n matrix
of zeros
ones(m,n) Returns an m-by-n matrix
of ones
diag (A) Extracts the diagonal of
matrix A
rand(m,n) Returns an m-by-n matrix
of random numbers

a - >> b=ones(3,1)

b= 1

b - >> eye(3)

ans = 1 0 0

0 1 0

0 0 1

13- Matrix arithmetic operations.

8
A+ B or +A A*B is valid if A and B are of the same size

A*2 is valid if A’s number of column equals B’s number of rows

A^2 is valid if A is square and equals A*A

α*A or A*α multiplies each element of A by α

14- Matrix functions

det Determinant

diag Diagonal matrices and


diagonals of a matrix

eig Eigenvalues and


eigenvectorsy

inv Matrix inverse

norm Matrix and vector


norms

ranki Number of linearly


independent rows or
columns

Table 1.6 Matrix functions

15- Anatomy of a M-File function.

function f = factorial(n). (1)

% FACTORIAL(N) returns the factorial of N. (2)

% Compute a factorial value. (3)


f = prod(1:n); (4)

>> f = factorial (5)

9
f=
120

Table 1.7 : Anatomy of a M-File function

Part no. M-file element Description

Define the function


(1) Function definition line name, and the number
and order of input and
output arguments.
A one line summary
(2) description
H1 line of the program,
displayed when you
request Help.
A more detailed
(3) Help text description of the
program.
Program code that
(4) Function body performs the actual
computations.

16- Input and output arguments.

function [outputs] = function_name(inputs)

10
Table 1.8 : Example of input and output arguments

function C=FtoC(F) One input argument and


one output argument

function area=TrapArea(a,b,h) Three inputs and one


output

function [h,d]=motion(v,angle) Two inputs and two o


output

17- The ‘‘if...end’’ structure

• if... end

• if ... else ... end

• if ... elseif ... else ... end

if expression

statements

end

Example:

discr = b*b - 4*a*c;

if discr < 0

disp(’Warning: discriminant is negative, roots are

imaginary’);

11
end

18- The ‘‘for...end’’ loop

for variable = expression

statements

end

Example

for ii=1:5

x=ii*ii

end

n = 5; A = eye(n);

for j=2:n

for i=1:j-1

A(i,j)=i/j;

A(j,i)=i/j;

end

end

19- The ‘‘while...end’’ loop

while expression

statements

end
x=1

12
while x <= 10

x = 3*x

end

13
(Part 2)
1– Differentiation (diff).

Clear

Clc

%% input

syms x

y=x

%% processing

diff (y , x ) or diff (y , x , a)

Example: y = 9*x + x^3 + 10 (diff)

Clear

Clc

%% input

Syms x

Y = 9*x + x^3 + 10 ;

%% processing

diff ( y , x)

14
2– Integral(int).

Clear

Clc

%% input

syms x

y=x

%% processing

int ( y , x) or int (y , x , a , b)

Example: y = 9*x + x^3 + 10 (int)

Clear

Clc

%% input

syms x

y = 9*x + x^3 + 10 ;

%% processing

Int ( y , x )

15
(Part 3)

Q/ kerosene (SG=0.85) flows through the Venturi meter shown in figure


below with flow rates between 0.005 and 0.05. determine the range in
pressure difference, needed to measure these flow rates .

If the flow is assumed to be steady, inviscid, and incompressible, the


relationship between flow rate and pressure can be rearranged to give

16

You might also like