This example shows you how to solve algebraic equations using the Symbolic Math Toolbox™ and return complete, parameterized solutions or any conditions on the solution.
To solve algebraic equations symbolically, use the
solve function. The solve function can provide complete information about all solutions of an equation, even if there are infinitely many, by introducing a parameterization. It can also provide information under which conditions these solutions are valid. To obtain this information, set the option ReturnConditions to true.
Solve the equation
sin(C*x) = 1. Specify
x as the variable to solve for. The
solve function handles
C as a constant. Provide three output variables for the solution, the newly generated parameters in the solution, and the conditions on the solution.
syms C x eq = sin(C*x) == 1; [solx, params, conds] = solve(eq, x, 'ReturnConditions', true)
solx = (pi/2 + 2*pi*k)/C params = k conds = C ~= 0 & in(k, 'integer')
To verify the solution, substitute the solution into the equation using
subs. To work on under the assumptions in
conds for the rest of this example, use
assume. Test the solution using
isAlways function returns logical
true) indicating that the solution always holds under the given assumptions.
SolutionCorrect = subs(eq, x, solx) assume(conds) isAlways(SolutionCorrect)
SolutionCorrect = sin(pi/2 + 2*pi*k) == 1 ans = 1
To obtain one solution out of the infinitely many solutions, find a value of the parameters
params by solving the conditions
conds for the parameters; do not specify the ReturnConditions option. Substitute this value of
k into the solution using
subs to obtain a solution out of the solution set.
k0 = solve(conds, params) subs(solx, params, k0)
k0 = 0 ans = pi/(2*C)
To obtain a parameter value that satisfies a certain condition, add the condition to the input to
solve. Find a value of the parameter greater than
99/4 and substitute in to find the solution.
k1 = solve([conds, params > 99/4], params) subs(solx, params, k1)
k1 = 26 ans = (105*pi)/(2*C)
To find a solution in a specified interval, you can solve the original equation with the inequalities that specify the interval.
[solx1, params1, conds1] = solve([eq, x > 2, x < 7], x, 'ReturnConditions', true)
solx1 = (pi + 4*pi*k)/(2*C) params1 = k conds1 = 0 < C & 4*C < pi + 4*pi*k & pi + 4*pi*k < 14*C | C < 0 & pi + 4*pi*k < 4*C & 14*C < pi + 4*pi*k
Alternatively, you can also use the existing solution, and restrict it with additional conditions. Note that while the condition changes, the solution remains the same. The
solve function expresses
solx1 with different parameterizations, although they are equivalent.
[ ~, ~, conds2] = solve(x == solx, x < 7, x > 2, x, 'ReturnConditions', true)
conds2 = 4/pi < (4*k + 1)/C & (4*k + 1)/C < 14/pi
Obtain those parameter values that satisfy the new condition, for a particular value of the constant C:
conds3 = subs(conds2, C, 5) solve(conds3, params)
conds3 = 4/pi < (4*k)/5 + 1/5 & (4*k)/5 + 1/5 < 14/pi ans = 2 3 4 5