How to repeat previous steps from the output

5 views (last 30 days)
x=1 for a=[5 6 7 8] %solved optimization problem to get "x" end
Now, the new "x" is not 1, but the optimal value and Continue this to 5 iteration
How to do the last (above) step?

Accepted Answer

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 19 Jun 2021
Here is a simple optimization problem from MATLAB help documentation taken as an example to solve within a loop iteration.
x = optimvar('x',2);
eq1 = exp(-exp(-(x(1) + x(2)))) == x(2)*(1 + x(1)^2);
eq2 = x(1)*cos(x(2)) + x(2)*sin(x(1)) == 1/2;
prob = eqnproblem;
prob.Equations.eq1 = eq1;
prob.Equations.eq2 = eq2;
show(prob)
x0.x = [0 0];
[sol,fval,exitflag] = solve(prob,x0);
disp(sol.x);
disp(sol.x);
% Solve in iteration
for a=[5 6 7 8 9 10]
eq1 = exp(-exp(-(x(1) + x(2)))) == a*x(2)*(1 + x(1)^2); % Variabel "a" is used in the example equation
eq2 = x(1)*cos(x(2)) + x(2)*sin(x(1)) == 1/2;
prob = eqnproblem;
prob.Equations.eq1 = eq1;
prob.Equations.eq2 = eq2;
[sol,fval,exitflag] = solve(prob,sol);
disp(sol.x);
end

More Answers (0)

Categories

Find more on Problem-Based Optimization Setup in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!