Solve symbolic equations numerically
collapse all in page
Syntax
S = vpasolve(eqn,var)
S = vpasolve(eqn,var,init_param)
Y = vpasolve(eqns,vars)
Y = vpasolve(eqns,vars,init_param)
[y1,...,yN] = vpasolve(eqns,vars)
[y1,...,yN] = vpasolve(eqns,vars,init_param)
___ = vpasolve(___,'Random',true)
Description
example
S = vpasolve(eqn,var)
numerically solves the equation eqn
for the variable var
. If you do not specify var
, vpasolve
solves for the default variable determined by symvar. For example, vpasolve(x + 1 == 2, x)
numerically solves the equation x+1=2 for x.
By default, vpasolve
finds the solutions to 32 significant digits. To change the number of significant digits, use the digits
function.
example
S = vpasolve(eqn,var,init_param)
numerically solves the equation eqn
for the variable var
using the initial guess or search range init_param
.
example
Y = vpasolve(eqns,vars)
numerically solves the system of equations eqns
for the variables vars
. This syntax returns a structure array Y
that contains the solutions. The fields in the structure array correspond to the variables specified by vars
. If you do not specify vars
, vpasolve
solves for the default variables determined by symvar.
Y = vpasolve(eqns,vars,init_param)
numerically solves the system of equations eqns
for the variables vars
using the initial guess or search range init_param
.
example
[y1,...,yN] = vpasolve(eqns,vars)
numerically solves the system of equations eqns
for the variables vars
. This syntax assigns the solutions to the variables y1,...,yN
. If you do not specify vars
, vpasolve
solves for the default variables determined by symvar.
example
[y1,...,yN] = vpasolve(eqns,vars,init_param)
numerically solves the system of equations eqns
for the variables vars
using the initial guess or search range init_param
.
example
___ = vpasolve(___,
uses a random initial guess for finding solutions. Use this input to avoid returning the same solution repeatedly for nonpolynomial equations. If you specify initial guesses for all variables, setting 'Random'
,true)'Random'
to true
has no effect.
Examples
collapse all
Solve Polynomial and Nonpolynomial Equations
Open Live Script
Solve a polynomial equation. For polynomial equations, vpasolve
returns all solutions.
syms xS = vpasolve(2*x^4 + 3*x^3 - 4*x^2 - 3*x + 2 == 0, x)
S =
Solve a nonpolynomial equation. For nonpolynomial equations, vpasolve
returns the first solution that it finds.
S = vpasolve(sin(x) == 1/2, x)
S =
Find Multiple Solutions by Specifying Initial Guesses
Open Live Script
Find multiple solutions of the equation by specifying the initial guesses when using vpasolve
.
Plot the left and right sides of the equation.
syms xeqnLeft = 200*sin(x);eqnRight = x^3 - 1;fplot([eqnLeft eqnRight])title([texlabel(eqnLeft) ' = ' texlabel(eqnRight)])
The plot shows that the equation has three solutions. If you do not specify the initial guess, vpasolve
returns the first solution that it finds.
S1 = vpasolve(eqnLeft == eqnRight, x)
S1 =
Find one of the other solutions by specifying an initial guess that is close to that solution.
S2 = vpasolve(eqnLeft == eqnRight, x, -3)
S2 =
S3 = vpasolve(eqnLeft == eqnRight, x, 4)
S3 =
Solve System of Equations
Open Live Script
Solve a system of equations. Use one output argument to return the solutions in the form of a structure array.
syms u vY = vpasolve([v^3 + 2*u == v, v^2 == u], [u,v])
Y = struct with fields: u: [3x1 sym] v: [3x1 sym]
Display the solutions by accessing the fields of the structure array Y
.
uSol = Y.u
uSol =
vSol = Y.v
vSol =
If vpasolve
cannot find a solution, it returns an empty object.
syms xeqns = [3*x+2, 3*x+1];Y = vpasolve(eqns,x)
Y = Empty sym: 0-by-1
Assign Solutions to Variables When Solving System of Equations
Open Live Script
When solving a system of equations, use multiple output arguments to assign the solutions directly to output variables. The order in which the solver returns the solutions follows the order in which you specify the variables.
syms x y[sol_x, sol_y] = vpasolve([x*sin(10*x) == y^3, y^2 == exp(-2*x/3)], [x,y])
sol_x =
sol_y =
Specify Ranges of Solutions
Open Live Script
You can specify ranges of solutions of an equation. For example, if you want to restrict your search to only real solutions, you cannot use assumptions because vpasolve
ignores assumptions. Instead, specify a search interval. For the following equation, if you do not specify ranges, the numeric solver returns all six solutions of the equation.
syms xS = vpasolve(x^6 - x^2 == 3, x)
S =
Suppose you need only real solutions of this equation. You cannot use assumptions on variables because vpasolve
ignores them.
assume(x,'real')S = vpasolve(x^6 - x^2 == 3, x)
S =
Specify the search range to restrict the returned results to particular ranges. For example, to return only real solutions of this equation, specify the search interval as [-Inf Inf]
.
S = vpasolve(x^6 - x^2 == 3, x, [-Inf Inf])
S =
To return nonnegative solutions, specify the search interval as [0 Inf]
.
S = vpasolve(x^6 - x^2 == 3, x, [0 Inf])
The search range can also contain complex numbers, such as [-1, 1+2i]
. In this case, vpasolve uses a rectangular search area in the complex plane where -1
specifies the bottom-left corner of the search area and 1+2i
specifies the top-right corner of that area.
S = vpasolve(x^6 - x^2 == 3, x, [-1 1+2i])
S =
Provide Initial Guess to Find Solutions
Open Live Script
Find the solution for the following system of equations.
syms x yeqn1 = exp(-x^2-y^2)*(x-4) - exp((-x^2-y^2)/2)*(x-2) == 0
eqn1 =
eqn2 = exp(-x^2-y^2)*(y-2) - exp((-x^2-y^2)/2)*(y-4) == 0
eqn2 =
Find the solution for the variables x
and y
without specifying initial guess. vpasolve
cannot find a solution and it returns an empty object.
[solX, solY] = vpasolve([eqn1 eqn2],[x y])
solX = Empty sym: 0-by-1 solY = Empty sym: 0-by-1
Now specify the initial guesses x = 2
and y = 4
. vpasolve
returns the solutions that are close to the initial guesses.
[solX, solY] = vpasolve([eqn1 eqn2],[x y],[2; 4])
solX =
solY =
Find Multiple Solutions for Nonpolynomial Equation
Open Live Script
By default, vpasolve
returns the same solution on every call. To find more than one solution for nonpolynomial equations, set 'Random'
to true
. This makes vpasolve
use a random initial guess which can lead to different solutions on successive calls.
If 'Random'
is not specified, vpasolve returns the same solution on every call.
syms xf = x-tan(x);for n = 1:3 S = vpasolve(f,x)end
S =
S =
S =
When 'Random'
is set to true, vpasolve returns a distinct solution on every call.
for n = 1:3 S = vpasolve(f,x,'Random',true)end
S =
S =
S =
The 'Random'
option can be used in conjunction with a search range.
S = vpasolve(f,x,[10 12],'Random',true)
S =
Input Arguments
collapse all
eqn
— Equation to solve
symbolic equation | symbolic expression
Equation to solve, specified as a symbolic equation or symbolic expression. A symbolic equation is defined by the relation operator ==. 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
.
var
— Variable to solve equation for
symbolic variable
Variable to solve equation for, specified as a symbolic variable. If var
is not specified, symvar determines the variables.
eqns
— System of equations or expressions to solve
symbolic vector | symbolic matrix | symbolic array
System of equations or expressions to solve, specified as a symbolic vector, matrix, or array of equations or expressions. These equations or expressions can also be separated by commas. If an equation is a symbolic expression (without the right side), the solver assumes that the right side of the equation is 0.
vars
— Variables to solve system of equations for
symbolic vector | symbolic matrix
Variables to solve system of equations for, specified as a symbolic vector or symbolic matrix. These variables are specified as a vector or comma-separated list. If vars
is not specified, symvar determines the variables.
init_param
— Initial guess or search range for solution
numeric value | vector | matrix with two columns
Initial guess or search range for a solution, specified as a numeric value, vector, or matrix with two columns.
If
init_param
is a number or, in the case of multivariate equations, a vector of numbers, then the numeric solver uses it as an initial guess. Ifinit_param
is specified as a scalar while the system of equations is multivariate, then the numeric solver uses the scalar value as an initial guess for all variables. For an example, see Find Multiple Solutions by Specifying Initial Guesses.If
init_param
is a matrix with two columns, then the two entries of the rows specify the bounds of an initial guess for the corresponding variables. To specify an initial guess in a matrix of search ranges, specify both columns as the initial guess value.If you specify
init_param
as a search range[a b]
and the valuesa,b
are complex numbers, thenvpasolve
searches for the solutions in the rectangular search area in the complex plane. Here,a
specifies the bottom-left corner of the rectangular search area, andb
specifies the top-right corner of that area. For an example, see Specify Ranges of Solutions.To omit a search range for a variable, set the search range for that variable to
[NaN, NaN]
ininit_param
. All other uses ofNaN
ininit_param
will error.
Output Arguments
collapse all
S
— Solutions of univariate equation
symbolic value | symbolic array
Solutions of univariate equation, returned as symbolic value or symbolic array. The size of a symbolic array corresponds to the number of the solutions.
Y
— Solutions of system of equations
structure array
Solutions of system of equations, returned as a structure array. The number of fields in the structure array corresponds to the number of variables to be solved for.
y1,...,yN
— Variables that are assigned solutions of system of equations
array of numeric variables | array of symbolic variables
Variables that are assigned solutions of system of equations, returned as an array of numeric or symbolic variables. The number of output variables or symbolic arrays must equal the number of variables to be solved for. 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 or symbolic arrays.
Tips
If
vpasolve
cannot find a solution, it returns an empty object. Provide initial guess to help the solver finding a solution. For an example, see Provide Initial Guess to Find Solutions.For polynomial equations,
vpasolve
returns all solutions. For nonpolynomial equations, there is no general method of finding all solutions andvpasolve
returns only one solution by default. To find several different solutions for nonpolynomial, you can set'Random'
to true and usevpasolve
repeatedly.When you solve a system of equations with nonunique solutions, the behavior of
vpasolve
depends on whether the system is polynomial or nonpolynomial. If polynomial,vpasolve
returns all solutions by introducing an arbitrary parameter. If nonpolynomial, a single numerical solution is returned, if it exists.When you solve a system of rational equations,
vpasolve
transforms the rational equations to polynomials by multiplying out the denominators.vpasolve
returns all solutions of the resulting polynomial system, which also include the roots of the denominators.vpasolve
ignores assumptions set on variables. You can restrict the returned results to particular ranges by specifying appropriate search ranges using the argument init_param. However, if the equations or expressions have other variables besides the variables to solve for as specified byvars
, thenvpasolve
returns the solutions forvars
in terms of the other variables that are complex in general.The output variables y1,...,yN do not specify the variables for which
vpasolve
solves equations or systems. Ify1,...,yN
are the variables that appear in eqns, that does not guarantee thatvpasolve(eqns)
will assign the solutions toy1,...,yN
using the correct order. Thus, for the call[a,b] = vpasolve(eqns)
, you might get the solutions fora
assigned tob
and vice versa. To ensure the order of the returned solutions, specify the variables vars. For example, the call[b,a] = vpasolve(eqns,[b,a])
assigns the solutions fora
assigned toa
and the solutions forb
assigned tob
.You can solve equations symbolically using
solve
, and then numerically approximate the results usingvpa
. Using this approach, you get numeric approximations of all solutions found by the symbolic solver. However, this can reduce computational speed since solving symbolically and postprocessing the results take more time than directly using the numeric solvervpasolve
.
Algorithms
When you set
'Random'
totrue
and specify a search range for a variable, random initial guesses within the search range are chosen using the internal random number generator (with uniform distribution).When you set
'Random'
totrue
and do not specify a search range for a variable, random initial guesses are generated using a Cauchy distribution with a half-width of100
. This means the initial guesses are real valued and have a large spread of values on repeated calls.
Version History
Introduced in R2012b
expand all
R2021b: vpasolve(eqns,vars)
returns all solutions for the symbolic matrix vars
If vars
is declared as a symbolic matrix, vpasolve(
now returns all solutions for eqns
,vars
)vars
.
For example, find the solutions of four equations with four unknowns. Declare the four unknowns as a symbolic matrix. vpasolve(
now returns all solutions for the unknowns.eqns
,vars
)
syms a b c deqn1 = b + c + d == 50;eqn2 = a + c + d == 65;eqn3 = a + b + d == 70;eqn4 = a + b + c == 65;vars = [a b; c d];sol = vpasolve([eqn1 eqn2 eqn3 eqn4],vars)
sol = struct with fields: a: 33.333333333333333333333333333333 c: 13.333333333333333333333333333333 b: 18.333333333333333333333333333333 d: 18.333333333333333333333333333333
In previous releases, if vars
is a symbolic matrix, then vpasolve(
treats eqns
,vars
)vars
as a column vector and it only returns the solutions for the first column of vars
. For example, find the solutions for the same equations with the unknowns declared as a symbolic matrix.
syms a b c deqn1 = b + c + d == 50;eqn2 = a + c + d == 65;eqn3 = a + b + d == 70;eqn4 = a + b + c == 65;vars = [a b; c d];sol = vpasolve([eqn1 eqn2 eqn3 eqn4],vars)
sol = struct with fields: a: [1×1 sym] c: [1×1 sym]
See Also
dsolve | equationsToMatrix | fzero | isolate | linsolve | solve | symvar | vpa | digits
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office