IN MATLAB RECURSIVE FUNCTION 1. Sum of Factorials. The Recursive Function: A ser
ID: 2253080 • Letter: I
Question
IN MATLAB
RECURSIVE FUNCTION 1. Sum of Factorials. The Recursive Function: A series that sums up the factorial of the natural numbers from 1 to N can be expressed as The recursive algorithm: N-1 N-2 N-3 Write independent matlab code to solve the above problem with the following methods: 1. 2. 3. A monolithic program (with no functions) A standard (non-recursive) user defined function (an a program to call it) A recursive function (an a program to call it) Test your programs with N-7. These will produce three independent codes & output.Explanation / Answer
1)
%%%% Matlab code %%%
clc;
clear all;
close all;
format long;
N=input('Enter the limit of the sum, N = ');
sum=0;
for n=1:N
sum=sum+factorial(n);
end
fprintf(' sum = %d ', sum);
OUTPUT:
Enter the limit of the sum, N = 7
sum = 5913
2)
%% function %%%
function [sum] = fact_ (N)
sum=0;
for n=1:N
sum=sum+factorial(n);
end
end
%%%% calling main function %%%
clc;
clear all;
close all;
format long;
N=input('Enter the limit of the sum, N = ');
sum=fact_(N);
fprintf(' sum = %d ', sum);
OUTPUT:
Enter the limit of the sum, N = 7
sum = 5913
3)
%%% Recursive function for factorial %%%
function [sum] = fact_ (N)
sum=1;
if (N>=1)
sum=N*fact_(N-1);
else
sum=1;
end
%%%% main programme
clc;
clear all;
close all;
format long;
N=input('Enter the limit of the sum, N = ');
sum=0;
for n=1:N
sum=sum+fact_(n);
end
fprintf(' sum = %d ', sum);
OUTPUT:
Enter the limit of the sum, N = 7
sum = 5913