Filter löschen
Filter löschen

How to use fminunc to solve simultaneous system of equations?

4 Ansichten (letzte 30 Tage)
Ash Ash
Ash Ash am 13 Jul. 2018
Kommentiert: Matt J am 14 Jul. 2018
Hi, I am looking to minimise a system of multivariate underconstrained simultaneous equations using fminunc, with 5 variables and 20 equations, which are all generated in a symbolic form because the equations will vary based on inputs.
Firstly I have figured out how to solve one example equation using fminunc:
x=sym('x',[2,1]);y=sym('y',[3,1]);
eqn=3*x(1)^2 + 2*x(1)*x(2) + x(2)^2 - 4*x(1) + 5*x(2);
fun = matlabFunction(eqn) ;
funMiddleMan = @(y) fun(y(1),y(2));
sol = fminunc(funMiddleMan, [1,1])
The problem I am faced with now, is how do I repeat this for multiple equations?
x=sym('x',[2,1]);y=sym('y',[3,1]);
eqn(1)=3*x(1)^2 + 2*x(1)*x(2) + x(2)^2 - 4*x(1) + 5*x(2);
eqn(2)=3*x(1)^2 + 2*x(1)*x(2) + x(2)^2 - 2*x(1) + 4*x(2);
fun = matlabFunction(eqn) ;
funMiddleMan = @(y) fun(y(1),y(2));
sol_2 = fminunc(funMiddleMan, [1,1])
I tried the script below and it gave me the following error
Error using fminunc (line 346)
Supplied objective function must return a scalar value.
Could you please help me? Thank you.
  1 Kommentar
Kaushik Lakshminarasimhan
Kaushik Lakshminarasimhan am 13 Jul. 2018
What is it that you are trying to minimize? You have two equations, so your funMiddleMan returns two outputs instead of one. You need to reformulate your objective so that funMiddleMan returns a scalar value.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Matt J
Matt J am 14 Jul. 2018
Use FSOLVE instead of FMINUNC. It is better tailored to this specific type of problem. Also, you do not have to scalarize the objective function with FSOLVE, so it will mean less code modification for you.
  2 Kommentare
Walter Roberson
Walter Roberson am 14 Jul. 2018
fsolve() looks for simultaneous zeros, but the user wants to minimize.
Matt J
Matt J am 14 Jul. 2018
I'm working on a hunch that that's not what the OP really wants. fsolve() will still try to minimize the norm of the objective if simultaneous zeros cannot be found

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 14 Jul. 2018
To minimize a system of equations, you need to use gamultiobj(), which will build pareto fronts.

Kategorien

Mehr zu Get Started with Optimization Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by