EquationProblem - System of nonlinear equations - MATLAB (original) (raw)
System of nonlinear equations
Description
Specify a system of equations using optimization variables, and solve the system using solve.
Creation
Create an EquationProblem
object by using the eqnproblem function. Add equations to the problem by creating OptimizationEquality objects and setting them as Equations
properties of theEquationProblem
object.
prob = eqnproblem; x = optimvar('x'); eqn = x^5 - x^4 + 3*x == 1/2; prob.Equations.eqn = eqn;
Warning
The problem-based approach does not support complex values in the following: an objective function, nonlinear equalities, and nonlinear inequalities. If a function calculation has a complex value, even as an intermediate value, the final result might be incorrect.
Properties
Problem equations, specified as an OptimizationEquality array or structure withOptimizationEquality
arrays as fields.
Example: sum(x.^2,2) == 4
Problem label, specified as a string or character vector. The software does not useDescription
for computation. Description
is an arbitrary label that you can use for any reason. For example, you can share, archive, or present a model or problem, and store descriptive information about the model or problem in Description
.
Example: "An iterative approach to the Traveling Salesman problem"
Data Types: char
| string
This property is read-only.
Optimization variables in the object, specified as a structure of OptimizationVariable objects.
Data Types: struct
Object Functions
evaluate | Evaluate optimization expression or objectives and constraints in problem |
---|---|
issatisfied | Constraint satisfaction of an optimization problem at a set of points |
optimoptions | Create optimization options |
prob2struct | Convert optimization problem or equation problem to solver form |
show | Display information about optimization object |
solve | Solve optimization problem or equation problem |
solvers | Determine default and valid solvers for optimization problem or equation problem |
varindex | Map problem variables to solver-based variable index |
write | Save optimization object description |
Examples
To solve the nonlinear system of equations
exp(-exp(-(x1+x2)))=x2(1+x12)x1cos(x2)+x2sin(x1)=12
using the problem-based approach, first define x
as a two-element optimization variable.
Create the first equation as an optimization equality expression.
eq1 = exp(-exp(-(x(1) + x(2)))) == x(2)*(1 + x(1)^2);
Similarly, create the second equation as an optimization equality expression.
eq2 = x(1)*cos(x(2)) + x(2)*sin(x(1)) == 1/2;
Create an equation problem, and place the equations in the problem.
prob = eqnproblem; prob.Equations.eq1 = eq1; prob.Equations.eq2 = eq2;
Review the problem.
EquationProblem :
Solve for:
x
eq1:
exp((-exp((-(x(1) + x(2)))))) == (x(2) .* (1 + x(1).^2))
eq2:
((x(1) .* cos(x(2))) + (x(2) .* sin(x(1)))) == 0.5
Solve the problem starting from the point [0,0]
. For the problem-based approach, specify the initial point as a structure, with the variable names as the fields of the structure. For this problem, there is only one variable, x
.
x0.x = [0 0]; [sol,fval,exitflag] = solve(prob,x0)
Solving problem using fsolve.
Equation solved.
fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
sol = struct with fields: x: [2×1 double]
fval = struct with fields: eq1: -2.4070e-07 eq2: -3.8255e-08
exitflag = EquationSolved
View the solution point.
Create a set of equations in two optimization variables.
x = optimvar("x"); y = optimvar("y"); prob = eqnproblem; prob.Equations.eq1 = x^2 + y^2/4 == 2; prob.Equations.eq2 = x^2/4 + 2*y^2 == 2;
Solve the system of equation starting from x=1,y=1/2.
x0.x = 1; x0.y = 1/2; sol = solve(prob,x0)
Solving problem using fsolve.
Equation solved.
fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
sol = struct with fields: x: 1.3440 y: 0.8799
Evaluate the equations at the points x0
and sol
.
vars = optimvalues(prob,x=[x0.x sol.x],y=[x0.y sol.y]); vals = evaluate(prob,vars)
vals = 1×2 OptimizationValues vector with properties:
Variables properties: x: [1 1.3440] y: [0.5000 0.8799]
Equation properties: eq1: [0.9375 8.4322e-10] eq2: [1.2500 6.7431e-09]
The first point, x0
, has nonzero values for both equations eq1
and eq2
. The second point, sol
, has nearly zero values of these equations, as expected for a solution.
Find the degree of equation satisfaction using issatisfied
.
[satisfied details] = issatisfied(prob,vars)
satisfied = 1×2 logical array
0 1
details = 1×2 OptimizationValues vector with properties:
Variables properties: x: [1 1.3440] y: [0.5000 0.8799]
Equation properties: eq1: [0 1] eq2: [0 1]
The first point, x0
, is not a solution, and satisfied
is 0
for that point. The second point, sol
, is a solution, and satisfied
is 1
for that point. The equation properties show that neither equation is satisfied at the first point, and both are satisfied at the second point.
Version History
Introduced in R2019b
You can now evaluate optimization expressions and constraints using evaluate and issatisfied for EquationProblem
objects.
The value of a constraint depends on the constraint type. An equation is equivalent to an ==
constraint. For expressions L
and R
:
Constraint Type | Value |
---|---|
L <= R | L – R |
L >= R | R – L |
L == R | abs(L – R) |
For details, see the evaluate andissatisfied reference pages.