%% Approximate Value of π with Different Precision
% Represent the mathematical quantity \pi as a symbolic constant. The constant \pi is an irrational number.

X = sym(pi)

% Use vpa to show the decimal representation of \pi with 12 significant digits.

Xdec = vpa(X,12)

% Find the rational fraction approximation of \pi using the rat function with default tolerance. The resulting expression is a character vector.

R = rat(sym(pi))

% Use str2sym to turn the character vector into a single fractional number.

Q = str2sym(R)

% Show the decimal representation of the fractional number 355/113. This approximation agrees with \pi to 6 decimal places.

Qdec = vpa(Q,12)

% You can specify a tolerance for additional accuracy in the approximation.

R = rat(sym(pi),1e-8)
Q = str2sym(R)

% The resulting approximation, 104348/33215, agrees with \pi to 9 decimal places.

Qdec = vpa(Q,12)

%
% Copyright 2019 The MathWorks, Inc.