Matlab
University of kirkuk Report
Collage of engineering.
Mechanical engineering
Second Stag e.
NAME:MO
STAFA
QASIM
TAHR
Under The Supervision : Dr. Ahmed Yashar
(Part1) CHAPTER(1):
Basic airthmetic operators
Operation Symbol Meaning Example
Addition + 13+2=15
Subtraction - 13-5=8
Multiplication * 6*6=36
Ordinary Divison / The result can be a 13/5=2.6
decimal number
15/3=5.0
2/9=0.222
Quotient(div) Div The Result is just the 13 DIV 5=2
integer part of the
actual answer 15 DIV 3=5
2 DIV 9=0
Hierarchy of airthmetic Operations:
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
y=exp(-a) * sin(x) + 10*sqrt(y)
Predefined constant values:
Pi The π number , π = 3.14159…
I,j The imaginary unit I,√−1
NaN not a number
Inf the infinity,∞
Example: sin(pi/4) ans=0.7071
(2-14)
Basic Plot:
Plot for the vectors x and y
>> x=0:pi/100:2*pi;
>> y=sin(x);
>> plot (x,y)
----------------------------------------------------------------------------------------------------
Typical example of multiple plots
>> 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’) (3-14)
>>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])
Attributes for plot:
Symbol , Color Symbol ,Lines Style Symbol, Marker
k black - Solid + plus sign
r red −− Dashed O circule
b blue : Dotted * Asterisk
g green -. Dash-dot . Point
c Cyan None No Line × Cross
m Magenta s Square
y Yellow d Diamond
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
Operation Matrix ARRAY
Addition + +
Subtraction - -
Multipilcation * .*
Division / ./
Left Division \ .\
Exponentiation ^ .^
Summary of matrix and arrayy operations
(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:
>>A=[1 2 3;4 5 6;7 8 0];
>>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])
Anatomy of a M-File Function
Difference between scripts and function
(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
Relational and logical operators:
Operator Description
> Greater than
< Less than
>= greater than or equal to
<= Less than or equal to
== Equal to
∼= Not Equal to
& AND operator
| 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, (|)
Sving Output to a file:
In addition to displaying output on the screen, the command fprintf can be used for writing the
output to a file. The saved data can subsequently be used by MATLAB or other softwares.
To save the results of some computation to a file in a text format requires the following steps:
1. Open a file using fopen
2. Write the output using fprintf
3. Close the file using fclose.
(10-14)
(Part2) (Intergral in Matlab)
F(x)=main function
X=Integral for x.
Or, for specific integration
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
y= x^3 + 6*x +1 y= x^2 +6*x +7
%%processing int( y, x, 2, 6 )
Int( f(x),x )
(11-14)
(Differention in Matlab)
F(x)=main function
X=Differential with respect x
IF 1st differentiation =1 , IF 2nd n=2
Example1:- Example2:-
r = x3 + 5x+6 y= 6x7 + log(x)+10
r/=? y// =?
Clear Clear
Clc Clc
%%input %%input
Symsx Symsx
y = x^3 + 3*x +5 y = 6*x^7 + log(x) + 10
%%processing %%processing
diff ( y, x ) diff ( y, x , 2 )
(12-14)
(Part3)
Solution:
Function [ V ] = Velocity ( q , 0 )
V= q/o (1)
End
Function [dp] = bernoli : (V1,V2)
dp = ((( v2^2/19.6 ) – (V1^2 / 19.6)) * (8338.5)) / 1000 (2)
end
V1 = Velocity ( 0.005 , 7.853 * 10^ - 3)
V2 = Velocity ( 0.005 , 2.827 * 10^ - 3) (3)
dp = Bernolli: ( V1 , V2 )
( 13-14)
V1 = Velocity ( 0.05 ,7.853 *10^ - 3 )
V2 = Velocity ( 0.05 , 2.827*10^ - 3 )
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 )