main content

equations and systems solver -凯发k8网页登录

equations and systems solver

support for character vector or string inputs has been removed. instead, use to declare variables and replace inputs such as solve('2*x == 1','x') with solve(2*x == 1,x).

description

example

s = solve(eqn,var) solves the equation eqn for the variable var. if you do not specify var, the function determines the variable to solve for. for example, solve(x 1 == 2, x) solves the equation x   1 = 2 for x.

example

s = solve(eqn,var,name,value) uses additional options specified by one or more name,value pair arguments.

example

y = solve(eqns,vars) solves the system of equations eqns for the variables vars and returns a structure that contains the solutions. if you do not specify vars, solve uses to find the variables to solve for. in this case, the number of variables that symvar finds is equal to the number of equations eqns.

example

y = solve(eqns,vars,name,value) uses additional options specified by one or more name,value pair arguments.

example

[y1,...,yn] = solve(eqns,vars) solves the system of equations eqns for the variables vars. the solutions are assigned to the variables y1,...,yn. if you do not specify the variables, solve uses symvar to find the variables to solve for. in this case, the number of variables that symvar finds is equal to the number of output arguments n.

[y1,...,yn] = solve(eqns,vars,name,value) uses additional options specified by one or more name,value pair arguments.

example

[y1,...,yn,parameters,conditions] = solve(eqns,vars,'returnconditions',true) returns the additional arguments parameters and conditions that specify the parameters in the solution and the conditions on the solution.

examples

solve the quadratic equation without specifying a variable to solve for. solve chooses x to return the solution.

syms a b c x
eqn = a*x^2   b*x   c == 0
eqn = ax2 bx c=0
s = solve(eqn)
s = 

(-b b2-4ac2a-b-b2-4ac2a)

specify the variable to solve for and solve the quadratic equation for a.

sa = solve(eqn,a)
sa = 

-c bxx2

solve a fifth-degree polynomial. it has five solutions.

syms x
eqn = x^5 == 3125;
s = solve(eqn,x)
s = 

(5-σ1-54-525-5i4-σ1-54 525-5i4σ1-54-525 5i4σ1-54 525 5i4)where  σ1=554

return only real solutions by setting 'real' option to true. the only real solutions of this equation is 5.

s = solve(eqn,x,'real',true)
s = 5

when solve cannot symbolically solve an equation, it tries to find a numeric solution using vpasolve. the vpasolve function returns the first solution found.

try solving the following equation. solve returns a numeric solution because it cannot find a symbolic solution.

syms x
eqn = sin(x) == x^2 - 1;
s = solve(eqn,x)
warning: unable to solve symbolically. returning a numeric solution using .
s = -0.63673265080528201088799090383828

plot the left and the right sides of the equation. observe that the equation also has a positive solution.

fplot([lhs(eqn) rhs(eqn)], [-2 2])

figure contains an axes object. the axes object contains 2 objects of type functionline.

find the other solution by directly calling the numeric solver vpasolve and specifying the interval.

v = vpasolve(eqn,x,[0 2])
v = 1.4096240040025962492355939705895

when solving for multiple variables, it can be more convenient to store the outputs in a structure array than in separate variables. the solve function returns a structure when you specify a single output argument and multiple outputs exist.

solve a system of equations to return the solutions in a structure array.

syms u v
eqns = [2*u   v == 0, u - v == 1];
s = solve(eqns,[u v])
s = struct with fields:
    u: 1/3
    v: -2/3

access the solutions by addressing the elements of the structure.

s.u
ans = 

13

s.v
ans = 

-23

using a structure array allows you to conveniently substitute solutions into other expressions.

use the subs function to substitute the solutions s into other expressions.

expr1 = u^2;
e1 = subs(expr1,s)
e1 = 

19

expr2 = 3*v   u;
e2 = subs(expr2,s)
e2 = 

-53

if solve returns an empty object, then no solutions exist.

eqns = [3*u 2, 3*u 1];
s = solve(eqns,u)
 
s =
 
empty sym: 0-by-1
 

the solve function can solve inequalities and return solutions that satisfy the inequalities. solve the following inequalities.

x>0

y>0

x2 y2 xy<1

set 'returnconditions' to true to return any parameters in the solution and conditions on the solution.

syms x y
eqn1 = x > 0;
eqn2 = y > 0;
eqn3 = x^2   y^2   x*y < 1;
eqns = [eqn1 eqn2 eqn3];
s = solve(eqns,[x y],'returnconditions',true);
s.x
ans = 

u-3v22-v2

s.y
ans = v
s.parameters
ans = (uv)
s.conditions
ans = 4v2<uu<40<v

the parameters u and v do not exist in matlab® workspace and must be accessed using s.parameters.

check if the values u = 7/2 and v = 1/2 satisfy the condition using subs and isalways.

condwithvalues = subs(s.conditions, s.parameters, [7/2,1/2]);
isalways(condwithvalues)
ans = logical
   1

isalways returns logical 1 (true) indicating that these values satisfy the condition. substitute these parameter values into s.x and s.y to find a solution for x and y.

xsol = subs(s.x, s.parameters, [7/2,1/2])
xsol = 

114-14

ysol = subs(s.y, s.parameters, [7/2,1/2])
ysol = 

12

solve the system of equations.

2u2 v2=0

u-v=1

when solving for more than one variable, the order in which you specify the variables defines the order in which the solver returns the solutions. assign the solutions to variables solv and solu by specifying the variables explicitly. the solver returns an array of solutions for each variable.

syms u v
eqns = [2*u^2   v^2 == 0, u - v == 1];
vars = [v u];
[solv, solu] = solve(eqns,vars)
solv = 

(-23-2i3-23 2i3)

solu = 

(13-2i313 2i3)

entries with the same index form the pair of solutions.

solutions = [solv solu]
solutions = 

(-23-2i313-2i3-23 2i313 2i3)

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)
solx = πk
parameters = k
conditions = kz

the solution πk contains the parameter k, where k must be an integer. the variable k does not exist in the matlab® workspace and must be accessed using parameters.

restrict the solution to 0<x<2π. 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)
solk = 1
valx = subs(solx,parameters,solk)
valx = π

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)
ans = logical
   1

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)
valx = 4π
vpa(valx)
ans = 12.566370614359172953850573533118

solve the equation exp(log(x)log(3x))=4.

by default, solve does not apply simplifications that are not valid for all values of x. in this case, the solver does not assume that x is a positive real number, so it does not apply the logarithmic identity log(3x)=log(3) log(x). as a result, solve cannot solve the equation symbolically.

syms x
eqn = exp(log(x)*log(3*x)) == 4;
s = solve(eqn,x)
warning: unable to solve symbolically. returning a numeric solution using .
s = -14.009379055223370038369334703094-2.9255310052111119036668717988769i

set 'ignoreanalyticconstraints' to true to apply simplification rules that might allow solve to find a solution. for details, see algorithms.

s = solve(eqn,x,'ignoreanalyticconstraints',true)
s = 

(3e-log(256) log(3)2233elog(256) log(3)223)

solve applies simplifications that allow the solver to find a solution. the mathematical rules applied when performing simplifications are not always valid in general. in this example, the solver applies logarithmic identities with the assumption that x is a positive real number. therefore, the solutions found in this mode should be verified.

the sym and syms functions let you set assumptions for symbolic variables.

assume that the variable x is positive.

syms x positive

when you solve an equation for a variable under assumptions, the solver only returns solutions consistent with the assumptions. solve this equation for x.

eqn = x^2   5*x - 6 == 0;
s = solve(eqn,x)
s = 1

allow solutions that do not satisfy the assumptions by setting 'ignoreproperties' to true.

s = solve(eqn,x,'ignoreproperties',true)
s = 

(-61)

for further computations, clear the assumption that you set on the variable x by recreating it using syms.

syms x

when you solve a polynomial equation, the solver might use root to return the solutions. solve a third-degree polynomial.

syms x a
eqn = x^3   x^2   a == 0;
solve(eqn, x)
ans = 

(root(z3 z2 a,z,1)root(z3 z2 a,z,2)root(z3 z2 a,z,3))

try to get an explicit solution for such equations by calling the solver with 'maxdegree'. the option specifies the maximum degree of polynomials for which the solver tries to return explicit solutions. the default value is 2. increasing this value, you can get explicit solutions for higher order polynomials.

solve the same equations for explicit solutions by increasing the value of 'maxdegree' to 3.

s = solve(eqn, x, 'maxdegree', 3)
s = 

(19σ1 σ1-13-118σ1-σ12-13-319σ1-σ1i2-118σ1-σ12-13 319σ1-σ1i2)where  σ1=a2 1272-1729-a2-1271/3

solve the equation sin(x) cos(2x)=1.

instead of returning an infinite set of periodic solutions, the solver picks three solutions that it considers to be the most practical.

syms x
eqn = sin(x)   cos(2*x) == 1;
s = solve(eqn,x)
s = 

(0π65π6)

choose only one solution by setting 'principalvalue' to true.

s1 = solve(eqn,x,'principalvalue',true)
s1 = 0

input arguments

equation to solve, specified as a symbolic expression or symbolic equation. the relation operator defines symbolic equations. if eqn is a symbolic expression (without the right side), the solver assumes that the right side is 0, and solves the equation eqn == 0.

variable for which you solve an equation, specified as a symbolic variable. by default, solve uses the variable determined by .

system of equations, specified as symbolic expressions or symbolic equations. if any elements of eqns are symbolic expressions (without the right side), solve equates the element to 0.

variables for which you solve an equation or system of equations, specified as a symbolic vector or symbolic matrix. by default, solve uses the variables determined by .

the order in which you specify these variables defines the order in which the solver returns the solutions.

name-value arguments

example: 'real',true specifies that the solver returns real solutions.

flag for returning only real solutions, specified as the comma-separated pair consisting of 'real' and one of these values.

falsereturn all solutions.
truereturn only those solutions for which every subexpression of the original equation represents a real number. this option also assumes that all symbolic parameters of an equation represent real numbers.

see solve polynomial and return real solutions.

flag for returning parameters in solution and conditions under which the solution is true, specified as the comma-separated pair consisting of 'returnconditions' and one of these values.

falsedo not return parameterized solutions and the conditions under which the solution holds. the solve function replaces parameters with appropriate values.
truereturn the parameters in the solution and the conditions under which the solution holds. for a call with a single output variable, solve returns a structure with the fields parameters and conditions. for multiple output variables, solve assigns the parameters and conditions to the last two output variables. this behavior means that the number of output variables must be equal to the number of variables to solve for plus two.

see solve inequalities.

example: [v1, v2, params, conditions] = solve(sin(x) y == 0,y^2 == 3,'returnconditions',true) returns the parameters in params and conditions in conditions.

simplification rules applied to expressions and equations, specified as the comma-separated pair consisting of 'ignoreanalyticconstraints' and one of these values.

falseuse strict simplification rules.
trueapply purely algebraic simplifications to expressions and equations. setting ignoreanalyticconstraints to true can give you simpler solutions, which could lead to results not generally valid. in other words, this option applies mathematical identities that are convenient, but the results might not hold for all possible values of the variables. in some cases, it also enables solve to solve equations and systems that cannot be solved otherwise. for details, see algorithms.

see shorten result with simplification rules.

flag for returning solutions inconsistent with the properties of variables, specified as the comma-separated pair consisting of 'ignoreproperties' and one of these values.

falsedo not include solutions inconsistent with the properties of variables.
trueinclude solutions inconsistent with the properties of variables.

see ignore assumptions on variables.

maximum degree of polynomial equations for which solver uses explicit formulas, specified as a positive integer smaller than 5. the solver does not use explicit formulas that involve radicals when solving polynomial equations of a degree larger than the specified value.

see solve polynomial equations of high degree.

flag for returning one solution, specified as the comma-separated pair consisting of 'principalvalue' and one of these values.

falsereturn all solutions.
truereturn only one solution. if an equation or a system of equations does not have a solution, the solver returns an empty symbolic object.

see return one solution.

output arguments

solutions of an equation, returned as a symbolic array. the size of a symbolic array corresponds to the number of the solutions.

solutions of a system of equations, returned as a structure. the number of fields in the structure correspond to the number of independent variables in a system. if 'returnconditions' is set to true, the solve function returns two additional fields that contain the parameters in the solution, and the conditions under which the solution is true.

solutions of a system of equations, returned as symbolic variables. the number of output variables or symbolic arrays must be equal to the number of independent variables in a system. if you explicitly specify independent variables vars, then the solver uses the same order to return the solutions. if you do not specify vars, the toolbox sorts independent variables alphabetically, and then assigns the solutions for these variables to the output variables.

parameters in a solution, returned as a vector of generated parameters. this output argument is only returned if returnconditions is true. if a single output argument is provided, parameters is returned as a field of a structure. if multiple output arguments are provided, parameters is returned as the second-to-last output argument. the generated parameters do not appear in the matlab® workspace. they must be accessed using parameters.

example: [solx, params, conditions] = solve(sin(x) == 0, 'returnconditions', true) returns the parameter k in the argument params.

conditions under which solutions are valid, returned as a vector of symbolic expressions. this output argument is only returned if returnconditions is true. if a single output argument is provided, conditions is returned as a field of a structure. if multiple output arguments are provided, conditions is returned as the last output argument.

example: [solx, params, conditions] = solve(sin(x) == 0, 'returnconditions', true) returns the condition in(k, 'integer') in conditions. the solution in solx is valid only under this condition.

tips

  • if solve cannot find a solution and returnconditions is false, the solve function internally calls the numeric solver vpasolve that tries to find a numeric solution. for polynomial equations and systems without symbolic parameters, the numeric solver returns all solutions. for nonpolynomial equations and systems without symbolic parameters, the numeric solver returns only one solution (if a solution exists).

  • if solve cannot find a solution and returnconditions is true, solve returns an empty solution with a warning. if no solutions exist, solve returns an empty solution without a warning.

  • if the solution contains parameters and returnconditions is true, solve returns the parameters in the solution and the conditions under which the solutions are true. if returnconditions is false, the solve function either chooses values of the parameters and returns the corresponding results, or returns parameterized solutions without choosing particular values. in the latter case, solve also issues a warning indicating the values of parameters in the returned solutions.

  • if a parameter does not appear in any condition, it means the parameter can take any complex value.

  • the output of solve can contain parameters from the input equations in addition to parameters introduced by solve.

  • parameters introduced by solve do not appear in the matlab workspace. they must be accessed using the output argument that contains them. alternatively, to use the parameters in the matlab workspace use syms to initialize the parameter. for example, if the parameter is k, use syms k.

  • the variable names parameters and conditions are not allowed as inputs to solve.

  • to solve differential equations, use the function.

  • when solving a system of equations, always assign the result to output arguments. output arguments let you access the values of the solutions of a system.

  • maxdegree only accepts positive integers smaller than 5 because, in general, there are no explicit expressions for the roots of polynomials of degrees higher than 4.

  • the output variables y1,...,yn do not specify the variables for which solve solves equations or systems. if y1,...,yn are the variables that appear in eqns, then there is no guarantee that solve(eqns) will assign the solutions to y1,...,yn using the correct order. thus, when you run [b,a] = solve(eqns), you might get the solutions for a assigned to b and vice versa.

    to ensure the order of the returned solutions, specify the variables vars. for example, the call [b,a] = solve(eqns,b,a) assigns the solutions for a to a and the solutions for b to b.

algorithms

when you use ignoreanalyticconstraints, the solver applies some of these rules to the expressions on both sides of an equation.

  • log(a) log(b) = log(a·b) for all values of a and b. in particular, the following equality is valid for all values of a, b, and c:

      (a·b)c = ac·bc.

  • log(ab) = b·log(a) for all values of a and b. in particular, the following equality is valid for all values of a, b, and c:

      (ab)c = ab·c.

  • if f and g are standard mathematical functions and f(g(x)) = x for all small positive numbers, f(g(x)) = x is assumed to be valid for all complex values x. in particular:

    • log(ex) = x

    • asin(sin(x)) = x, acos(cos(x)) = x, atan(tan(x)) = x

    • asinh(sinh(x)) = x, acosh(cosh(x)) = x, atanh(tanh(x)) = x

    • wk(x·ex) = x for all branch indices k of the lambert w function.

  • the solver can multiply both sides of an equation by any expression except 0.

  • the solutions of polynomial equations must be complete.

version history

introduced before r2006a

see also

functions

  • | | | | | |

live editor tasks

topics

    external websites

      网站地图