LAB-03-Contitional Statements and Loops-Solutions
LAB-03-Contitional Statements and Loops-Solutions
Exercise 06: Write a MATLAB program to fill in the matrix A = [amn] using loops. The matrix entries are
calculated as fellow:
𝑚𝑛 𝑚
𝑎𝑚𝑛 = +
4 2𝑛
Consider 𝑚 ∈ {1, . . . , 100} and 𝑛 ∈ {1, . . . , 20}, run this code in the command window, display A, then save it
in a MAT file.
Exercise 07: Write a MATLAB program to display the numbers from a user specified start number to a user
specified stop number. Assume that the second number the user enters will be larger than the first number.
▪ Use the while loop to solve this problem.
▪ Run the program for start number =4 and Stop number: 12, and verify that it operates correctly.
Exercise 08: Create a MATLAB program consisting of the MATLAB code in the figure and save it in a script
file named “MyProgram”.
clear; clc;
▪ Run the program and verify that the values in the array numbers numbers = 1:0.5:5;
are displayed. for k = 1:1:length(numbers)
▪ Determine the size and value(s) of the variable k after the loop val = numbers(k);
has been executed for the program. fprintf('%0.1f ', val);
▪ Explain the functionality of this program. end
▪ Rewrite this program by using the while loop. fprintf('\n');
Exercise 09: Write a MATLAB program that reads a sequence of float numbers ending in zero (0) from keyboard
and find their sum and average.
1|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
Save the program in a script file (called MyScript.m) and run it for the values of x; 3, -8 and 0.
Script file myscript.m
After running the script file on the command window, you should get the following results:
2|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
Solution of exercise 02
The MATLAB script that takes the current hour as input and outputs a greeting message depending on the time
of day:
▪ Morning: 6:00 AM - 11:59 AM
▪ Afternoon: 12:00 PM - 5:59 PM
▪ Evening: 6:00 PM - 11:59 PM
▪ Night: 12:00 AM - 5:59 AM
The clock function calculates the current date and time from the system time. The statement c = clock returns a
six-element date vector containing the current date and time in decimal form: [year month day hour minute
seconds]. The current hour value is the fourth element of the vector (current hour =c(4)).
First version of the program with the if…else statement:
% exercise 2: time_greeting
% 1. retrieve current hour.
format shortg
c = clock;
current_hour=c(4);
else
disp('Good Night!');
end
1) Save each program in a script file and run them for the current hour in your system.
Running version 1
3|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
Running version 2
Solution of exercise 03
Write a MATLAB program that takes three side lengths (a, b, and c) as inputs and classifies the triangle as either
equilateral, isosceles, or scalen.
▪ Equilateral triangle: All three sides of the triangle are equal
▪ Isosceles triangle: All two sides of the triangle are equal
▪ Scalene triangle: No sides of the triangle are equal
4|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
Explanations:
o If all three sides are equal (a == b && b == c), it prints "Equilateral triangle".
o If at least two sides are equal (a == b || b == c || a == c), it prints "Isosceles triangle".
o If none of the above conditions are met, it concludes that it's a "Scalene triangle".
Solution of exercise 04
sum_even = 0;
for i = 2:2:n
sum_even = sum_even + i;
end
disp(['The sum of even numbers from 1 to ', num2str(n), ' is: ', num2str(sum_even)]);
5|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
Solution of exercise 05
Write a script that uses a for loop to compute and plot the finite Fourier series given by the sum in the equation
below, for the interval 𝑥 = [0, 8𝜋]. The plot you should see is called the 'sawtooth function'.
100
sin(𝑛𝑥)
𝑦(𝑥) = ∑
𝑛
𝑥=1
Solution of exercise 06
MATLAB program
6|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
% Initialize matrix A
A = zeros(m_max, n_max);
Results
Solution of exercise 07
Explanation: This program prompts the user to enter the start and stop numbers. Then, it checks if the stop number
is indeed larger than the start number. If not, it throws an error, by using the error function of MATLAB.
After that, it enters a while loop. As long as current_number (initial value is the value of start number) is less
than or equal to the stop number, it will execute the loop body (it displays the current number and then increments
current_number by 1). The loop continues until current_number is greater than stop_number.
7|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
current_number = start_number;
In case the first number is greater than the second one, it displays the following error message
Solution of exercise 08
After running the script:
As shown in the figure below, the values in the array numbers are:
8|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
The size and value of variable k after running the program are shown in the figure below:
Functionality: This script generates a vector of numbers from 1 to 5 with a step size of 0.5, then uses a for loop
to iterate through the elements of the numbers array and print each value in formatted manner. The loop continues
as long as k is less than or equal to the length of the numbers array.
fprintf: This is a function in MATLAB used for formatted printing. It allows you to print text and variables with
specific formatting. Thus, the statement fprintf('%0.1f ', val); is used to print the value of val with one digit
before the decimal point and one digit after, and it separates it from the next value by a space. For example, if val
is 3.456, it will be printed as "3.5 " (one digit before the decimal point, one digit after, and a space after), as shown
in the figure below:
9|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
Solution of exercise 09
MATLAB program
% Initialize variables
sum_numbers = 0;
count = 0;
10 | P a g e