필터 지우기
필터 지우기

Solving constrained non linear system using fmincon doesn't work

조회 수: 1 (최근 30일)
Pl Pl
Pl Pl 2013년 2월 12일
Hi all,
I have a non linear differential equation:
dm(1) = f1(m(1),m(2),m(3))
dm(2) = f2(m(1),m(2),m(3))
dm(3) = f3(m(1),m(2),m(3))
whose solutions must lie on the unit sphere. I'm trying to compute the fixed points of the system so I have to solve the system:
f1(m(1),m(2),m(3)) = 0
f2(m(1),m(2),m(3)) = 0
f3(m(1),m(2),m(3)) = 0
I first tried to use fsolve but this gave irrelevant solutions, not even lying on the unit sphere. I then tried to use fmincon by squaring my dm-vector so that it becomes positive. Here is what my code looks like:
Differential equation:
function dm = llgfix(t,m,param)
dm = zeros(3,1);
dm(1) = f1(m(1),m(2),m(3));
dm(2) = f2(m(1),m(2),m(3));
dm(3) = f3(m(1),m(2),m(3));
dm = dm.^2;
end
Constraints:
function [c, ceq] = unitsphere(x)
c = [];
ceq = x(1)^2 + x(2)^2 + x(3)^2 - 1;
Minimisation:
m0 = rand(1,3);
options = optimset('Display','iter','Algorithm','active-set');
fix = fmincon(@llgfix,m0./norm(m0),[],[],[],[],[],[],@unitsphere,options)
However, when running the code, I always get the same error:
Error using fmincon (line 674)
User supplied objective function must return a scalar value.
and I can't figure out why, does anybody have an idea?
Thanks in advance!

채택된 답변

Alan Weiss
Alan Weiss 2013년 2월 13일
This question was answered (satisfactorily, I believe) on comp.soft-sys.matlab.
Alan Weiss
MATLAB mathematical toolbox documentation

추가 답변 (2개)

Sven
Sven 2013년 2월 13일
Hi Pi Pi,
Two things. Firstly, your specific error:
User supplied objective function must return a scalar value.
comes because the "objective" function you are providing to fmincon (ie, the function llgfix) is returning a 1-by-3 matrix in the output variable dm. Instead, try:
function dm_sum = llgfix(t,m,param)
dm = zeros(3,1);
dm(1) = f1(m(1),m(2),m(3));
dm(2) = f2(m(1),m(2),m(3));
dm(3) = f3(m(1),m(2),m(3));
dm_sum = sum(dm.^2);
end
Next, when you call fmincon, you use the syntax:
fix = fmincon(@llgfix,...)
fmincon will, by default (ie, with just the @ symbol), only provide one input variable - that's the variable you are trying to modify.
However, your actual llgfix function takes three input variables. I think that this isn't what you intend. Note that you can use syntax like:
fix = fmincon(@(m)llgfix(otherVar,m,otherVar2),...)
To get fmincon to basically call a function with a different set of inputs. Or, you can modify llgfix to only take one input.
Did this get you on the right track?
Thanks, Sven.

Pl Pl
Pl Pl 2013년 2월 13일
Thanks to Sven and my apologies to Alan for not having indicated before that the problem was solved, I completely forgot this discussion.

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by