%% Use Parameters and Conditions to Refine Solution
% Return the complete solution of an equation with parameters and conditions of the solution by specifying 'ReturnConditions' as true.
% Solve the equation sin(x)=0. Provide two additional output variables for output arguments parameters and conditions.

syms x
eqn = sin(x) == 0;
[solx,parameters,conditions] = solve(eqn,x,'ReturnConditions',true)

% The solution \pi k contains the parameter k, where k must be an integer. The variable k does not exist in MATLAB workspace and must be accessed using parameters.
% Restrict the solution to 0<x<2\pi. Find a valid value of k for this restriction. Assume the condition, conditions, and use solve to find k. Substitute the value of k found into the solution for x.

assume(conditions)
restriction = [solx > 0, solx < 2*pi];
solk = solve(restriction,parameters)
valx = subs(solx,parameters,solk)

% Alternatively, determine the solution for x by choosing a value of k. Check if the value chosen satisfies the condition on k using isAlways.
% Check if k=4 satisfies the condition on k.

condk4 = subs(conditions,parameters,4);
isAlways(condk4)

% isAlways returns logical 1(true), meaning that 4 is a valid value for k. Substitute k with 4 to obtain a solution for x. Use vpa to obtain a numeric approximation.

valx = subs(solx,parameters,4)
vpa(valx)

%
% Copyright 2006-2019 The MathWorks, Inc.