0% found this document useful (0 votes)
2 views2 pages

Matlab Python Question4

The document provides MATLAB pseudocode for generating a sequence based on the formula an = n^2 + 1, resulting in the array [2, 5, 10, 17, 26, 37, 50, 65, 82, 101]. It also includes a Python function for calculating the factorial using recursion, demonstrating the recursive calls and resulting in an output of 120 for factorial(5). Additionally, it suggests presentation slides to summarize the analysis of both MATLAB and Python implementations.

Uploaded by

mustaphawilliam0
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)
2 views2 pages

Matlab Python Question4

The document provides MATLAB pseudocode for generating a sequence based on the formula an = n^2 + 1, resulting in the array [2, 5, 10, 17, 26, 37, 50, 65, 82, 101]. It also includes a Python function for calculating the factorial using recursion, demonstrating the recursive calls and resulting in an output of 120 for factorial(5). Additionally, it suggests presentation slides to summarize the analysis of both MATLAB and Python implementations.

Uploaded by

mustaphawilliam0
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

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

You might also like