calculate pi using viete formula in matlab

Here's the implementation of viete formula in MATLAB to calculate pi:

main.m
clear; clc;
terms = 10; % number of terms for calculation
a = 1;
for i=1:terms
    a = sqrt(2 + a);
end
pi = 2^(terms+1)/a;
fprintf('Value of pi using %d terms of Vieta\'s formula is %f\n', terms, pi);
202 chars
9 lines

This code calculates the value of pi using Vieta's formula based on the specified number of terms (in this case, 10). Vieta's formula provides an approximation of pi based on the square root of 2 and an iteratively increasing value a.

Output:

main.m
Value of pi using 10 terms of Vieta's formula is 3.141592
58 chars
2 lines

related categories

gistlibby LogSnag