0% found this document useful (0 votes)
11 views

LAB Total

First Laboratory Assignment in MATLAB Environments “Familiarization with MATLAB” Level: L3 Avionics

Uploaded by

BEKHITI Belkacem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

LAB Total

First Laboratory Assignment in MATLAB Environments “Familiarization with MATLAB” Level: L3 Avionics

Uploaded by

BEKHITI Belkacem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

First Laboratory Assignment in MATLAB Environments Level: L3 Avionics

“Familiarization with MATLAB” Submitted to the students of


IASS Blida-1

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.

MATRICES, OPERATIONS AND BASIC MATLAB FUNCTIONS:


 Matrices and Functions Operation:

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]

 Scalar, Vector and Matrix functions:


First Laboratory Assignment in MATLAB Environments Level: L3 Avionics
“Familiarization with MATLAB” Submitted to the students of
IASS Blida-1

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)

Effective documentation can be accomplished with the use of:

1. Proper selection of variable names to reflect the quantities they represent.


2. Comments within the program.
3. Structure charts.
4. Flowcharts.
5. A verbal description of the program, often in Pseudocode.

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, 𝐴 = √𝑠(𝑠 − 𝑎)(𝑠 − 𝑏)(𝑠 − 𝑐)

1. Enter the side lengths 𝑎, 𝑏 and 𝑐.


2. Compute the perimeter. 𝑃 = 𝑎 + 𝑏 + 𝑐
𝑃
3. Compute the semi-perimeter. 𝑠 = 2
4. Compute the area. 𝐴 = √𝑠(𝑠 − 𝑎)(𝑠 − 𝑏)(𝑠 − 𝑐)
5. Display the results 𝑃 and 𝐴.
6. Stop.

Example 2: (Conditional Operations) Given the (𝑥, 𝑦) coordinates of a point, compute its polar coordinates (𝑟, 𝜃),
where 𝑟 = √𝑥 2 + 𝑦 2 , 𝜃 = tan−1(𝑦/𝑥 )

1. Enter the coordinates 𝐱 and 𝐲.


2. Compute the hypoteneuse (‫𝐫 )وتر المثلث‬.
𝐫 = 𝐬𝐪𝐫𝐭(𝐱^𝟐 + 𝐲^𝟐)
3. Compute the angle 𝛉.
3.1 If 𝐱 ≥ 𝟎 𝛉 = 𝑎𝑡𝑎𝑛(𝐲/𝐱)
3.2 Else 𝛉 = 𝐚𝐭𝐚𝐧(𝐲/𝐱) + 𝐩𝐢
4. Convert the angle to degrees. 𝛉 = (𝟏𝟖𝟎𝛉)/𝜋
5. Display the results 𝐫 and theta.
6. Stop.
First Laboratory Assignment in MATLAB Environments Level: L3 Avionics
“Familiarization with MATLAB” Submitted to the students of
IASS Blida-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.

1. Initialize the total to zero.


2. Initialize the counter to zero.
3. While the total is less than 20 000 compute the total.
3.1 Increment the counter by 1. 𝐤 = 𝐤 + 𝟏
3.2 Update the total. total = 𝐒(𝐤) + total
4. Display the current value of the counter.
5. Display the value of the total.
6. Stop.

The above program implements the pseudocode. The


statements in the while loop are executed until the
variable total equals or exceeds 𝟐 × 𝟏𝟎𝟒 .

 Relational Operators and Logical Variables:

General Remarks on Structures:


A loop is a structure for repeating a calculation a
number of times. Each repetition of the loop is a pass.
MATLAB uses two types of explicit loops: the for loop,
when the number of passes is known ahead of time, and
the while loop, when the looping process must terminate
when a specified condition is satisfied and thus the
number of passes is not known in advance.

The switch structure provides an alternative to using


the if, elseif, and else commands. Anything programmed
using switch can also be programmed using if
structures. However, for some applications the switch
structure is more readable than code using the if
structure. The syntax and example are

Example: Write a script file to determine the number of terms


required for the sum of the series 𝐒𝐤 = 𝟓𝐤 𝟐 − 𝟐𝐤, 𝐤 =
𝟏, 𝟐, 𝟑, . .., to exceed 10 000. What is the sum for this many terms?

Solution: because we do not know how many times we must evaluate


the expression 𝟓𝐤 𝟐 − 𝟐𝐤, we use a while loop. The script file is the
following:
First Laboratory Assignment in MATLAB Environments Level: L3 Avionics
“Familiarization with MATLAB” Submitted to the students of
IASS Blida-1

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.

The xy Plotting Functions:

Complex function Plot

Labeling Curves and Data:


First Laboratory Assignment in MATLAB Environments Level: L3 Avionics
“Familiarization with MATLAB” Submitted to the students of
IASS Blida-1

The hold Command:

Polar Plots:

Three-Dimensional Line Plots:

Surface Mesh Plots:


First Laboratory Assignment in MATLAB Environments Level: L3 Avionics
“Familiarization with MATLAB” Submitted to the students of
IASS Blida-1

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)

Institute of Aeronautics & Space Studies Module: (MATLAB)

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<𝑥

function y = piecewise1(x) function y = piecewise2(x)


if x <= -4 switch x
y = 3; case x * (-3 < x & x <= 0)
elseif -4 < x & x <= -3 y = x^2 + 6*x + 8;
y = -4*x -13; case x * (x <= -4)
elseif -3 < x & x <= 0 y = 3;
y = x^2 + 6*x + 8; case x * (-4 < x & x <= -3)
else y = -4*x - 13;
y = 8; otherwise
end y = 8;
end

clear all, clc, x = -8 : .01 : 4; n= length(x) ;


for ix = 1 : n
y(ix) = piecewise1(x(ix)); % y(ix) = piecewise2(x(ix));
end
plot(x, y), axis([-8 4 -2 9]) , xlabel('x'), ylabel('y'), grid on
Blida University (Algeria) 3rd Level: L3 LMD (Avionics)

Institute of Aeronautics & Space Studies Module: (MATLAB)

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

clear all, clc, a=1; alpha=(abs(a)*sqrt(pi));


f = @(x) ((1/(alpha))*exp(-x.^2/a.^2)).*((x >= -2)) + ((1/(alpha))*exp(-x.^2/a.^2)).*((x <= 2)) +
(x.^2).*((x >= -4)&(x <= -2)) + (x.^2).*((x <= 4)& (x >= 2));
x = linspace(-10, 10, 500);
figure, plot(x, f(x),'-r','linewidth',1.5), xlabel('x'), ylabel('y'), grid on

Example: 04 (Given a real input 𝑥) write MATLAB code and plot of the function

clear all, clc, x = -2 : .01 : 7;


ind = (-1 <= x) & (x < 2); y(ind) = x(ind).^2 + x(ind);
ind = (2 <= x) & (x < 4); y(ind) = x(ind) + 2;
ind = (4 <= x) & (x < 6); y(ind) = 0.05*(x(ind).^3 + x(ind).^2 - 3);
ind = x >= 6; y(ind) = exp(0.1*(x(ind)-6));
plot(x, y), axis([-2 7 -2 15]) , xlabel('x'), ylabel('y'), grid on

%----------------------------- Defining a function symbolically----------------------------%


clear all, clc, syms x
y = piecewise(x<-2, -2, -2<x<2, x, x>2, 2);
fplot(y)
%------------------------------------------------------------------------------------%

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)

Institute of Aeronautics & Space Studies Module: (MATLAB)

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

|𝑥 3 − 1| if 𝑥 < 1 or 𝑥 > 5 𝑥 2 + cos(𝑥) if 𝑥 < 2 or 𝑥 > 7


f(𝑥) = { √𝑥 + 1 if 1 ≤ 𝑥 < 3 g(𝑥) = { sin(𝑥) if 2 ≤ 𝑥 < 7
ln(𝑥) if 3 ≤ 𝑥 ≤ 5 |𝑥 − tan(𝑥)| if 𝑥 = 7

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

f(𝑥) − 2g(𝑥) if 𝑥 > 1


𝑤={ with f(𝑥) = |cos(𝑥) + sin(𝑥)|, g(𝑥) = 𝑥 3 + 𝑥 − 1
2 f(𝑥) − g(𝑥) if 𝑥 ≤ 1

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)

Institute of Aeronautics & Space Studies Module: (MATLAB)

TP N0 03
Iterations and For Statement

Example: 01 (Given a large integer 𝑛) write MATLAB code to compute the next sequences

clear all, clc, n=100; sum=0


𝜋2 𝑛 (−1)𝑘+1 for k=1:n
=∑ sum = sum +((-1) ^(k+1) ) *(1/k)^2;
12 𝑘=1 𝑘2 end
sum
%------------------------------------------------------------------------------------%
Example: 02 (Given an integer 𝑛) write MATLAB code to compute the factorial sequences

clear all, clc, n=5; P=1


𝑛 for k=1:n
P[𝑛] = ∏ 𝑘 P = P*k;
𝑘=1
end
P % factorial(n)
%------------------------------------------------------------------------------------%

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)

Institute of Aeronautics & Space Studies Module: (MATLAB)

Exercise:07 write a MATLAB code to calculate the following


𝑛 𝑥
∑ if 𝑥 < 0
𝑘=1 𝑘
𝑛 𝑥
f(𝑥, 𝑛) = ∑ if 0 ≤ 𝑥 < 2
𝑘=1 2𝑘 − 1
𝑛 𝑥
∑ if 𝑥 ≥ 2
{ 𝑘=1 3𝑘 − 1

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:11 (Given an integers 𝑛, 𝑚) write a MATLAB code to calculate the following


𝑛 𝑚 1
𝐴=∑ (∑ )
𝑖=1 𝑗=1 𝑖 × 𝑗

Exercise:12 (Given an integers 𝑛, 𝑚, ℓ) write a MATLAB code to calculate the following


𝑛 2𝑘 − 1 𝑚 ℓ (−1)𝑖+𝑗
𝑆 =7+∑ + ∑ (∑ )
𝑘=1 𝑘3 𝑖=1 𝑗=1 𝑖 + 𝑗

Exercise:13 write a MATLAB code to check if a number is even or odd

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)

Institute of Aeronautics & Space Studies Module: (MATLAB)

𝜋 𝑛 (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)

Institute of Aeronautics & Space Studies Module: (MATLAB)

TP N0 04
Iterations (While and For loops)

Example: 01 write MATLAB code to solve the next problem

Use a while loop to determine clear all, clc


how many months it will take to sum=1000
accumulate 15000000 DA in month=0
your retirement account under while sum<1500000
the following conditions: initial sum=(sum+750)*1.5
deposit = 1,000 DA, you pay 750 month=month+1
DA into the account each end
month. disp(month)

Example: 02 The problem is to find the smallest positive integer 𝑘 so that 2−𝑘 = 𝜀 in
floating point arithmetic (𝜀 = 0.001).

x=1; k=0; epsi=0.001;


while x>epsi
x=x/2;
k=k+1;
end
k
Example: 03 find the smallest integer 𝑝 so that 1 + 2−𝑘 = 1 in floating point arithmetic.
x=1; k=0; y=1; z=x+y;
while x~=z
y=y/2; k=k+1;
z=x+y;
end
k, 2*y, eps
%------------------------------------------------------------------------------------%
Exercise:01 (Given 2 inputs 𝑥 and 𝑛) write a MATLAB code to calculate

𝑛 𝑥𝑘 𝑛 𝑥 2𝑘+1 𝑛 ((2𝑘 − 1)𝑥 + 1)


S[𝑛] = ∑ (−1)𝑘+1 , S[𝑛] = ∑ (−1)𝑘 , S[𝑛] = 1 + (∑ )
𝑘=1 𝑘 𝑘=0 2𝑘 + 1 𝑘=1 𝑥𝑘

Repeat the same program if −3 ≤ 𝑥 ≤ 3

Exercise:02 write a MATLAB code to calculate the sequence S[𝑛] < 24 and find such 𝑛
𝑛 𝜋 2𝑛 𝜋
S[𝑛] = 1 + ∑ sin ( ) + ∑ cos ( )
𝑘=1 𝑘 𝑘=1 2𝑘 − 1

Exercise:03 write a MATLAB code to calculate the following

𝑥 𝑥2 𝑥𝑛 𝑛 𝑥𝑘
𝑃 = (1 + ) (1 + ) … (1 + ) = ∏ (1 + ) with −5≤𝑥 ≤5
1 2 𝑛 𝑘=1 𝑘
Blida University (Algeria) 3rd Level: L3 LMD (Avionics)

Institute of Aeronautics & Space Studies Module: (MATLAB)

Exercise:04 (Given a real inputs 𝑥, 𝑦) write a MATLAB code to calculate the

2 𝑛 1
𝑇(𝑥, 𝑦) = 1 + ∑ sin(𝑘𝜋𝑥) 𝑒 −(𝑘𝜋𝑦)
𝜋 𝑘=1 𝑘

plot the results.

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

ℎ f(𝑎) + f(𝑏) 𝑚−1 𝑚−1


S[𝑛] = ( + 2∑ f(𝑎 + 2𝑘ℎ) + 4 ∑ f(𝑎 + (2𝑘 + 1)ℎ)) with 𝑚 = 𝑛/2
3 2 𝑘=1 𝑘=0

Repeat for f(𝑥) = 1/𝑥 calculate the sum S < 10 given that 𝑎 = 1 and 𝑛 = 10

Exercise:06 (Given 𝑥 = 0.1 & 𝑛 = 10) write a MATLAB code to calculate


𝑛 𝑛 𝜋𝑥 𝜋𝑥
𝑆=∑ ∑ 𝐴 sin ( ) × cos ( ) (𝐴 = 0 if 𝑖 = 𝑗, 𝐴 = 𝑖 if 𝑖 > 𝑗, 𝐴 = 𝑗 if 𝑗 > 𝑖)
𝑖=1 𝑗=1 𝑖 𝑗

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 𝑘

Determine the value of 𝑥.

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 𝑧𝑚𝑎𝑥 .

Exercise:08 In the exercise 07 at which time the projectile reaches 𝑧 = 300𝑚

%-----------------------------------Homework-----------------------------------%

Exercise:09 (Given 𝑥, 𝑡 & 𝑛) write a MATLAB code to calculate

𝑥2 𝑛 (−1)𝑘+1 (2𝑘 − 1)𝜋𝑥 (2𝑘 − 1)𝜋𝑡


𝑈(𝑥, 𝑡) = +∑ sin ( ) × cos ( )
2 𝑘=1 (2𝑘 − 1) 2 2

Check that if 𝑛 = 10 then 𝑈(0.1,0.1) = 0.0846539


%-------EX01-------- clear all, clc, U=0; n=10; S=[];
x=[-2:0.01:2]; nx=length(x);
clear all, clc, S=0; n=100; for i=1:nx
for k=1:n
x=0.3; U=U+(((-1)^(k+1))/k)*x(i)^(k);
for k=1:n end
S=S+(((-1)^(k+1))/k)*x^(k); S=[S U];
end end
S plot(x,S), grid on
%------------------------------------------------------------------------------------------------------------------%
%-------EX02-------- clear all, clc, n=1; S=1;
while S<24
clear all, clc, n=10; S1=0; S2=0; S1=0; S2=0;
for k=1:n
for k=1:n S1=S1+sin(pi/k);
S1=S1+sin(pi/k); end
end for k=1:2*n
S2=S2+cos(pi/((2*k)-1));
for k=1:2*n end
S2=S2+cos(pi/((2*k)-1)); S(n)=1+S1+S2;
end i=n;
n=n+1;
S=1+S1+S2 end
S(i-1)
%------------------------------------------------------------------------------------------------------------------%
%-------EX03-------- clear all, clc, x=[-2:0.1:2]; nx=length(x);
clear all, clc, x=[0:0.1:2]; nx=length(x); n=10; P=[];
n=3; P=[]; for i=1:nx
for i=1:nx P1=1;
P1=1; for k=1:n
for k=1:n P1=P1*(1+x(i)^k/k);
P1=P1*(1+x(i)^k/k); end
end P=[P P1];
P=[P P1]; end
end P;
plot(x,P), grid on indx = find(abs(x-0.5)<=eps)
P(indx)
%------------------------------------------------------------------------------------------------------------------%
%-------EX04--------
clear all, clc, n=10; T=[];
x=[-0.5:0.1:2]; nx=length(x); indx = find(abs(x-1.2)<=eps)
y=[-0.5:0.1:2]; ny=length(y); indy = find(abs(y-0.8)<=eps)
for i=1:nx
X=[]; T(indx,indy)
for j=1:ny
U=1; x1=find(min(x)<=x<=max(x));
for k=1:n y1=find(min(y)<=y<=max(y));
U=U+(2/(pi*k))*(sin(k*pi*x(i))*exp(-k*pi*y(j)));
end surf(x1,y1,T)
X=[X U];
end
T=[T;X];
end

%------------------------------------------------------------------------------------------------------------------%
%-------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)

Institute of Aeronautics & Space Studies Module: (MATLAB)

TP N0 05
Vectors and Matrices

Example: 01 write MATLAB code to calculate 𝐳 = 𝑨𝐱 + 𝐲

%------------------------------------------------------------------------------------%
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)

Institute of Aeronautics & Space Studies Module: (MATLAB)

Exercise:07 Given a vectors 𝑨, 𝑩 ∈ ℝ4 , write a MATLAB code to calculate


2
𝑪(𝑖) = 2𝑨(𝑖) − 𝑩(𝑖), 𝑫(𝑖) = 𝑬(𝑖) × 𝑩(𝑖), 𝑬(𝑖) = (𝑪(𝑖)) + 1

Given that 𝑨 = (1 2 3), 𝑩 = (4 5 6)

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:10 write a MATLAB code to determine the position of an element in a 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 𝑨.

Exercise:15 Given a matrices 𝑨, 𝑩 ∈ ℝ𝑛×𝑛 , write a MATLAB code to calculate 𝑫 = |𝑨 − 𝑩|,


and search for the max value in 𝑫.

Exercise:16 Given 2 matrices 𝑨, 𝑩 ∈ ℝ𝑛×𝑛 , write a MATLAB code to calculate

2𝑨(𝑖, 𝑗) − 𝑩(𝑖, 𝑗) if 𝑖 ≥ 𝑗
𝑫(𝑖, 𝑗) = {
𝑨(𝑖, 𝑗) + 𝑩(𝑖, 𝑗) if 𝑖 < 𝑗

Exercise:17 Given a matrix 𝑨 ∈ ℝ𝑛×𝑛 and a vector 𝐱 ∈ ℝ𝑛 , write a MATLAB code to


calculate 𝐲(𝑖) = ∑𝑛𝑗=1 𝑨(𝑖, 𝑗)𝐱(𝑗), repeat the same things but now with 𝐲(𝑖) = ∑𝑛𝑗=1 𝑨(𝑖, 𝑗)𝐱(𝑗)
𝑖≠𝑗

Exercise:18 Given a matrix 𝑨 ∈ ℝ𝑛×𝑛 and 2 vectors 𝐱, 𝐲 ∈ ℝ𝑛 , write a MATLAB code to


calculate 𝐳 = 𝑨𝐱 + 𝐲 that is 𝐳(𝑖) = ∑𝑛𝑗=1 𝑨(𝑖, 𝑗)𝐱(𝑗) + 𝐲(𝑖).

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𝐱.

If 𝑪 is the transpose of 𝑩 then write a MATLAB code to calculate 𝑫 = 𝑩𝑪 & 𝑬 = 𝑨𝑪𝑨

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

Sorting Operator: arranges the elements of the collection in ascending or descending


order.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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

You might also like