Solve Nonlinear System of Equations, Problem-Based - MATLAB & Simulink (original) (raw)

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.

See Also

solve | fcn2optimexpr

Topics