Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Solve equations numerically

`S = vpasolve(eqn)`

`S = vpasolve(eqn,var)`

`S = vpasolve(eqn,var,init_guess)`

`Y = vpasolve(eqns)`

`Y = vpasolve(eqns,vars)`

`Y = vpasolve(eqns,vars,init_guess)`

`[y1,...,yN] = vpasolve(eqns)`

`[y1,...,yN] = vpasolve(eqns,vars)`

`[y1,...,yN] = vpasolve(eqns,vars,init_guess)`

`___ = vpasolve(___,'Random',true)`

numerically
solves the system of equations `Y`

= vpasolve(`eqns`

,`vars`

,`init_guess`

)`eqns`

for the variables `vars`

using
the starting values or the search range `init_guess`

.

`[`

numerically
solves the system of equations `y1,...,yN`

] = vpasolve(`eqns`

,`vars`

,`init_guess`

)`eqns`

for the variables
specified by `vars`

using the starting values or
the search range `init_guess`

.

`___ = vpasolve(___,`

uses a random starting point for finding solutions. Use this input to avoid returning the
same solution repeatedly for nonpolynomial equations. If you specify starting points for
all variables, setting `'Random'`

,true)`'Random'`

to `true`

has no
effect.

For polynomial equations, `vpasolve`

returns
all solutions:

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

ans = - 0.88011377126068169817875190457835 + 0.76331583387715452512978468102263i - 0.88011377126068169817875190457835 - 0.76331583387715452512978468102263i 0.50511377126068169817875190457835 + 0.81598965068946312853227067890656i 0.50511377126068169817875190457835 - 0.81598965068946312853227067890656i

If `vpasolve`

returns an empty object, then no solution was
found.

eqns = [3*x+2, 3*x+1]; vpasolve(eqns, x)

ans = Empty sym: 0-by-1

For nonpolynomial equations, `vpasolve`

returns
the first solution that it finds:

syms x vpasolve(sin(x^2) == 1/2, x)

ans = -226.94447241941511682716953887638

When solving a system of equations, use one output argument to return the solutions in the form of a structure array:

syms x y S = vpasolve([x^3 + 2*x == y, y^2 == x], [x, y])

S = struct with fields: x: [6×1 sym] y: [6×1 sym]

Display solutions by accessing the elements of the structure
array `S`

:

S.x

ans = 0.2365742942773341617614871521768 0 - 0.28124065338711968666197895499453 + 1.2348724236470142074859894531946i - 0.28124065338711968666197895499453 - 1.2348724236470142074859894531946i 0.16295350624845260578123537890613 - 1.6151544650555366917886585417926i 0.16295350624845260578123537890613 + 1.6151544650555366917886585417926i

S.y

ans = 0.48638903593454300001655725369801 0 0.70187356885586188630668751791218 + 0.87969719792982402287026727381769i 0.70187356885586188630668751791218 - 0.87969719792982402287026727381769i - 0.94506808682313338631496614476119 + 0.85451751443904587692179191887616i - 0.94506808682313338631496614476119 - 0.85451751443904587692179191887616i

When solving a system of equations, use multiple output arguments to assign the solutions directly to output variables. To ensure the correct order of the returned solutions, specify the variables explicitly. The order in which you specify the variables defines the order in which the solver returns the solutions.

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

Plot the two sides of the equation, and then use the plot to specify initial guesses for the solutions.

Plot the left and right sides of the equation $$200\mathrm{sin}(x)={x}^{3}-1$$.

syms x eqnLeft = 200*sin(x); eqnRight = x^3 - 1; fplot([eqnLeft eqnRight]) title([texlabel(eqnLeft) ' = ' texlabel(eqnRight)])

This equation has three solutions. If you do not specify the
initial guess (zero-approximation), `vpasolve`

returns
the first solution that it finds:

vpasolve(200*sin(x) == x^3 - 1, x)

ans = -0.0050000214585835715725440675982988

Find one of the other solutions by specifying the initial point that is close to that solution:

vpasolve(200*sin(x) == x^3 - 1, x, -4)

ans = -3.0009954677086430679926572924945

vpasolve(200*sin(x) == x^3 - 1, x, 3)

ans = 3.0098746383859522384063444361906

You can specify ranges for 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 eight solutions of the equation:

syms x vpasolve(x^8 - x^2 == 3, x)

ans = -1.2052497163799060695888397264341 1.2052497163799060695888397264341 - 0.77061431370803029127495426747428 + 0.85915207603993818859321142757163i - 0.77061431370803029127495426747428 - 0.85915207603993818859321142757164i -1.0789046020338265308047436284205i 1.0789046020338265308047436284205i 0.77061431370803029127495426747428 + 0.85915207603993818859321142757164i 0.77061431370803029127495426747428 - 0.85915207603993818859321142757163i

Suppose you need only real solutions of this equation. You cannot
use assumptions on variables because `vpasolve`

ignores
them.

assume(x, 'real') vpasolve(x^8 - x^2 == 3, x)

ans = -1.2052497163799060695888397264341 1.2052497163799060695888397264341 - 0.77061431370803029127495426747428 + 0.85915207603993818859321142757163i - 0.77061431370803029127495426747428 - 0.85915207603993818859321142757164i -1.0789046020338265308047436284205i 1.0789046020338265308047436284205i 0.77061431370803029127495426747428 + 0.85915207603993818859321142757164i 0.77061431370803029127495426747428 - 0.85915207603993818859321142757163i

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]`

:

vpasolve(x^8 - x^2 == 3, x, [-Inf Inf])

ans = -1.2052497163799060695888397264341 1.2052497163799060695888397264341

Return only nonnegative solutions:

vpasolve(x^8 - x^2 == 3, x, [0 Inf])

ans = 1.2052497163799060695888397264341

The search range can contain complex numbers. In this case, `vpasolve`

uses
a rectangular search area in the complex plane:

vpasolve(x^8 - x^2 == 3, x, [-1, 1 + i])

ans = - 0.77061431370803029127495426747428 + 0.85915207603993818859321142757164i 0.77061431370803029127495426747428 + 0.85915207603993818859321142757164i

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 starting value 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 vpasolve(f,x) end

ans = 0 ans = 0 ans = 0

When `'Random'`

is set to `true`

,
`vpasolve`

returns a distinct solution on every call.

syms x f = x-tan(x); for n = 1:3 vpasolve(f,x,'Random',true) end

ans = -227.76107684764829218924973598808 ans = 102.09196646490764333652956578441 ans = 61.244730260374400372753016364097

`random`

can be used in conjunction with a
search range:

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

ans = 10.904121659428899827148702790189

If

`vpasolve`

returns an empty object, then no solution was found.`vpasolve`

returns all solutions only for polynomial equations. For nonpolynomial equations, there is no general method of finding all solutions. When you look for numerical solutions of a nonpolynomial equation or system that has several solutions, then, by default,`vpasolve`

returns only one solution, if any. To find more than just one solution, set`'Random'`

to true. Now, calling`vpasolve`

repeatedly might return several different solutions.When you solve a system where there are not enough equations to determine all variables uniquely, the behavior of

`vpasolve`

behavior 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, the toolbox transforms it to a polynomial system by multiplying out the denominators.

`vpasolve`

returns all solutions of the resulting polynomial system, including those that are also roots of these 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_guess`

.If

`init_guess`

specifies 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.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`

.Place equations and expressions to the left of the argument list, and the variables to the right.

`vpasolve`

checks for variables starting on the right, and on reaching the first equation or expression, assumes everything to the left is an equation or expression.If possible, solve equations symbolically using

`solve`

, and then approximate the obtained symbolic results numerically using`vpa`

. Using this approach, you get numeric approximations of all solutions found by the symbolic solver. Using the symbolic solver and postprocessing its results requires more time than using the numeric methods directly. This can significantly decrease performance.

When you set

`'Random'`

to`true`

and specify a search range for a variable, random starting points within the search range are chosen using the internal random number generator. The distribution of starting points within finite search ranges is uniform.When you set

`random`

to`true`

and do not specify a search range for a variable, random starting points are generated using a Cauchy distribution with a half-width of`100`

. This means the starting points are real valued and have a large spread of values on repeated calls.