vpasolve

Solve equations numerically

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.

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(___,'Random',true) 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' to true has no effect.

Examples

collapse all

Solve a polynomial equation. For polynomial equations, vpasolve returns all solutions.

syms x
S = vpasolve(2*x^4 + 3*x^3 - 4*x^2 - 3*x + 2 == 0, x)
S = 

(-2.0-1.00.51.0)

Solve a nonpolynomial equation. For nonpolynomial equations, vpasolve returns the first solution that it finds.

S = vpasolve(sin(x) == 1/2, x)
S = 0.52359877559829887307710723054658

Find multiple solutions of the equation 200sin(x)=x3-1 by specifying the initial guesses when using vpasolve.

Plot the left and right sides of the equation.

syms x
eqnLeft = 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 = -0.0050000214585835715725440675982988

Find one of the other solutions by specifying an initial guess that is close to that solution.

S2 = vpasolve(eqnLeft == eqnRight, x, -3)
S2 = -3.0009954677086430679926572924945
S3 = vpasolve(eqnLeft == eqnRight, x, 4)
S3 = 3.0098746383859522384063444361906

Solve a system of equations. Use one output argument to return the solutions in the form of a structure array.

syms u v
Y = 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 S.

uSol = Y.u
uSol = 

(05.82842712474619009760337744841940.1715728752538099023966225515806)

vSol = Y.v
vSol = 

(0-2.41421356237309504880168872420970.4142135623730950488016887242097)

If vpasolve cannot find a solution, it returns an empty object.

syms x
eqns = [3*x+2, 3*x+1];
Y = vpasolve(eqns,x)
 
Y =
 
Empty sym: 0-by-1
 

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 = 88.90707209659114864849280774681
sol_y = 0.00000000000013470479710676694388973703681918

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 x
S = vpasolve(x^6 - x^2 == 3, x)
S = 

(-1.29294233500847243691965504363821.2929423350084724369196550436382-0.50188125716943915856832436499602-1.0429452224956770037495194222175i-0.50188125716943915856832436499602+1.0429452224956770037495194222175i0.50188125716943915856832436499602-1.0429452224956770037495194222175i0.50188125716943915856832436499602+1.0429452224956770037495194222175i)

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 = 

(-1.29294233500847243691965504363821.2929423350084724369196550436382-0.50188125716943915856832436499602-1.0429452224956770037495194222175i-0.50188125716943915856832436499602+1.0429452224956770037495194222175i0.50188125716943915856832436499602-1.0429452224956770037495194222175i0.50188125716943915856832436499602+1.0429452224956770037495194222175i)

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 = 

(-1.29294233500847243691965504363821.2929423350084724369196550436382)

To return nonnegative solutions, specify the search interval as [0 Inf].

S = vpasolve(x^6 - x^2 == 3, x, [0 Inf])
S = 1.2929423350084724369196550436382

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 = 

(-0.50188125716943915856832436499602+1.0429452224956770037495194222175i0.50188125716943915856832436499602+1.0429452224956770037495194222175i)

Find the solution for the following system of equations.

syms x y
eqn1 = exp(-x^2-y^2)*(x-4) - exp((-x^2-y^2)/2)*(x-2) == 0
eqn1 = 

e-x2-y2x-4-e-x22-y22x-2=0

eqn2 = exp(-x^2-y^2)*(y-2) - exp((-x^2-y^2)/2)*(y-4) == 0
eqn2 = 

e-x2-y2y-2-e-x22-y22y-4=0

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 = 1.9999092125057125429174334656647
solY = 4.0000907874942874570825665343353

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 x
f = x-tan(x);
for n = 1:3
    S = vpasolve(f,x)
end
S = 0
S = 0
S = 0

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 = -227.76107684764829218924973598808
S = 102.09196646490764333652956578441
S = 61.244730260374400372753016364097

The 'Random' option can be used in conjunction with a search range.

S = vpasolve(f,x,[10 12],'Random',true)
S = 10.904121659428899827148702790189

Input Arguments

collapse all

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.

Variable to solve equation for, specified as a symbolic variable. If var is not specified, symvar determines the variables.

System of equations or expressions to be 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.

Variables to solve system of equations for, specified as a symbolic vector. These variables are specified as a vector or comma-separated list. If vars is not specified, symvar determines the variables.

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. If init_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 values a,b are complex numbers, then vpasolve 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, and b 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] in init_param. All other uses of NaN in init_param will error.

Output Arguments

collapse all

Solutions of univariate equation, returned as symbolic value or symbolic array. The size of a symbolic array corresponds to the number of the solutions.

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.

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 and vpasolve returns only one solution by default. To find several different solutions for nonpolynomial, you can set 'Random' to true and use vpasolve 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.

  • The output variables y1,...,yN do not specify the variables for which vpasolve solves equations or systems. If y1,...,yN are the variables that appear in eqns, that does not guarantee that vpasolve(eqns) will assign the solutions to y1,...,yN using the correct order. Thus, for the call [a,b] = vpasolve(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] = vpasolve(eqns,[b,a]) assigns the solutions for a assigned to a and the solutions for b assigned to b.

  • You can solve equations symbolically using solve, and then numerically approximate the results using vpa. 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 solver vpasolve.

Algorithms

  • When you set 'Random' to true 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' to true and do not specify a search range for a variable, random initial guesses are generated using a Cauchy distribution with a half-width of 100. This means the initial guesses are real valued and have a large spread of values on repeated calls.

Introduced in R2012b