Solve this using Matlab Exercise 2 Create your own power and factorial functions
ID: 3679208 • Letter: S
Question
Solve this using Matlab
Exercise 2 Create your own power and factorial functions and use them to calculate the value of pi using Eq. (2). Use function functions to compute and display in sequence, the values for up to the order term (N) of your choosing. For each of the preceding, compute and display the percent relative error. Hints (a) Declare Eq. (2) as an anonymous function. (b) Use function functions to accumulate the sum from 1 to N displaying the required values on each iteration value of n compute p for N 8. 1 V8 (4m)! (1103 26,390n) m 9801 (n!) 396 Eq. (2)Explanation / Answer
%enter the value upto which the summation should happen
clear
N = input(‘Enter the value of N: ‘);
%finding square root and then performing division
k=sqrt(8)./9801;
n=0;
h=n;
d=0;
%performing the function
while(h<=N)
{
%calling power function
x = power(396,4h);
%calling factorial function
y = fact(h);
m=fact(4*h)./power(y,h);
o=(1103+(26,390*h))./x;
d=d+(m*o);
g=(d-w)./d;
%printing relative error
fprintf(‘the relative error is %f’,(g*100));
w=d;
h=h+1;
fprintf(‘value is %d ’,d);
}
P=k+d;
fprintf(‘the value after summation is %d’,p)
%function factorial
function fact1 = fact(n)
pro = 1;
while(n>0)
{
pro = pro*n;
n = n-1;
}
end
%function power
function power1 = power(x,n)
l = x.^n;
end