Documentation |
Numeric solver
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.
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.
[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.
___ = vpasolve(___,Name,Value) numerically solves the equation or system of equations for the variable or variables using additional options specified by one or more Name,Value pair arguments.
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
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 = x: [6x1 sym] y: [6x1 sym]
Display solutions by accessing 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 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*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
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.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
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
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