Calculate using the approximation pi almostequalto 2 sigma^infinity_n = 0 n!/(2n
ID: 2081965 • Letter: C
Question
Calculate using the approximation pi almostequalto 2 sigma^infinity_n = 0 n!/(2n + 1) n! is the factorial: n! = 1 times 2 times 3 times ... times n n is the semifactorial: n = {1 times 3 times ... times n, n is odd 2 times 4 times ... times n, n is even, not to be confused with (n)!, which is the factorial function iterated twice. The semifactorial of n is the product of the integers between 1 and n which have the same parity (even or odd) as n. For example, 7 = 1 times 3 times 5 times 7 = 105, and 8 = 2 times 4 times 6 times 8 = 384. MATLAB has a built-in factorial function, but you will need to write the semifactorial function yourself. The sum given above goes to infinity. Your MATLAB function should take a single input argument which specifies how many terms of the summation should be computed.Explanation / Answer
Code:
prompt = 'interger value? ';
n = input(prompt);
f = 1;
if mod(n,2) == 0
for i = 2:2:n
f = f *i;
end
else
for i = 0: n/2
f = f * (2*i+1);
end
end
f
Function:
function f = semifactorial(n)
f = 1;
if mod(n,2) == 0
for i = 2:2:n
f = f *i;
end
else
for i = 0: n/2
f = f * (2*i+1);
end
end
end
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.