The value of n can be estimated from the following equation: pi^3/32 sigma^infin
ID: 3861685 • Letter: T
Question
The value of n can be estimated from the following equation: pi^3/32 sigma^infinity_n=1 (-1)^n/(2n + 1)^3 Write a MATLAB program in a script file that determines pi from the previous equation by adding terms until the value of pi determined by your program approaches the value of the special variable pi in MATLAB to within 14 decimal digits. Create a variable called error that refers to the absolute difference between the value of n calculated from your program and the value of the special variable pi. The loop should end if this error value reaches 1. 0E-6 or less. Generate a counter for the loop that determines the number of passes the loop takes until it ends. This number is called the number of iterations. Use fprintf to display the pi value calculated by the program in each iteration as: iter = 2, Value of PI = X.XXXXXXXXXX iter = 3, Value of PI = X.XXXXXXXXXX iter = 4, Value of PI = X.XXXXXXXXXX iter = 5, Value of PI = X.XXXXXXXXXX At the end of the loop, the program must display the result using fprintf as: ********************************************** The program takes XXX iterations to reach a value of PI = XX.XXXXXXX with an error of XXXXEXX Where ********* is a line of asterisks, XXX is the number of iterations displayed as an integer value. XX.XXXXX is the value of the pi displayed in f format with 10 decimal digits, and XXXXEXX is the error displayed in e format with 6 decimal digits.Explanation / Answer
Matlab code:
clear all;
close all;
format long;
calcpi= 0;
error = abs(pi-calcpi);
n = 0;
summ = 0;
while(error > 10^(-6))
summ = summ + ((-1)^(n)/((2*n + 1)^3));
calcpi = ((32*summ)^(1/3) );
error = abs(pi-calcpi);
n = n+1;
fprintf("Iteration= %d PI= %d ", n, calcpi );
end
fprintf('The program takes %d iterations to find PI= %d with error = %d',n,calcpi,error);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.