Matlab: Name:Mo Stafa Qasim Tahr
Matlab: Name:Mo Stafa Qasim Tahr
Mechanical engineering
Second Stag e.
NAME:MO
STAFA
QASIM
TAHR
Mathematical operations .
1-First The contents of all parentheses are evaluated first, starting from the innermost
parentheses and working outward.
2-Second All exponentials are evaluated, working from left to right
3-Third All multiplications and divisions are evaluated, working from left to right
4-Fourth All additions and subtractions are evaluated, starting from left to right
Example:
>> 1/(2+3^2)+4/5*6/7
Ans= 0.7766.
(1-14)
CHAPTER(2):
Elementry Function:
Cos(x) cosine Abs(x) Absolute value
Sin(x) sine Sign(x) signum function
tan(x) tangent Max(x) maximum value
acos(x) Arc cosine Min(x) Minimum value
asine(x) Arc sine Ceil(x) Round toward+ ∞
atan(x) Arc tangent Floor(x) Round toward -∞
exp(x) Exponential Roumd(x) round to nearest integer
sqrt(x) square root Rem(x) Remainder after division
log(x) natural logarithm Angle(x) phase angle
log10(x) common logarithm Conj(x) complex conjutagte
Example:
(1).
y=e−asin(x) + 10√ y
>> x=0:pi/100:2*pi;
>> y=sin(x);
----------------------------------------------------------------------------------------------------
Matrix generation:
Matrices are fundamental to MATLAB. Therefore, we need to become familiar
with matrix generation and manipulation. Matrices can be generated in several
ways.
Elementary matrices:
(4-14)
CHAPTER(3)
Array operators:
.* Element-by-element multiplications
./ Element-by-element division
.^ Element-by-element exponentiation
>> C= A.*B
>>C=A.*B
10 40 90
C= 160 250 360
490 640 810
(5-14)
Solving Linear Equations:
With matrix notation, a system of simultaneous linear equations is written Ax = b
where there are as many equations as unknown. A is a given square matrix of order
n, b is a given column vector of n components, and x is an unknown column vector
of n components. In linear algebra we learn that the solution to Ax = b can be
written as x = A−1 b, where A−1 is the inverse of A.
Matrix Inverse:
>>inv(A)
−1.7778 0.8889 −0.1111
Ans= 1.5556 −0.7778 0.2222
−0.1111 0.2222 −0.1111
det(A)ans=27
Matrix function:
(6-14)
CHAPTER(4)
Introduction:
So far in these lab sessions, all the commands were executed in the Command Window. The
problem is that the commands entered in the Command Window cannot be saved and executed
again for several times. Therefore, a different way of executing repeatedly commands with
MATLAB is:
1. to create a file with a list of commands,
2. save the file, and
3. run the file.
If needed, corrections or changes can be made to the commands in the file. The files that are used
for this purpose are called script files or scripts for short. This section covers the following
topics:
M-File Scripts
M-File Function
M-File Scripts
A script file is an external file that contains a sequence of MATLAB statements. Script files have
a filename extension .m and are often called M-files. M-files can be scripts that simply execute a
series of MATLAB statements, or they can be functions that can accept arguments and can
produce one or more outputs.
A=[1 2 3; 3 3 4; 2 3 3];
B=[1; 1; 2];
x-A\b
x= -0.5000
1.5000
-0.5000 (7-14)
Example2:
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 3])
(8-14)
CHAPETER(5)
CONTROL FLOW:
MATLAB has four control flow structures: the if statement, the for loop, the while loop, and the
switch statement.
THE “if…end’’structure
If…end
If…else…end
If…elseif…else…end
== Equal to
∼= Not Equal to
| OR operator
∼ NOT operator
(9-14)
Operator precedence
Precedence Operator
1 Parentheses ()
2 Transpose (. 0 ), power (.ˆ), matrix power (ˆ)
3 Unary plus (+), unary minus (−), logical negation ( ∼)
4 Multiplication (. ∗), right division (. /), left division (.\), matrix
multiplication (∗),matrix right division (/), matrix left division (\)
5 Addition (+), subtraction (−)
6 Colon operator (:)
7 Less than (), greater than or equal to (≥), equal to (==), not equal to ( ∼=)
8 Element-wise AND, (&)
9 Element-wise OR, (|)
F(x)=main function
X=Integral for x.
Int(F(x),x,a,b)
a=intial value
b=end value
Example1:- Example2:-
6
∫ y= x3 + 6x + 1 ∫ y = x2 + 6x + 7
2
y//=? y=?
Clear clear
Clc clc
%%input %%input
Symsx symsx
%%processing int( y, x, 2, 6 )
Int( f(x),x )
(11-14)
(Differention in Matlab)
F(x)=main function
Example1:- Example2:-
r/=? y// =?
Clear Clear
Clc Clc
%%input %%input
Symsx Symsx
%%processing %%processing
diff ( y, x ) diff ( y, x , 2 )
(12-14)
(Part3)
Solution:
Function [ V ] = Velocity ( q , 0 )
V= q/o (1)
End
dp= Bernoli ( V1 , V2 )
V =0.6367
V1 = 0.6367
V = 1.7687
V2 = 1.7687
dp = 1.1572
dp = 1.1572
V = 6.3670
V1 = 6.3670
V = 17 . 6866
V2 = 176866
dp = 115.7177
dp = 115.7177
( 14 – 14 )