%% Find Rational Approximation of Golden Ratio
% Define the golden ratio X = (1+\sqrt{5})/2 as a symbolic number.

X = (sym(1) + sqrt(5))/ 2

% Find the rational approximation of X within a tolerance of 1e-4.

R = rat(X,1e-4)

% To return the rational approximation with 10 coefficients, set the 'Length' option to 10. This option ignores the specified tolerance in the approximation.

R10 = rat(X,1e-4,'Length',10)

% To return the rational approximation with all positive coefficients, set the 'Positive' option to true.

Rpos = rat(X,1e-4,'Positive',true)

%
% Copyright 2019 The MathWorks, Inc.