Need help on this problem on MATLAB... Calculation of the value of pi. The Nilak
ID: 3739894 • Letter: N
Question
Need help on this problem on MATLAB...
Calculation of the value of pi. The Nilakantha Series was developed in the 15th century as a way to calculate the value of pi pi 3+4/(2*3*4)-4/(4*56)+ 4/(67 8) -4/(8"910)+. Use a for loop to approximate pi using this technique (5 points). Continue the calculations until the absolute value of the difference between the value of pi stored in MATLAB and the approximation is less than 0.001. Report the appproximation and the number of iterations required (5 points). Hint: to alternate the sign on each term use (1) raised to a powerlExplanation / Answer
% initialize pie to 3
pie = 3;
i = 0;
x = 2;
% infinite loop
while true
% if the error is less than 0.001
if abs(pie - pi) < 0.001
break;
end
% calculate current term
temp = 4 / ( x * ( x + 1 ) * ( x + 2 ) );
% if the current term is even term
if mod( i , 2 ) == 0
pie = pie + temp;
% if the current term is odd term
else
pie = pie - temp;
end
i = i + 1;
x = x + 2;
end
fprintf('Calculate pi : %f ', pie);
fprintf('Actual pi : %f ', pi);
fprintf('No of iterations : %d ', i);
Samle Output
Calculate pi : 3.140881
Actual pi : 3.141593
No of iterations : 6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.