how to use fmincon in a loop

I have 5 unknown parameters but only 4 equations, but each parameter has an interval, so I prefer to use fmincon. For example, these are 4 equations:
2*x(3) + x(5) + 5*x(1) - 1=0;
x7*(4) + 3*x(3) + x(2) - 10=0;
x(3) + 2*x(5) + 5*x(1) - 3=0;
3*x(4) + 9*x(5) + x(2) - 10=0;
My idea is like this:
for x(1)=0.1:0.1:1
fmincon(@myfun, x0,[]...)
end
But I do not know how to let x(1) be changed in the loop.
Thank you for any suggestions in advance.

4 Comments

what are the conditions? I think you can solve this without using fmincon
There are no special conditions, but x(1)...x(5) should be less than 1 but larger than 0. And I think I can add this requirement in the fmincon function.
3*x(4) + 9*x(6) + x(2) - 10=0;
is this correct? x(6)? I thought you had only 5 parameters?
Sorry, I just correct my mistake. You are right, only 5 parameters. This is just an example.

Sign in to comment.

 Accepted Answer

A = [5 0 2 0 1;
0 1 3 7 0;
5 0 1 0 2;
0 1 0 3 9];
b = [-1;-10;-3;-10];
FF = @(x) norm(A*x+b);
xx = fmincon(FF,rand(5,1),[],[],[],[],zeros(5,1),ones(5,1))

5 Comments

Hi Amit,
I think you missed my biggest question that I asked: how can I put [fmincon] in a loop. Since there are 5 parameters but 4 equations, so I try to fix a parameter first and then use [fmincon] to calculate others, and then do it in a loop.
Can I put those equations in my script with {fmincon}, instead of writing a function to include my equations?
Cong, Yes, you can definitely put this in a script. However, I don't think this will give you a solution. I have tested the equations you have given here by varying x(5) between 0 and 1 and allowing the rest to be solved as A\b but I could not find an solution satisfying 0< x(i) < 1.
The method I posted earlier, minimizes the function to the minimum value possible but it wont solve it. Are you sure there is a solution for this problem within the ranges you specified?
Hi Amit, the equations that I posted are not my real problem which are more complex and non-linear. Here I just want to get an idea how to vary my x(5) while using 'fmincon'. I figure out that it is a problem of using anonymous function. Here is a simple example, but note that some contents are not provided.
for p=1:10
i=i+1;
x(i,:) = fmincon(@(X) myfun(X,p), x0, [], [], [], [], ...
[], [], [], options);
end
Thank you for the time.
what is i there?
In a way, lets say you're trying to vary x(5) only, you can do it two way.
  1. set the upper and lower bounds for x(5) as value-eps and value + eps, this will contain the value of x(5) within the range.
  2. For you objective function, use only 4 input variable and pass the value of x(5) as a scalar as in fmincon(@(x) myfun(x,x5val). In this scenario, you'll have to write the function accordingly.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 7 Feb 2014

Commented:

on 10 Feb 2014

Community Treasure Hunt

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

Start Hunting!