Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# vpasolve

Numeric solver

## Syntax

S = vpasolve(eqn,var,init_guess)
Y = vpasolve(eqns,vars,init_guess)
[y1,...,yN] = vpasolve(eqns,vars,init_guess)

## Description

S = vpasolve(eqn,var,init_guess) numerically solves the equation eqn for the variable var using the starting value or the search range init_guess.

Y = vpasolve(eqns,vars,init_guess) numerically solves the system of equations eqns for the variables vars using the starting values or the search range init_guess. This syntax returns a structure array that contains the solutions. The number of fields in the structure array corresponds to the number of independent variables in a system.

[y1,...,yN] = vpasolve(eqns,vars,init_guess) numerically solves the system of equations eqns for the variables vars using the starting values or the search range init_guess. This syntax assigns the solutions to the variables y1,...,yN.

## Input Arguments

 eqn Symbolic equation defined by the relation operator == or symbolic expression. 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 for which you solve an equation. Default: Variable determined by symvar eqns Symbolic equations or expressions that need to be solved as a system. These equations or expressions can be separated by commas or can be presented as a vector. If an equation is a symbolic expression (without the right side), the solver assumes that the right side of that equation is 0. vars Variables for which you solve an equation or a system of equations. These variables can be separated by commas or can be presented as a vector. Default: Variables determined by symvar init_guess Number, vector, or matrix with two columns that specifies the initial guess for the solution. If init_guess is a number or, in case of multivariate equations, a vector of numbers, then the numeric solver uses it as a starting point. If init_guess is a matrix with two columns, then the numeric solver uses init_guess as a search range. Also, if init_guess is a vector with two elements, and eqn is univariate, then the numeric solver uses init_guess as a search range. Default: vpasolve uses its own internal choices for starting points and search ranges.

## Output Arguments

 S Symbolic array that contains solutions of an equation when you solve one equation. The size of a symbolic array corresponds to the number of the solutions. Y Structure array that contains solutions of a system when you solve a system of equations. The number of fields in the structure array corresponds to the number of independent variables in a system. y1,...,yN Variables to which the solver assigns the solutions of a system of equations. The number of output variables or symbolic arrays must equal 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 or symbolic arrays.

## Examples

Solve this polynomial equation numerically. 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.76331583387715452512978468102263*i
0.50511377126068169817875190457835 + 0.81598965068946312853227067890656*i
0.50511377126068169817875190457835 - 0.81598965068946312853227067890656*i
- 0.88011377126068169817875190457835 + 0.76331583387715452512978468102263*i```

Solve this equation numerically. For non-polynomial 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 =
x: [6x1 sym]
y: [6x1 sym]```

To display the solutions, access the elements of the structure array S:

`S.x`
```ans =
0
0.2365742942773341617614871521768
- 0.28124065338711968666197895499453 + 1.2348724236470142074859894531946*i
0.16295350624845260578123537890613 + 1.6151544650555366917886585417926*i
0.16295350624845260578123537890613 - 1.6151544650555366917886585417926*i
- 0.28124065338711968666197895499453 - 1.2348724236470142074859894531946*i```
`S.y`
```ans =
0
0.48638903593454300001655725369801
0.70187356885586188630668751791218 + 0.87969719792982402287026727381769*i
- 0.94506808682313338631496614476119 - 0.85451751443904587692179191887616*i
- 0.94506808682313338631496614476119 + 0.85451751443904587692179191887616*i
0.70187356885586188630668751791218 - 0.87969719792982402287026727381769*i```

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 left and right sides of the equation 200*sin(x) = x^3 - 1:

```syms x
ezplot(200*sin(x))
hold on
ezplot(x^3 - 1)
title('200*sin(x) = x^3 - 1')```

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

To find one of the other solutions, specify 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```

Solve this polynomial equation. The numeric solver returns all eight solutions of this equation:

```syms x
vpasolve(x^8 - x^2 == 3, x)```
```ans =
-1.2052497163799060695888397264341
1.2052497163799060695888397264341
- 0.77061431370803029127495426747428 + 0.85915207603993818859321142757164*i
- 0.77061431370803029127495426747428 - 0.85915207603993818859321142757164*i
1.0789046020338265308047436284205*i
-1.0789046020338265308047436284205*i
0.77061431370803029127495426747428 + 0.85915207603993818859321142757164*i
0.77061431370803029127495426747428 - 0.85915207603993818859321142757164*i```

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.85915207603993818859321142757164*i
- 0.77061431370803029127495426747428 - 0.85915207603993818859321142757164*i
1.0789046020338265308047436284205*i
-1.0789046020338265308047436284205*i
0.77061431370803029127495426747428 + 0.85915207603993818859321142757164*i
0.77061431370803029127495426747428 - 0.85915207603993818859321142757164*i```

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

Now 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.85915207603993818859321142757164*i
0.77061431370803029127495426747428 + 0.85915207603993818859321142757164*i```

## Alternatives

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.

expand all

### Tips

• vpasolve returns all solutions only for polynomial equations. For non-polynomial equations, there is no general method of finding all solutions numerically. When you solve a non-polynomial equation or system numerically, and the solutions exist, vpasolve returns only one solution.

• 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.

• If vars is a vector, then init_guess can be a scalar, a vector of the same length as vars, or a matrix with two columns and the number of rows equal to the number of vars. If vars is a vector and init_guess is a scalar, then init_guess is expanded into a vector of the same length as vars with all elements equal to init_guess.

• 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.