LAB Total
LAB Total
Introduction: MATLAB is an abbreviation for MATrix LABoratory. It is a matrix-based system for scientific calculations.
You can solve numerical problems without necessarily having to write a long program.
ALGORITHMS: Before writing any computer program, it is useful to first outline the steps that will be necessary. An algorithm is the
sequence of steps needed to solve a problem.
MAT LAB SCRIPTS: Once a problem has been analyzed, and the algorithm for its solution has been written and refined, the algorithm
then is translated into a particular programming language. A computer program is a sequence of instructions, in a given language
that accomplishes a task. To execute, or run, a program is to have the computer actually follow these instructions.
Figure 1 y=sin(x)
Example 1: returns to :
A = [1 2 3;4 5 6;7 8 9];
A(3,2); % displays 8
A(2,1) + A(2,2) + A(2,3) % the sum of row 2
A(:,2); % read as all rows, column 2 and displays a column vector
x = [-pi:0.1:pi]' ;
y = sin(x);
[x y]
Remark: The 𝑙𝑒𝑛𝑔𝑡ℎ(𝐴) Computes either the number of elements of 𝐴 if 𝐴 is a vector or the largest value of 𝑚 or n
if A is an 𝑚 × 𝑛 matrix.
PROGRAMMING USING MATLAB: The MATLAB interactive mode is very useful for simple problems, but more complex
problems require a script le. Such a le can be called a computer program, and writing such a le is called programming.
An algorithm is an ordered sequence of precisely defined instructions that performs some task in a finite amount of time.
There are three categories of algorithmic operations:
(a) Sequential operations, (b) Conditional operations and (c) Iterative operations (loops)
Pseudocode: Use of natural language, such as English, to describe algorithms often results in a description that is too
verbose and is subject to misinterpretation.
Example 1: (Sequential Operations) Compute the perimeter 𝑝 and the area 𝐴 of a triangle whose sides are:
𝑎, 𝑏 and 𝑐. The formulas are: 𝑃 = 𝑎 + 𝑏 + 𝑐, 𝑠 = 𝑝/2, 𝐴 = √𝑠(𝑠 − 𝑎)(𝑠 − 𝑏)(𝑠 − 𝑐)
Example 2: (Conditional Operations) Given the (𝑥, 𝑦) coordinates of a point, compute its polar coordinates (𝑟, 𝜃),
where 𝑟 = √𝑥 2 + 𝑦 2 , 𝜃 = tan−1(𝑦/𝑥 )
Example 3: (Iterative Operations) Determine how many terms are required for the sum ∑ 𝐒(𝐤) to exceed 20 000,
where 𝐒(𝐤) = 𝟏𝟎𝐤 𝟐 − 𝟒𝐤 + 𝟐 . What is the sum for this many terms? Because we do not know how many times we
must evaluate the expression 𝐒(𝐤) = 𝟏𝟎𝐤 𝟐 − 𝟒𝐤 + 𝟐, we use a while loop, which is covered in later.
Basic plotting: MATLAB has an excellent set of graphic tools. Plotting a given data set or the results of computation is possible
with very few commands. You are highly encouraged to plot mathematical functions and results of analysis as often as possible.
Trying to understand mathematical equations with graphics is an enjoyable and very efficient way of learning mathematics. Being
able to plot mathematical functions and data freely is the most important step, and this section is written to assist you to do just
that.
Polar Plots:
Symbolic functions: accept array inputs. Calculate f(𝑥, 𝑦) for multiple values of 𝑥 and . You can differentiate symbolic
functions, integrate or simplify them, substitute their arguments with values, and perform other mathematical operations. For
example, you can find the derivative of f(𝑥, 𝑦) with respect to 𝑥.
Examples:
dy
1 y 2 (t )
dt
d 2 y dy
5 1 y 2 (t )
dt 2 dx
dy
1 y 2 t and y 0 1
dt
First Laboratory Assignment in MATLAB Environments Level: L3 Avionics
“Familiarization with MATLAB” Submitted to the students of
IASS Blida-1
Ezplot (MATLAB Function Reference) ezplot(f) plots a graph of f(x), where f is a symbolic expression representing a
mathematical expression involving a single symbolic variable, say x. The domain on the x-axis is usually [-2*pi, 2*pi] .
First Laboratory Assignment in MATLAB Environments Level: L3 Avionics
“Familiarization with MATLAB” Submitted to the students of
IASS Blida-1
Example 2:
Blida University (Algeria) 3rd Level: L3 LMD (Avionics)
TP N0 02
Conditional Statements
Example: 01 (Given a real input 𝑡) write MATLAB code and plot of the function
𝑡 if 0 ≤ 𝑡 ≤ 1
f(𝑡) = {2 − 𝑡 if 1 ≤ 𝑡 ≤ 2
clear all, clc, t=-6:0.1:6; k=length(t);
0 elsewhere
for i=1:k
if t(i)>=0 & t(i)<=1
x(i)=t(i);
elseif t(i)>1 & t(i)<=2
x(i)=-t(i)+2;
else
x(i)=0;
end
end
plot(t,x,'r','linewidth',10)
grid('minor')
%------------------------------------------------------------------------------------%
Example: 02 (Given a real input 𝑥) write MATLAB code and plot of the function
3 if 𝑥 ≤ −4
−4𝑥 − 13 if − 4 < 𝑥 ≤ −3
f(𝑥) = { 2
𝑥 + 6𝑥 + 8 if −3<𝑥 ≤0
8 if 0<𝑥
Example: 03 (Given a real input 𝑥) write MATLAB code and plot of the function
1 − 𝑥 22
𝑒 𝛼 if − 2 ≤ 𝑥 ≤ 2
f(𝑡) = { 𝛼 2 and 𝛼=1
𝑥 if − 4 ≤ 𝑡 ≤ −2 or 2 ≤ 𝑡 ≤ 4
0 elsewhere
Example: 04 (Given a real input 𝑥) write MATLAB code and plot of the function
Exercise:01 (Given an input integer 𝑛) write MATLAB program to calculate the following
1 if 𝑛 ≥ 0 𝑛 + 1 if 𝑛 < 2
𝑘1 = { 𝑘2 = {
0 if 𝑛 < 0 2𝑛 if 𝑛 ≥ 2
Exercise:02 (Given a real input 𝑥) write a MATLAB program to calculate the following
2 sin(𝑥) if 𝑥 > 3
𝑦1 = {𝑥 − 1 if 𝑥 ≠ 1 𝑦2 = {
5𝑥 if 𝑥 = 1 tan(𝑥) if 𝑥 ≤ 3
Exercise:03 (Given an input integer 𝑚) write a MATLAB code to calculate the following
3𝑚 if 𝑚 "positive even"
3𝑚 if 𝑚 "positive even"
𝑝1 = { 𝑝2 = {2𝑚 + 1 if 𝑚 "positive odd"
2𝑚 + 1 if 𝑚 "positive odd"
0 if 𝑚 negative
Exercise:04 (Given real 𝑎 & 𝑏) write a MATLAB code to calculate the max of 𝑎 & 𝑏
Exercise:05 (Given a real input 𝑥) write a MATLAB code to calculate the following
sin(𝑥) if 𝑥 > 5
ln(𝑥 + 1) if 𝑥 > 0.5
f(𝑥) = { −𝑥 g(𝑥) = {cos(𝑥) if 𝑥 < 5
𝑒 + 2𝑥 if 𝑥 ≤ 0.5
𝑥 if 𝑥 = 5
Blida University (Algeria) 3rd Level: L3 LMD (Avionics)
Exercise:06 (Given real 𝑎 & 𝑏) write a MATLAB code to calculate the following
𝑎×𝑏 if 𝑎 < 𝑏
𝑐 = {𝑎+𝑏 if 𝑎 > 𝑏
1 if 𝑎 = 𝑏
Exercise:07 (Given a real input 𝑥) write a MATLAB code to calculate the following
Exercise:08 (Given a real input 𝑥) write a MATLAB code to calculate the (𝑦, 𝑧, 𝑡)
√𝑦 − 1 if 𝑦 > 3 𝑦+𝑧 if 𝑦 ≤ 𝑧
y = 2𝑥 + 1 z = { 𝑡={
5|cos(𝑦)| if 𝑦 ≤ 3 𝑦−𝑧 if 𝑦 > 𝑧
Exercise:09 (Given a real input 𝑥) write a MATLAB code to calculate the (𝑦, 𝑧)
𝑦/2 if 𝑥 > 0
|sin(𝑥)| if 𝑥 < 2
y={ 𝑧 = {2𝑦 if 𝑥 < 0
|cos(𝑥)| if 𝑥 ≥ 2
𝑦2 if 𝑥 = 0
Exercise:10 (Given a real input 𝑥) write a MATLAB code to calculate the following
Exercise:10 (Given a real input 𝑥) write a MATLAB code to calculate the following
ln(1 + |𝑥 3 − 1|)
if 0 < 𝑥 ≤ 2
1+𝑥
𝑤= 3
+ 𝑥4 if 2 < 𝑥 ≤ 10
𝑥
{ cos 2 (𝑥) + 𝑒 −2𝑥 if 𝑥 ≤ 0 or 𝑥 > 10
Exercise:11 (Given a real input 𝑥) write a MATLAB code to calculate the following
𝑥 if 𝑦 ≤ 𝑥 ≤ 𝑧 or 𝑧 < 𝑥 ≤ 𝑦
2 𝑦 2 − √3𝑦 if 𝑦 ≥ 4
y = { 𝑥 2− √𝑥 if 𝑥 > 0 z={ w = {𝑦 if 𝑥 < 𝑦 ≤ 𝑧 or 𝑧 < 𝑦 ≤ 𝑥
2𝑥 − 𝑥 + 1 if 𝑥 ≤ 0 tan(𝑦) if 𝑥 < 4 𝑧 if 𝑥 < 𝑧 ≤ 𝑦 or 𝑦 < 𝑧 ≤ 𝑥
%------------------------------------------------------------------------------------%
%-------EX01-------- %-----------------
clear all, clc,
n=input('Ente the value of real n: '); n=input('Ente the value of real n: ');
if n>=0, k1=1; else, k1=0; end if n<2, k2=n+1; else, k2=2*n; end
k1 k2
%------------------------------------------------------------------------------------------------------------------%
%-------EX02-------- %-----------------
clear all, clc, clear all, clc,
x=[-2:0.01:2]; nx=length(x); x=[-2:0.01:2]; nx=length(x);
for i=1:nx for i=1:nx
if x(i)~=1, if x(i)>3,
y1(i)=(x(i))^2-1; y2(i)=sin(x(i));
else, else,
y1(i)=5*x(i); y2(i)=tan(x(i));
end end
end end
plot(x,y1), grid on plot(x,y2), grid on
%------------------------------------------------------------------------------------------------------------------%
%-------EX03-------- %-----------------
clear all, clc, clear all, clc,
m=input('Ente the value of real m=: '); m=input('Ente the value of real m=: ');
if and(mod(m,2)==0,m>0),
if mod(m,2)==0, p2=3*m;
p1=3*m; elseif and(mod(m,2)~=0,m>0),
else, p2=2*m+1;
p1=2*m+1; else
end p2=0;
p1 end
p2
%------------------------------------------------------------------------------------------------------------------%
%-------EX04-------- %-----------------
clear all, clc, clear all, clc,
a=input('Ente the value of real a=: '); a=input('Ente the value of real a=: ');
b=input('Ente the value of real b=: '); b=input('Ente the value of real b=: ');
if a>b, max=a; else, max=b; end if a>b, min=b; else, min =a; end
max min
%------------------------------------------------------------------------------------------------------------------%
%-------EX05-------- %-----------------
clear all, clc, x=[-2:0.01:2]; nx=length(x); clear all, clc, x=[-1:0.01:2]; nx=length(x);
for i=1:nx for i=1:nx
if x(i)>0.5, if and(x(i)>0.5, x(i)<1.5),
y1(i)=log(x(i)+1); y2(i)=log(x(i)+1);
else, else,
y1(i)=exp(-x(i))+2*x(i); y2(i)=exp(-x(i))+2*x(i);
end end
end end
plot(x,y1), grid on plot(x,y2), grid on
%-------EX08--------
clear all, clc, x=[-2:0.01:2]; nx=length(x);
y=2*x+1;
for i=1:nx
for i=1:nx
if y(i)<=z(i),
if y(i)>3,
t(i)=y(i)+z(i);
z(i)=sqrt(y(i)-1);
else,
else,
t(i)=y(i)-z(i);
z(i)=5*abs(cos(y(i)));
end
end
end
end
plot(x,t), grid on, hold on
Blida University (Algeria) 3rd Level: L3 LMD (Avionics)
TP N0 03
Iterations and For Statement
Example: 01 (Given a large integer 𝑛) write MATLAB code to compute the next sequences
Exercise:01 (Given an integer 𝑛) write MATLAB code to compute the next sequences
𝑛 1 𝑛 𝑛 𝑛
S[𝑛] = ∑ ( ), P[𝑛] = ∏ 𝑘, S[𝑛] = ∑ (3𝑘 − 2) , S[𝑛] = ∑ (−1)𝑘+1 (2𝑘 − 1)
𝑘=1 𝑘 𝑘=1 𝑘=1 𝑘=1
Exercise:02 (Given an integer 𝑛) write MATLAB code to compute the next sequences
1 1 1 1 𝑛 1 1 1 1 𝑛
S[𝑛] = 1 − + − + ⋯ = ∑ …, S[𝑛] = 5 − + − + +⋯ =4+∑ …
2 3 4 𝑛 𝑘=1 4 9 16 25 𝑘=1
Exercise:03 write a MATLAB code to calculate the first 100 terms of 𝜋 by using
𝜋 1 1 1 1 1
=1− + − + − +⋯
4 3 5 7 9 11
Exercise:04 write a MATLAB code to calculate the next sum
𝑛 (−1)𝑘+1
S[𝑛] = ∑
𝑘=0 3𝑘 + 1
Exercise:05 (Given a real input 𝑥) write a MATLAB code to calculate the following
𝑛 𝑘(−1)𝑘 𝑛 (−1)𝑘+1 ((2𝑘 − 1)𝑥 + 1)
f(𝑥, 𝑛) = 1 + ∑ 𝑘 , g(𝑥, 𝑛) = 1 + ∑
𝑘=1 𝑥 + 1 𝑘=1 𝑥 2𝑘
Let 𝑥 = 0.5 Let 𝑥 = 0.5
Exercise:06 (Given an integers 𝑛, 𝑚) write a MATLAB code to calculate the sequence
(−1)𝑘+1
𝑛 𝑚 1
S[𝑛] = 2 + ∑ +∑ 2
𝑘=1 5𝑘 + 1 𝑘=1 𝑘 + 1
Blida University (Algeria) 3rd Level: L3 LMD (Avionics)
x(𝑡) = 4.9𝑡 2 + 2𝑡
Exercise:08 The equation of motion of a mass in a free fall is given by {
𝑉(𝑡) = 9.8𝑡 + 2
with 𝑎 ≤ 𝑡 ≤ 𝑏, x(𝑡) is the position and 𝑉(𝑡) is the speed. Divide the time interval by 𝑛 = 50,
then write a MATLAB code to calculate the (𝑡, 𝑥, 𝑥), plot the results.
Exercise:09 In the standard atmosphere the air's pressure 𝑃 and the temperature 𝑇 are
functions varying in terms of the altitude 𝑧. In the troposphere layer 0 ≤ 𝑧 ≤ 11000 and
5.257
𝑃 = 101.325 × (1 − (2.25577. 10−5 × 𝑧)) & 𝑇 = 288.15 − (0.0065 × 𝑧)
(z: In meters, P: in Kilopascal, T: in kelvin). Divide the altitude interval by 𝑛 = 50, and write
a MATLAB code to calculate the (𝑧, 𝑃, 𝑇), plot the results.
Exercise:10 write a MATLAB code to calculate the table of all multiplications (𝑖, 𝑗 = 1: 10)
Exercise:14 write a MATLAB code to calculate the PGCD & PPCM of two numbers 𝑎 & 𝑏
%-----------------------------------Homework-----------------------------------%
Exercise:15 Prove the following statements, and write a MATLAB code to calculate them
(𝑛 = large number e. g 𝑛 = 100)
𝑛 1 𝑛 1 𝑛 1 𝜋2 𝑛 (−1)𝑘+1 𝑛 𝑥𝑘
2=∑ ( ) 𝑒=∑ ( ) Ln[2] = ∑ =∑ 𝑒𝑥 = ∑
𝑘=1 2𝑘 𝑘=1 𝑘! 𝑘=1 𝑘 12 𝑘=1 𝑘2 𝑘=1 𝑘!
𝜋 𝑛 (−1)𝑘 𝜋 2 𝑛 1 𝜋2 𝑛 1 𝜋2 𝑛 1 𝑛 𝑘
=∑ =∑ =∑ =∑ 1=∑
𝑘=1 (2𝑘) 𝑘=0 (2𝑘 + 1) 𝑘=1 (𝑘 + 1)!
4 2 2 2
𝑘=0 2𝑘 + 1 24 8 6 𝑘=1 𝑘
Blida University (Algeria) 3rd Level: L3 LMD (Avionics)
𝜋 𝑛 (2𝑘)2 𝜋 𝑛 (−1)𝑘 1 1 𝑛 2𝑘 + 1
=∏ =∑ ( 2𝑘+1 + 2𝑘+1 ) 𝑒=∑ ( )
2 𝑘=1 (2𝑘 − 1)(2𝑘 + 1) 4 𝑘=0 (2𝑘 + 1) 2 3 𝑘=1 (2𝑘)!
𝑛 𝑘2 𝑛 𝑘 𝑛 9 𝑛
𝑒−1=∑ ( ) 𝑒 𝑖𝜋 = − ∑ 1=∑ 1=∑ 2−𝑘
𝑘=1 (𝑘 + 1)! 𝑘=1 (𝑘 + 1)! 𝑘=1 10
𝑘
𝑘=1
1 𝑛 1 𝑛 1 𝑛 1 𝑛 1 𝑛 1 𝑛
= ∑ 9−𝑘 = ∑ 3−𝑘 = ∑ 5−𝑘 = ∑ 7−𝑘 = ∑ 4−𝑘 = ∑ 6−𝑘
8 𝑘=1 2 𝑘=1 4 𝑘=1 6 𝑘=1 3 𝑘=1 5 𝑘=1
1 𝑛 1 𝑛 1 1 𝑛 1
= ∑ 8−𝑘 =∑ =∑
7 𝑘=1 2 𝑘=0 (2𝑘 + 1)(2𝑘 + 3) 3 𝑘=0 (2𝑘 + 1)(2𝑘 + 5)
Blida University (Algeria) 3rd Level: L3 LMD (Avionics)
TP N0 04
Iterations (While and For loops)
Example: 02 The problem is to find the smallest positive integer 𝑘 so that 2−𝑘 = 𝜀 in
floating point arithmetic (𝜀 = 0.001).
Exercise:02 write a MATLAB code to calculate the sequence S[𝑛] < 24 and find such 𝑛
𝑛 𝜋 2𝑛 𝜋
S[𝑛] = 1 + ∑ sin ( ) + ∑ cos ( )
𝑘=1 𝑘 𝑘=1 2𝑘 − 1
𝑥 𝑥2 𝑥𝑛 𝑛 𝑥𝑘
𝑃 = (1 + ) (1 + ) … (1 + ) = ∏ (1 + ) with −5≤𝑥 ≤5
1 2 𝑛 𝑘=1 𝑘
Blida University (Algeria) 3rd Level: L3 LMD (Avionics)
2 𝑛 1
𝑇(𝑥, 𝑦) = 1 + ∑ sin(𝑘𝜋𝑥) 𝑒 −(𝑘𝜋𝑦)
𝜋 𝑘=1 𝑘
Exercise:05 Given a function defined by f(𝑥) = cos(𝑥) with 𝑎 ≤ 𝑥 ≤ 𝑏. Divide the interval
by a number 𝑛, the step ℎ is given by 𝑛ℎ = (𝑏 − 𝑎) write a MATLAB code to calculate the
sum S < 10 given that 𝑎 = 0 and 𝑛 = 100
Repeat for f(𝑥) = 1/𝑥 calculate the sum S < 10 given that 𝑎 = 1 and 𝑛 = 10
Exercise:07 (Given 𝑥 & 𝑦) write a MATLAB code to calculate 𝑆 with 𝑛 = 3 and plot the res
cos 𝑘 (𝑥 + 𝑦)
𝑛
𝑆 = 1+∑ ( )
𝑖=1 𝑘
Exercise:06 (Given 𝑥 & 𝑛) write a MATLAB code to calculate 𝑆(𝑥) ≤ 5.879608 with 𝑛 = 10
𝑛 𝑥
𝑆(𝑥) = 3 + 2 (∑ f ( )) f(𝑥) = sin(𝑥)
𝑘=1 𝑘
Exercise:07 The equation of motion for a guided projectile (its vertical displacement is
𝑧(𝑡)) is given by 𝑧(𝑡) = −0.47𝑡 3 + 6.48𝑡 2 + 18.78𝑡 + 10 with 0 ≤ 𝑡 ≤ 16𝑠. Divide the time
interval by a number 𝑛 = 100, write a MATLAB code to calculate the time at which the
projectile reaches the max altitude 𝑧𝑚𝑎𝑥 and find 𝑧𝑚𝑎𝑥 .
%-----------------------------------Homework-----------------------------------%
%------------------------------------------------------------------------------------------------------------------%
%-------EX05-------- %-----------------
clear all,clc, clear all,clc, a=1; b=2; n=10; h=(b-a)/n;
a=input('Ente the value of a=: '); y=@(x)(exp(x)); y1=y(a); y2=y(b);
b=input('Ente the value of b=: ');
n=input('Ente an even integer n=: '); s=0;
h=(b-a)/n; y=inline('cos(x)','x'); for i=0:((n/2)-1)
s=0; y1=y(a); y2=y(b); d=(2*i)+1; g=a+d*h; s=s+4*(y(g));
for i=0:((n/2)-1) end
d=(2*i)+1; g=a+d*h; s=s+4*(y(g));
end k=0;
k=0; for i=1:((n/2)-1)
for i=1:((n/2)-1) u=2*i; x=a+u*h; k=k+2*(y(x));
u=2*i; x=a+u*h; k=k+2*(y(x)); end
end
S=(h/3)*(y1+y2+s+k) S=(h/3)*(y1+y2+s+k)
%------------------
%-------EX06--------
clear all,clc, S=[]; n=10; x=[-2:0.1:2]; nx=length(x);
for k=1:nx
S1=0;
for i=1:n
for j=1:n
if i==j, A=0; elseif i>j, A=i; else, A=j; end
S1=S1+A*sin((pi*x(k))/i)*cos((pi*x(k))/j);
end
end
S=[S,S1];
end
indx = find(abs(x-0.1)<=eps)
S(indx)
x1=find(min(x)<=x<=max(x));
plot(x1,S), grid on
%-------EX07--------
clear all, clc, n=3; S=[]; cond=1; x=[-1:0.1:2]; nx=length(x); y=[-1:0.1:2]; ny=length(y);
for i=1:nx
X=[];
for j=1:ny
U=1;
for k=1:n
U=U+((cos(x(i)+y(j)))^k)/k;
end
X=[X, U];
end
S=[S; X]; cond=S(i,j);
end
indx = find(abs(x-0.4)<=eps), indy = find(abs(y-0.6)<=eps),
S(indy,indx)
S(indx,indy)
x1=find(min(x)<=x<=max(x));
y1=find(min(y)<=y<=max(y));
surf(x1,y1,S)
Blida University (Algeria) 3rd Level: L3 LMD (Avionics)
TP N0 05
Vectors and Matrices
%------------------------------------------------------------------------------------%
Exercise:01 Given a vector 𝑨 ∈ ℝ3 , write a MATLAB code to calculate 𝑩, 𝑪, 𝑫 where
2
𝑩(𝑖) = 𝑨(𝑖) + 𝑪(𝑖) + 𝑫(𝑖), 𝑪(𝑖) = (𝑨(𝑖)) + 2𝑨(𝑖), 𝑫(𝑖) = 2𝑨(𝑖) + 3𝑪(𝑖). Given that 𝑨 = (1 2 3)
Exercise:02 Given a two matrices 𝑨 ∈ ℝ𝑛×𝑛 , 𝑩 ∈ ℝ𝑛×𝑛 , write a MATLAB code to calculate
1 2 5 6
𝑪 = 2𝑨 + 𝑩, 𝑫 = 𝑨𝑇 + 𝑩𝑇 . Given that 𝑨 = ( ), 𝑩=( )
3 4 7 8
Exercise:03 Given a matrix 𝑨 ∈ ℝ𝑛×𝑛 and a three vectors 𝐱, 𝐲, 𝐳 ∈ ℝ𝑛×𝑛 where 𝐱 is the
diagonal elements of 𝑨, the elements of 𝐲 are the elements of second row and 𝐳 is the
elements of third row, write a MATLAB code to calculate 𝐱, 𝐲, 𝐳. Given that
1 2 3
𝑨 = (4 5 6) ∈ ℝ3×3
7 8 9
Exercise:04 The matrix 𝑨 and the vector 𝐱 write a MATLAB code to calculate the 𝐲 = 𝑨𝐱.
1 2 5
Given that 𝑨 = ( ) & 𝐱=( )
3 4 6
Exercise:05 Given two matrices 𝑨, 𝑩 ∈ ℝ𝑛×𝑛 write a MATLAB code to calculate
1 2 5 6
𝑪 = 𝑨. 𝑩 & 𝑫 = 𝑨. 𝑩 − 𝑩. 𝑨. Given that 𝑨=( ) & 𝑩=( ) where stands for the
3 4 7 8
elementwise product (i.e. Hadamard product).
Exercise:06 The matrix 𝑨 and the vector 𝐱 write a MATLAB code to calculate the
1
𝐲(𝑖) = [𝑨]
⏟𝐱 𝐳(𝑖) = (𝐲(𝑖) − 𝐱(𝑖))
𝑨(𝑖, 𝑗)
𝑖≠𝑗
1 2 3 1
Given that 𝑨 = (4 5 6) ∈ ℝ3×3 𝐱 = (2) ∈ ℝ3
7 8 9 3
Blida University (Algeria) 3rd Level: L3 LMD (Avionics)
Exercise:08 write a MATLAB code to calculate the average, max and min of a vector.
Exercise:09 write a MATLAB code to calculate the average sum of two vectors.
Exercise:11 Given two vectors 𝑨, 𝑩 ∈ ℝ𝑛 write a MATLAB code to determine the max of
𝑪(𝑖) = |𝑨(𝑖) − 𝑩(𝑖)|.
Exercise:12 Given two vectors 𝑨, 𝑩 ∈ ℝ𝑛 write a MATLAB code which allow you to add
one to any even element and to subtract one from any odd element.
Exercise:13 Given two vectors 𝑨, 𝑩 ∈ ℝ𝑛 write a MATLAB code calculate the scalar
product 𝑨. 𝑩
Exercise:14 Given a vector 𝑨 ∈ ℝ𝑛 , write a MATLAB code to calculate sum of all positive
elements and the sum of all negative elements of 𝑨.
2𝑨(𝑖, 𝑗) − 𝑩(𝑖, 𝑗) if 𝑖 ≥ 𝑗
𝑫(𝑖, 𝑗) = {
𝑨(𝑖, 𝑗) + 𝑩(𝑖, 𝑗) if 𝑖 < 𝑗
Exercise:19 Given 2 matrices 𝑨 ∈ ℝ𝑛×𝑚 & 𝑩 ∈ ℝ𝑚×𝑛 write a MATLAB code to calculate the
matrix product 𝑪 = 𝑨𝑩 that is 𝑪(𝑖, 𝑗) = ∑𝑚
𝑘=1 𝑨(𝑖, 𝑘)𝑩(𝑘, 𝑗)
Exercise:20 Given 2 matrices 𝑨 ∈ ℝ𝑛×𝑚 & 𝑩 ∈ ℝ𝑚×𝑛 and one vector 𝐱 ∈ ℝ𝑛 , write a
MATLAB code to calculate 𝐲 = 2𝑨𝑩𝐱 + 3𝐱.
Exercise:21 Given a matrix 𝑨 ∈ ℝ𝑛×𝑛 , write a MATLAB code to calculate 𝑩 = 𝑨𝑛 , and for a
given set of scalars 𝛼0 , 𝛼1 , 𝛼2 , … , 𝛼𝑛 write a MATLAB code to calculate 𝑪 = ∑𝑛𝑘=0 𝛼𝑘 𝑨𝑘 .
Appendix A
Searching for Maximum value of an Array: Sometimes, given an array of numbers in system
engineering, the smallest or largest value needs to be identified — and quickly!
There are several built-in ways to find a minimum or maximum value from an array in
MATLAB. Here three algorithms that can in fastest way compute these values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simple Methods for Find Max value of Vector array
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all, clc, Time =[1 2 3 4 101 6 7 8 100];
Max=Time(1); k=1;
while (k<=length(Time))
if (Time(k)> Max)
Max = Time(k);
end
k=k+1;
end
Max
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simple Methods for Find Max value of Vector array
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all, clc, Time =rand(1,10)
Max=Time(1);
for k=1:length(Time)
if (Time(k)> Max)
Max = Time(k);
end
end
Max
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simple Methods for Find Max value of Matrix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all, clc, A =[1 2 3;4 101 6;7 8 100];
Maxvalue=-inf;
Row=0; Column=0;
for i=1:size(A,1)
for j=1:size(A,2)
if A(i,j)> Maxvalue
Maxvalue= A(i,j);
Row=i; Column=j;
end
end
end
Maxvalue
Appendix A
The Left/right and Up/Down Flip Operator: The swapping (Flip) operator is very important in
signal processing and even in engineering. It returns to flip the entries in each row in the
left/right direction. Columns are preserved, but appear in a different order than before.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simple Methods For right/left reflection
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all, clc, A =10*rand(5,5); [m,n]=size(A);
for i=1:m
for j=1:n
B(i,j)= A(i,end-j+1);
end
end
A, B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simple Methods for Up/Down reflection
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all, clc, A =10*rand(5,5), [m,n]=size(A);
for i=1:m
for j=1:n
C(i,j)= A(end-i+1,j);
end
end
A, C
Upper/Lower Triangular (Sum Decomposition): Any matrix can be decomposed into a sum of
upper and lower triangular matrices. Here an algorithm that does this decomposition.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simple Method for Upper/Lower Triangular (Sum Decomposition)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all, clc, A =10*rand(3,3)
[m,n]=size(A); C=zeros(m,n); B=zeros(m,n); D=zeros(m,n);
for i=1:m
for j=1:n
D(i,i)= A(i,i);
if i>j
B(i,j)= A(i,j);
C(i,j)= C(i,j);
elseif i<j
C(i,j)= A(i,j);
B(i,j)= B(i,j);
end
end
end
C,B,D,
Appendix A
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simple Sorting Methods for Vectors "in descending order"
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all, clc, V=10*rand(1,4), V1=V; n = length(V);
while n ~= 0
nn = 0;
for ii = 1:n-1
if V(ii) > V(ii+1)
[V(ii+1),V(ii)] = deal(V(ii), V(ii+1));
nn = ii;
end
end
n = nn;
end
V1, V
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simple Sorting Methods for Matrices (Rows) "in descending order"
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all, clc, A=10*rand(4,4); B = zeros(size(A)); n = size(A,2);
for iRow = 1:size(A, 1)
v = A(iRow, :);
while any(diff(v) > 0) % Test if sorted in descending order
v = v(randperm(n,n)); % If not, shuffle the elements randomly
end
B(iRow,:) = v;
end
A,B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simple Sorting Methods for Matrices (COLUMNS)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all, clc, A=10*rand(4,4); B = zeros(size(A)); n = size(A,2);
for iCOL = 1:size(A, 2)
v = A(:, iCOL);
while any(diff(v) > 0) % Test if sorted in descending order
v = v(randperm(n, n)); % If not, shuffle the elements randomly
end
B(:, iCOL) = v;
end
A, B