%% Approximate Solution of Equation
% Solve the equation \cos(x) + x^2 + x = 42 using vpasolve. The solution is returned in decimal representation.

syms x
sol = vpasolve(cos(x) + x^2 + x == 42)

% Approximate the solution as a continued fraction.

R = rat(sol)

% To extract the coefficients in the denominator of the continued fraction, you can use the regexp function and convert them to a character array.

S = char(regexp(R,'(-*\d+','match'))

% Return the result as a symbolic array.

coeffs = sym(S(:,2:end))

% Use str2sym to turns the continued fraction R into a single fractional number.

Q = str2sym(R)

% You can also return the numerator and denominator of the rational approximation by specifying two output arguments for the rat function.

[N,D] = rat(sol)

%
% Copyright 2019 The MathWorks, Inc.