Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an M-file in MATLAB to implement the following steps: (a) Request a positi

ID: 2256490 • Letter: W

Question

Write an M-file in MATLAB to implement the following steps:

(a) Request a positive integer n from the user in the command window.

(b) Check whether n1. If not, report an error message and return to the command window.

(c) Generate a column vector v contains{1,2,···} up to n.

(d) Calculate the factorial and the Stirling’s approximation for each entry in v with the entrywise calculation

for a vector. (one statement, no points if the loopis used!)

(e) Generate a n × 3 matrix A with v, factorial and Stirling approximation as columns.

(f) Use the columns of A to calculate the absolute error and the relative error (NOT the ratio!) of the

Stirling’s approximation for each element in v.

(g) Add these two error vectors to A as the last two columns.

(h) Delete the first row of the matrix A

(i) Output the matrix with the title for each column.

Test your code with n=1 and n= 5. Include the outputs as well as your code in your answer.

Explanation / Answer

n = input('Enter a positive integer');
if n < 1
error('n must be positive integer greater than or equal to 1')
end
v = 1:n;
f = factorial(v);
f_approx = sqrt(2*pi*v).*((v/2.718).^v); % Stirling's approximation
A = [v.' f.' f_approx.'];

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote