Question: Matlab coding error using nest loops to calculate pi. Question: The value of p can also be expressed using the expression below as the summation
Matlab coding error using nest loops to calculate pi.
Question:
The value of p can also be expressed using the expression below as the summation of an infinite series. Please write an m file based on this equation to calculate p so that the value of p is accurate to the 8th digit after the decimal. Please plot the value of p as a function of k.

My code is as follows:
%Value of pi to 8th digit after decimal: 3.14159265
%Following are functions to calculate each element of the series
function a=a(k)
a=4./(8*k+1);
end;
function b=b(k)
b=2/(8*k+4);
end;
function c=c(k)
c=1/(8*k+5);
end;
function d=d(k)
d=1/(8*k+6);
end;
%Value of pi=3.14159265, so an upper and lower limit must be set to break
%loop once the accuracy to the 8th digit is met
lowpi=3.1415926499;
highpi=3.1415926501;
k=0; %starting k
calculated_pi=0; %starting calculation of pi
while true
calculated_pi += (a(k)-b(k)-c(k)-d(k))./(16.^k);
if calculated_pi >= lowpi && calculated_pi
break;
end;
k += 1; %set increment of k
end
k %prints value of k needed to reach 8th decimal accuracy
format long
calculated_pi %prints value of pi
However, I keep getting an error message saying, " A nested function cannot be defined inside a control statement (if, while, for, switch or try/catch). " from the four functions defined at the beginning of my code. I don't understand how they are nested inside the while statement.
oo -316k(8k4 1 8k2 4 8k 1-5 8k1-6) 16k (8k + 1 8k +4 8k + 5 8k + 6 k=0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
