eqnproblem
Create equation problem
Description
Use eqnproblem
to create an equation problem.
Tip
For the full workflow, see Problem-Based Workflow for Solving Equations.
specifies additional options using one or more name-value pair arguments. For example, you
can specify equations when constructing the problem by using the
prob
= eqnproblem(Name,Value
)Equations
name.
Examples
Solve Nonlinear System of Equations, Problem-Based
To solve the nonlinear system of equations
using the problem-based approach, first define x
as a two-element optimization variable.
x = optimvar('x',2);
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.
show(prob)
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: [2x1 double]
fval = struct with fields:
eq1: -2.4070e-07
eq2: -3.8255e-08
exitflag = EquationSolved
View the solution point.
disp(sol.x)
0.3532 0.6061
Unsupported Functions Require fcn2optimexpr
If your equation functions are not composed of elementary functions, you must convert the functions to optimization expressions using fcn2optimexpr
. For the present example:
ls1 = fcn2optimexpr(@(x)exp(-exp(-(x(1)+x(2)))),x); eq1 = ls1 == x(2)*(1 + x(1)^2); ls2 = fcn2optimexpr(@(x)x(1)*cos(x(2))+x(2)*sin(x(1)),x); eq2 = ls2 == 1/2;
See Supported Operations for Optimization Variables and Expressions and Convert Nonlinear Function to Optimization Expression.
Solve Nonlinear System of Polynomials, Problem-Based
When x
is a 2-by-2 matrix, the equation
is a system of polynomial equations. Here, means using matrix multiplication. You can easily formulate and solve this system using the problem-based approach.
First, define the variable x
as a 2-by-2 matrix variable.
x = optimvar('x',2,2);
Define the equation to be solved in terms of x
.
eqn = x^3 == [1 2;3 4];
Create an equation problem with this equation.
prob = eqnproblem('Equations',eqn);
Solve the problem starting from the point [1 1;1 1]
.
x0.x = ones(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: [2x2 double]
Examine the solution.
disp(sol.x)
-0.1291 0.8602 1.2903 1.1612
Display the cube of the solution.
sol.x^3
ans = 2×2
1.0000 2.0000
3.0000 4.0000
Input Arguments
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: prob = eqnproblem('Equations',eqn)
Equations
— Problem equations
[]
(default) | OptimizationEquality
array | structure with OptimizationEquality
arrays as fields
Problem equations, specified as an OptimizationEquality
array or structure with
OptimizationEquality
arrays as fields.
Example: sum(x.^2,2) == 4
Description
— Problem label
''
(default) | string | character vector
Problem label, specified as a string or character vector. The software does not use
Description
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
Output Arguments
prob
— Equation problem
EquationProblem
object
Equation problem, returned as an EquationProblem
object. Typically, to complete the problem description, you specify
prob.Equations
and, for nonlinear equations, an initial point
structure. Solve a complete problem by calling solve
.
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.
Version History
Introduced in R2019b
Comando MATLAB
Hai fatto clic su un collegamento che corrisponde a questo comando MATLAB:
Esegui il comando inserendolo nella finestra di comando MATLAB. I browser web non supportano i comandi MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)