Why does Matlab round this error to 0, even though I know it isn't precisely 0
Show older comments
I am trying to plot Ramanujan's Pi formula and observe how the errors are reducing as the number of iterations increases. However MATLAB simply outputs 0 after the first iteration even when I put the value of digits() to around 300. I know the formula doesn't calculate pi to a 100 decimals after the first iteration, in fact it's an additonal 8 decimal places per iteration after the first, yet it continues to output 0.0 as the value of the function - value of pi calculated to 300 digits. How can I fix this?
This is my code for the formula and the error function (I am using disp() at the end just to test the code).
% Ramanujan's Formula Plot %
rconst = sqrt(8)/9801;
rsum = 0;
rlim = 100;
plt3= 1:(rlim+1);
for n= 0:rlim
ctr = 1;
f4n = factorial(4*n);
fn = factorial(n);
num = (f4n * (1103+26390*n));
den = ((fn^4) * 396^(4*n));
rcalc = (num/den);
rtemp = (rconst * rcalc);
rsum = (rsum + rtemp);
rpi = (1/rsum);
plt3(n+1) = vpa(rpi);
ctr = ctr +1;
end
plot(plt3);
for ii = 1:41
var = abs(plt3(ii) - vpa(pi,334));
phi = sym(var);
disp(vpa(phi,334));
end
Accepted Answer
More Answers (1)
You need to pre-allocate as type 'sym',
plt3= sym(1:(rlim+1));
1 Comment
Anirudh Bhalekar
on 29 Aug 2020
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!