Documentation

# vpasolve

Solve equations numerically

## Syntax

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

## Description

example

````S = vpasolve(eqn)` numerically solves the equation `eqn` for the variable determined by `symvar`.```

example

````S = vpasolve(eqn,var)` numerically solves the equation `eqn` for the variable specified by `var`.```

example

````S = vpasolve(eqn,var,init_guess)` numerically solves the equation `eqn` for the variable specified by `var` using the starting point or search range specified in `init_guess`. If you do not specify `var`, `vpasolve` solves for variables determined by `symvar`.```
````Y = vpasolve(eqns)` numerically solves the system of equations `eqns` for variables determined by `symvar`. This syntax returns `Y` as a structure array. You can access the solutions by indexing into the array.```

example

````Y = vpasolve(eqns,vars)` numerically solves the system of equations `eqns` for variables specified by `vars`. This syntax returns a structure array that contains the solutions. The fields in the structure array correspond to the variables specified by `vars`.```
````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`.```
````[y1,...,yN] = vpasolve(eqns)` numerically solves the system of equations `eqns` for variables determined by `symvar`. This syntax assigns the solutions to variables `y1,...,yN`.```

example

````[y1,...,yN] = vpasolve(eqns,vars)` numerically solves the system of equations `eqns` for the variables specified by `vars`.```
````[y1,...,yN] = vpasolve(eqns,vars,init_guess)` numerically solves the system of equations `eqns` for the variables specified by `vars` using the starting values or the search range `init_guess`.```

example

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

## Examples

### Solve Polynomial Equation

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

### Solve Nonpolynomial Equation

For nonpolynomial equations, `vpasolve` returns the first solution that it finds:

```syms x vpasolve(sin(x^2) == 1/2, x)```
```ans = -226.94447241941511682716953887638```

### Assign Solutions to Structure Array

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

### Assign Solutions to Variables When Solving System of Equations

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

### Find Multiple Solutions by Specifying Starting Points

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}\left(x\right)={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```

### Specify Ranges for Solutions

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

### Find Multiple Solutions for Nonpolynomial Equation

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

## 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 N-D 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 that 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 for a solution, specified as a numeric value, vector, or matrix with two columns.

If `init_guess` is a number or, in the case of multivariate equations, a vector of numbers, then the numeric solver uses it as a starting point. If `init_guess` is specified as a scalar while the system of equations is multivariate, then the numeric solver uses the scalar value as a starting point for all variables.

If `init_guess` is a matrix with two columns, then the two entries of the rows specify the bounds of a search range for the corresponding variables. To specify a starting point in a matrix of search ranges, specify both columns as the starting point value.

To omit a search range for a variable, set the search range for that variable to `[NaN, NaN]` in `init_guess`. All other uses of `NaN` in `init_guess` will error.

By default, `vpasolve` uses its own internal choices for starting points and search ranges.

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

## Algorithms

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

## See Also

#### Mathematical Modeling with Symbolic Math Toolbox

Get examples and videos