Professional Documents
Culture Documents
Chapter One
Fundamental Concepts
1.1 Introduction
1
3. 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 large and complex application
programs.
2
Run SIMULINK
Get Help Enter a Command
New M-file
Launch Pad
a. for integer numbers the following with their ranges and the number
of bits that required to represent them:
3
Form Range No. of Bits
Ex.1)
ans= ans=
-128 90
c. >>int8(130) d. >>uint8(-12)
ans= ans=
127 0
ans= ans=
10 457000
So we notice that if the given number is out of the range of the form
the left then we get the minimum value of the form's range when the
given number where less than the range's minimum; and we get the
4
maximum value of the form's range when the given number is greater
than the range's maximum.
Operator Description
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
~= Not equal to
5
1.6 Arithmetic Operators in MATLAB:
Symbol Operation
+ Addition
- Subtraction
* Multiplication
/ Division
^ Power
Table 1.4 The arithmetic operators forms in MATLAB
6
>> 4*x
ans =
28.0000
7
1.9 Complex Number in Matlab:
>>z=x+i*y or >>z=x+j*y
Ex.:
>>x=2; >>abs(z)
>>y=3; ans:
>>z=x+i*y 3.6056
ans:
2.0000+3.0000i
>>angle(z) >>conj(z)
ans: ans:
0.9828 2.0000-3.0000i
8
1.10 Computer Number Systems:
We can convert:
Ex.:
>>bin2dec('10') >>dec2bin(2)
ans: ans:
2 10
>>hex2dec('A') >>dec2hex(10)
ans: ans:
10 A
>>base2dec('21',8) >>dec2base(17,8)
ans: ans:
17 21
9
H.w: 1. Convert the binary number: 101011 to a number in octal system.
(b) Using input Statement: which has the following general form:
(i) >>variable or constant name=input('some massage') ;which is
used for numeric variables or constant.
(ii) >>variable or constant name=input(' some massage ','s') ;which
is used for string variables or constant.
For output as we know the Matlab directly shows the result of any
variable or constant; and we can also use the Matlab output statement:
>>disp(variable or constant name or 'massage' )
which is used either
(i) to print out the massage.
Or
(ii) the value of the variable or constant without writing it's name.
Ex.:
Use the input statement to enter:
(i) x as a numeric value 5.8;
(ii) y as the string "hello this is the Matlab programming course";
then use the disp-function for showing their results.
10
x=
5.8000
>>y=input('Enter the char of the >>disp(y)
string y:','s') hello this is the Matlab programming
Enter the char of the string y: hello course
this is the Matlab programming
course
y=
hello this is the Matlab
programming course
1. blanks(n): that puts n-spaces between the string S1 and the string
S2.
2. deblanks: which deletes the spaces between any two strings S1 and
S2( after the string S1 and after S2).
3. strrep(S1,S2,S3): which replace the string S2 in S1 by S3.
4. strcmp(S1,S2): which compare between the two strings S1 and S2.
It's result is either 1 when (S1 S2) or 0 when (S1 different from
S2).
5. strncmp(S1,S2,n): which compare the first n-character of S1 with
the first n-character of S2.
6. findstr(S1,S2): which writes the position of S2 in the string S1.
7. lower(S): which writes S in small letters.
8. upper(S): which writes S in capital letters.
Rem.: We use the Matlab function char(n0.) to convert the number no.
into a character by the ASCII.
Ex.:
11
>>char(66) >>m{1,1}='my name is ';
ans: >>m{1,2}=' AAA ';
B >>deblank(m)
>> char(65:120)
ans = ans =
ABCDEFGHIJKLMNOPQRSTUV 'my name is' ' AAA'
WXYZ[\]^_`
abcdefghijklmnopqrstuvwx
2. primes(x): This function is used to compute all the prime numbers less
than or equal to x.
3. sign(x): This function is used to write the sign of x; it's result is either 1
when x>0; or 0 when x=0; or -1 when x<0.
12
5. floor(x): This function approximates x towards -∞.
11. gcd(x,y): This function is used to find the greatest common divisor of
x and y.
12. lcm(x,y): This function is used to find the least common multiple of
x and y.
13. gamma(x): This function is used to find the result of the gamma
function of x that defined as:
∞
∫ e−t t x−1 dt
0
14. beta(x,y): This function is used to find the beta function of x and y
which is defined by:
1
∫ t x−1 (1−t)y−1 dt
0
15. erf(x): This function is used to find the result of error function of x
which is defined by:
x
2 2
∫ e−t dt
√π 0
13
16. erfc(x): This function is used to find the result of cumulative error
function of x which is defined by:
∞
2 2
−t
∫ e dt =1−erf (x)
√π x
Ex.:
>>factorial(5) >>primes(12)
ans= ans=
120 1 2 3 5 7 11
>>factor(40) >>sign(-90)
ans= ans=
2 2 2 5 -1
>>fix(-1.9) >>floor(-1.1)
ans= ans=
-1 -2
>>ceil(-1.9) >>round(-3.6)
ans= ans=
-1 -4
ans = ans =
3 9
>>gcd(24,32) >>lcm(24,32)
ans= ans=
8 96
14
ans =
24 ans =
7.9365e-004
ans = ans =
0.5205 4.1370e-037
15
Chapter Two
2.1 if Statement:
16
Ex.: Write the Matlab commands for finding the result of
Sol.:
(a) Since the square root of (1-x) is defined only when x ≤ 1 this implies
that our condition is ( x ≤ 1) and y can be evaluated easily in this
case by the following Matlab commands:
Either: Or:
x=input('enter the value of x') x=input('enter the value of x')
if (x<=1) if (x<=1)
y=sqrt(1-x) y=sqrt(1-x)
end else
disp('undefined ')
end
(b) Since the square root of (b2-4ac) is defined only when b2-4ac≥0;
So we shall denote it by x= b2-4ac and the condition will be x≥0;
and in fact we have three cases: x>0, x<0, and x=0) the Matlab
commands may be given as follows:
17
y=0
elseif (x>0)
disp('The second case')
D=sqrt(x)
else
disp('The second case')
disp('Complex case ')
end
switch(condition or expression)
statement(s)
statement(s)
statement(s)
otherwise
statement(s)
end
18
Ex.: Write the Matlab commands to enter a student code number then
print his(her) name as the absence list.
Sol.:
switch (code)
case 1
disp('Ahmed')
case 2
disp('Ali')
case 3
disp('Bilal')
case 4
disp('Zaid')
case 5
disp('Yousif')
otherwise
disp('unknown code')
end
Ex.: Write the Matlab commands to input the first letter of your friends
name then print his(her)complete name?
19
disp('ahmed')
case 'b'
disp('belal')
otherwise disp('unkown')
end
H.W: Try to write the Matlab commands for:
(1) Finding the solution(roots) of : ax2+bx+c=0, where a, b, and c are
constants.
(2) Entering the day name then convert it into the day number (for
example 1 for Sunday , 2 for Monday,..).
(3) Input a number x then print positive when x is positive, and print
negative when x is negative.
(4) Input a number x then test if it is even or odd.
(5) Input three number x, y, and z then find their maximum and
minimum.
20
Chapter Three
Loops in Matlab
statement(s)
end
Note that you must put : between any two of the three above steps.
Ex. Use the for statement to enter 10 numbers find their summation? then
their product?
Sol.:
sum=0
21
product=1
product=product*number
end
Ex.: Write the Matlab commands to read 20 number then finds their
maximum and minimum.
Sol.:
1. input the first number and we set it as both maxi, and mini.
2. put the loop statement form 2 to 20.
22
3. For each one of these steps we shall input another number and
test whether the maxi is less than the new number; and mini is
greater than the new number; to have new values of the
maximum and minimum.
maxi=number
mini=number
for counter=2:20
if (maxi<number)
maxi=number
end %if-statement
if (mini>number)
mini=number
end %if-statement
end %for-statement
H.W: Use the for-statement to write the Matlab commands for each of
the following:
1. Enter 15 number then print positive for the positive ones and negative
for the negative one.
23
1 2 n
3. Evaluate z from the relation: z= 1! + 2! + …+ n! ; where n is any positive
integer number.
3 2 n +1
x x x
4. Evaluate w from the relation: w= + + …+ ; where x is any
1! 3 ! (2 n+1)!
3.2 while-Statement:
while(Condition or Expression)
Statement(s)
end
Sol.:
while(Number>0)
end
24
Sol.:
25
Chapter Four
Matrices in Matlab
1 −1
[ ] 3 5
A= 2 0 ; B= i 2 ?
7 9
[ ]
Sol.:
A= B=
1 -1 3 5
2 0 i 2
7 9
(ii) When the matrix is not given, we use the following loops and
the input-statement:
26
number of rows=input('enter the number of rows =')
end %j
end %i
number of rows=3;
number of columns=2;
27
1. sum(A): This function computes the summation of elements in
every column in A.
28
1 2 3 Where the min. element in the second col. =2
ans = ans=
1 0
0 0 0
0 0
0 0 0
ans = ans=
2 1
1 1 1
29
1 1 1 1 1
99 99 99
[
A= 99 99 99 ?
99 99 99 ]
Sol.:
>>A=99*ones(3)
ans=
99 99 99
99 99 99
99 99 99
Ex.:
[ 1 2]
We have the matrix 3 4 the
>> diag([1 2;3 4])
elements of the main diagonal of
ans = it is 1 and 4.
30
ans =
[ 0 −92 ]
sorting will be 1
0 -9
1 2
matrix B of size n n.
ans = ans =
3 -3
11. inv(B): This function computes the inverse of the square matrix B
−1
of size n n if det(B)≠0 (i.e Finding ( B ) if it is possible).
Ex.: >> inv([1 2;3 7]) Ex.: >> inv([1 2 3;4 5 6])
-3.0000 1.0000
31
Ex.: >> eig([1 2;3 4]) Ex.: >> eig([1 2 3;4 5 6;5 6 9])
ans = ans =
-0.3723 14.9732
5.3723 -0.6198
0.6465
ans = ans =
9.0185 15.2753
ans = 1
1 4 21
2 5 31
3 6
32
Ex.>>A=[3 5 7]; Ex. >>A=[1 4;2 5;3 6];
ans: ans:
7 3 6
5 2 5
3 1 4
4 1
5 2
6 3
Rem.: When the value of dim is 1, the array is flipped row-wise down.
When dim is 2, the array is flipped column-wise left to right.
3 6
2 5
1 4
33
Rem. rot90(A,k) rotates matrix A counterclockwise by k*90 degrees,
where k is an integer.
a n1 x 1+ an 2 x 2 +a n3 x 3+ …+a nn x n=b n¿
¿
in Matlab by using:
>>B=[b 1 ;b2 ;… ; bn ¿;
>>solution=inv(A)*B
Rem.:
sol.:
for i=1:n
for j=1:m
end %j
end %i
sum(A(7,:))
end % if n>=7
mean(A(:,4))
end % if m>=4
Ex. Write the Matlab instructions to interchange the second row by the
fifth row in a matrix An×m.
sol.:
for i=1:n
for j=1:m
35
A(i,j)=input('enter the matrix elements')
end %j
end %i
T=A(2,:);
A(2,:)=A(5,:);
A(5,:)=T;
end % if n>=5
Ex. Write the Matlab instructions to delete the fifth column in a matrix
An×m.
sol.:
for i=1:n
for j=1:m
end %j
end %i
A(:,5)=[]
end % if m>=5
36
Chapter Five
Applications
>>f=function rule
Sol.:
>>syms x y z
>>f=x*sin(z*y)
f=
x*sin(z*y)
37
5.2 Derivatives:
>>syms x y z >>diff(f , y)
>>f=x*sin(z*y) ans=
x*z*cos(z*y)
f=
>> diff(diff(f,y),x)
x*sin(z*y)
>>diff(f , x) ans =
ans= z*cos(y*z)
sin(z*y)
5.3 Integration:
for finding the definite integral of f w.r.t variable name from a to b. (i.e
this function is used to compute the area under the curve of f from a to b).
Also we can use the instruction:
38
>>int(f )
∫ cos3t dt ∫∫ x y dy dx
Ex.: Find ∫ sin2 x dx ; 0 ; 1 3 by Matlab?
Sol.:
>> syms x y t
>>f1=sin(2*x)
>>int(f2 , t, 0 , pi)
>>f3=x*y
>>int(int(f3,y,3,7),x,1,2)
a)
>>x=starting value:step size: Final value; .for 2-dim plotting
>>y=function at x;
>>plot(x,y)
And
b)
>>t= starting value:step size: Final value;
.for 3-dim plotting
39
>>x= function at t;
>>y=function at t;
>>plot3(t, x, y)
Or >>x=-2*pi:0.05*pi:2*pi;
>>x=-2*pi:0.05*pi:2*pi; >>y=sin(2*x);
>>plot(x,sin(2*x)) >>plot(x,y)
(2)
Or >>t=-4*pi:0.005*pi:4*pi;
>>t=-4*pi:0.005*pi:4*pi; >>y=cos(t*pi/2);
>>plot(t, cos(t*pi/2)) >>plot(t,y)
(3)
Or >>x=-2*pi:0.05*pi:2*pi;
>>x=-2*pi:0.05*pi:2*pi; >>y=0:0.01:2;
>>y=0:0.01:2; >>plot3(x,y,exp(x,y))
>>z=exp(x,y)
>>plot3(x,y,z)
H.w:
40
1.
2.
3.
>>syms t s
>>f=function of t;
>>laplace(f)
>>syms t s
>>F=function of s;
>>ilaplace(F)
1 1
Ex.: Compute:
L
[]; [ ]
t2
L−1 2
s −1
Sol.:
>>syms s t
>>f=1/(t^2);
>>laplace(f)
>>F=1/(s^2-1)
41
>>ilaplace(F)
42