Question 4 Solutions
(a) MATLAB Pseudocode for Sequence an = n2 + 1
Pseudocode:
% Initialize an empty array to store the sequence
for n = 1:10
a(n) = n^2 + 1;
end
% Display the sequence
disp(a)
Explanation: - Array usage: a stores all computed terms. Each index n corresponds to the nth term.
- Loop usage: for loop iterates from 1 to 10, calculates n2 + 1 , and stores it in a(n) . - Resulting
sequence: [2, 5, 10, 17, 26, 37, 50, 65, 82, 101] - disp(a) prints the sequence.
(b) Python Recursive Factorial Analysis
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(5))
(i) Concept Demonstrated
• ✅ Recursion: Function calls itself to solve smaller instances.
(ii) Sequence of Recursive Calls
factorial(5)
→ 5 * factorial(4)
→ 4 * factorial(3)
→ 3 * factorial(2)
→ 2 * factorial(1)
→ 1 * factorial(0)
- Calls resolve in reverse order once factorial(0) returns 1.
1
(iii) Final Output
• ✅ factorial(5) = 5 × 4 × 3 × 2 × 1 = 120
• Output: 120
Presentation Slide Suggestions: 1. Slide 1: Title - "Question 4: MATLAB & Python Analysis" 2. Slide 2:
MATLAB Pseudocode with explanation and resulting array 3. Slide 3: Python factorial code snippet 4.
Slide 4: Recursion concept and call sequence diagram 5. Slide 5: Final output and summary