GE 209 - COMPUTER PROGRAMING USING
MATLAB
Lectures
Dr. Salim Mokraoui
College of Engineering
smokraoui@[Link]
Chapter 9
Repetition Structures
(Examples)
Example 1 What is the output of the following code?
MATLAB Program Output (choose 1 answer)
a) 123 12 1
for i = 1:3
for j = 1:i b) 1 22 3332
fprintf('%d', j); c) 112123
end
d) 1
fprintf('\n');
12
end
123
for i = 1:3
a) 8
for j = 1:2
A(i,j) = i*j; b) 4
end c) 2
end
d) 6
disp(A(2,1));
a = 0;
b = 1;
a) 3
while a < 5
while b < 3 b) 5
b = b + 1; c) 9
a = a + b;
d) 7
end
end
disp(a)
Example 2
Consider the following matrix A of size 6x6 defined as follows:
(𝑖𝑖 + 1)
�(𝑗𝑗 + 1) 𝑓𝑓𝑓𝑓𝑓𝑓 𝑖𝑖 < 𝑗𝑗
𝐴𝐴 𝑖𝑖, 𝑗𝑗 = 𝑖𝑖 + 𝑗𝑗 𝑓𝑓𝑓𝑓𝑓𝑓 𝑖𝑖 = 𝑗𝑗
𝑖𝑖� 𝑓𝑓𝑓𝑓𝑓𝑓 𝑖𝑖 > 𝑗𝑗
𝑗𝑗
Write a MATLAB program that define the matrix A using the nested for-loop
Example 3
Cam is a mechanical device that transforms rotary motion into linear
motion. The motion of a certain cam is given by the following
equations:
𝜋𝜋
𝑦𝑦 = 6 2𝜃𝜃 − 0.5𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠 for 0 ≤ 𝜃𝜃 <
2
𝜋𝜋 2𝜋𝜋
𝑦𝑦 = 6 for ≤ 𝜃𝜃 <
2 3
2 2𝜋𝜋 4𝜋𝜋
𝑦𝑦 = 6 − 3 1 − 0.5𝑐𝑐𝑐𝑐𝑐𝑐 3 𝜃𝜃 − 𝜋𝜋 for ≤ 𝜃𝜃 <
3 3 3
4𝜋𝜋 3𝜋𝜋
𝑦𝑦 = 3 for ≤ 𝜃𝜃 <
3 2
3𝜋𝜋 2
𝜃𝜃 − 2 3𝜋𝜋 7𝜋𝜋
𝑦𝑦 = 3 − 1.5 𝜋𝜋 for ≤ 𝜃𝜃 <
2 4
4
7𝜋𝜋 2
3 3 𝜃𝜃 − 7𝜋𝜋
𝑦𝑦 = − 1 − 𝜋𝜋 4 for ≤ 𝜃𝜃 ≤ 2𝜋𝜋
4 4 4
4
You are required to complete the following program by filling the blanks of the
written code for implementation of the above problem in Matlab to obtain the
next correct plot (as given by the figure below).
% Defines range of the parameter theta (for 100 elements)
……………………………=………………………………………………………………………………………………………………………;
% Start “for loop” using the array of variable y
……………… k =…………:………………………………
if ………………………………………………………………………
y(k)=6*(2*theta(k)-0.5*sin(theta(k)));
…………………… theta(k)<2*pi/3
y(k)=6;
elseif theta(k)<4*pi/3
y(k)= …………………………………………………………………………………………………………………………
elseif …………………………………………………………
y(k)= 3 ;
elseif ……………………………………………………………………………………………………………………………………
y(k)=3-1.5*((theta(k)-3*pi/2)/(pi/4))^2 ;
……………
y(k)=0.75-0.75*(1-(theta(k)-7*pi/4)/(pi/4))^2 ;
……………
……………
% Plot the graph of y versus theta giving all labels
…………………………………………………………………………………………………………………………………………………………………
Example 4
The value of sin(x) can be approximated as:
𝑥𝑥 3 𝑥𝑥 5 𝑥𝑥 7 𝑥𝑥 9 (𝑛𝑛−1) 𝑥𝑥
(2𝑛𝑛−1)
𝑠𝑠𝑠𝑠𝑠𝑠 𝑥𝑥 = 𝑥𝑥 − + − + − ⋯ + −1
3! 5! 7! 9! 2𝑛𝑛−1 !
Make a MATLAB program to calculate sin of 0.85 using a while loop and a stopping
criteria. The loop should stop ones the value of the last term of the series is < 0.00001
x=0.85; k=0; y=1; sinx=0;
while abs(y)>=1e-5
k=k+1;
y=(-1)^(k-1)*x^(2*k-1)/factorial(2*k-1);
sinx=sinx+y;
disp([k abs(y) sinx sin(x)])
if k>=100
disp('Convergence problem, check your code')
break
end
end
Write a MATLAB program using a nested for loop to calculate and display all
elements of the Pascal triangle
Based on the matrix representation of the Pascal triangle for any row (i) and any 1 0 0 0 ⋯
column (j), the following conditions are true :
1 1 0 0 ⋯
𝐴𝐴 = 1 2 1 0 ⋯
• the first column and the diagonal positions take the value of 1 1 3 3 1 ⋯
• the upper triangle contains zeros,
1 4 6 4 1
⋯ ⋯ ⋯ ⋯ ⋯
• the rest of the elements is the sum of the two values above it and each row
started with 1. n=7;
for i=1:n
for j=1:n
if j==1 || i==j
P(i,j)=1;
elseif j>i
P(i,j)=0;
else
P(i,j)=P(i-1,j)+P(i-1,j-1);
end
end
end
P