Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The value of pi can be estimated from the following equation: pi^3/32 = sigma^in

ID: 2080618 • Letter: T

Question

The value of pi can be estimated from the following equation: pi^3/32 = sigma^infinity_n = 0 (-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 pi 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 f 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


% initialise variables
pi_initial_cube=0;
n=0;
pi_new_cube=0;

% the value of pi is to be calculated at least once .. therefore some
% statements are outside the loop
pi_new_cube=pi_initial_cube+32*(((-1)^n)/((2*n+1)^3)); % based on the given formula
pi_calculated=pi_new_cube^(1/3);
pi_calculated=round(pi_calculated*1e14)/1e14;
error=abs(pi_calculated-pi); % error between our calculated value of pi and matlab special variable 'pi'
  
iter=n+1; % first iteration is for n=0;
fprintf(' iter=%d',iter);
fprintf(', value of pi=%f',pi_calculated);
  
pi_initial_cube=pi_new_cube; % updating variables for second iteration.. first iteration of while is our second iteration
%% as one iteration is done before while loop
n=n+1;
  
% depending on the value of error, the following loop will be executed.
% therefore, we repeat the statments above
  
while abs(error)>=1e-6
pi_new_cube=pi_initial_cube+32*(((-1)^n)/((2*n+1)^3));
pi_calculated=pi_new_cube^(1/3);
pi_calculated=round(pi_calculated*1e14)/1e14; %% we need value of pi within 14 decimal places
error=abs(pi_calculated-pi);
  
n=n+1;
pi_initial_cube=pi_new_cube; % updating variables for next iteration of while loop
iter=n+1; %% while loop has n starting from 1, which is the second iteration
fprintf(' iter=%d',iter);
fprintf(', value of pi=%1.14f',pi_calculated);
end

number_of_iterations=iter;

fprintf(' This program takes %d iterations to reach a value of pi=%1.10f with an error of %1.6e',number_of_iterations,pi_calculated,error);

OUTPUT

iter=1, value of pi=3.174802
iter=3, value of pi=3.13511291696101
iter=4, value of pi=3.14377083641878
iter=5, value of pi=3.14062114485715
iter=6, value of pi=3.14210388509366
iter=7, value of pi=3.14129194905678
iter=8, value of pi=3.14178389122763
iter=9, value of pi=3.14146367259822
iter=10, value of pi=3.14168365475933
iter=11, value of pi=3.14152608792951
iter=12, value of pi=3.14164278860378
iter=13, value of pi=3.14155396183010
iter=14, value of pi=3.14162313060566
iter=15, value of pi=3.14156822245079
iter=16, value of pi=3.14161253590591
iter=17, value of pi=3.14157625789938
iter=18, value of pi=3.14160633164585
iter=19, value of pi=3.14158112444928
iter=20, value of pi=3.14160246099173
iter=21, value of pi=3.14158424155425
iter=22, value of pi=3.14159992269186
iter=23, value of pi=3.14158632943633
iter=24, value of pi=3.14159818962358
iter=25, value of pi=3.14158777999311
iter=26, value of pi=3.14159696629716
iter=27, value of pi=3.14158881890945
iter=28, value of pi=3.14159607832113
iter=29, value of pi=3.14158958239824
iter=30, value of pi=3.14159541825272
iter=31, value of pi=3.14159015598540
iter=32, value of pi=3.14159491743844
iter=33, value of pi=3.14159059521413
iter=34, value of pi=3.14159453061460
iter=35, value of pi=3.14159093722300
iter=36, value of pi=3.14159422711564
iter=37, value of pi=3.14159120748437
iter=38, value of pi=3.14159398566458
iter=39, value of pi=3.14159142386486
iter=40, value of pi=3.14159379118404
iter=41, value of pi=3.14159159914798
iter=42, value of pi=3.14159363278731
This program takes 42 iterations to reach a value of pi=3.1415936328 with an error of 9.791975e-07>>