Parallel optimization with extra parameters (fmincon)

2 views (last 30 days)
how do i run optimization (fmincon) repeatedly
I have a function
f = p1 + 2*a*c*p2 + 2*a*b*p3 + 2*b*c*p4
where
p1=-sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)+((x(6)-x(5))/2))+sin(((x(4)-x(3))/2)+((x(6)-x(5))/2)-((x(2)-x(1))/2))+sin(((x(2)-x(1))/2)+((x(6)-x(5))/2)-((x(4)-x(3))/2))+sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)-((x(6)-x(5))/2));
p2=sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)+((x(6)-x(5))/2))+sin(((x(4)-x(3))/2)+((x(6)-x(5))/2)-((x(2)-x(1))/2))-sin(((x(2)-x(1))/2)+((x(6)-x(5))/2)-((x(4)-x(3))/2))+sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)-((x(6)-x(5))/2));
p3=sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)+((x(6)-x(5))/2))-sin(((x(4)-x(3))/2)+((x(6)-x(5))/2)-((x(2)-x(1))/2))+sin(((x(2)-x(1))/2)+((x(6)-x(5))/2)-((x(4)-x(3))/2))+sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)-((x(6)-x(5))/2));
p4=sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)+((x(6)-x(5))/2))+sin(((x(4)-x(3))/2)+((x(6)-x(5))/2)-((x(2)-x(1))/2))+sin(((x(2)-x(1))/2)+((x(6)-x(5))/2)-((x(4)-x(3))/2))-sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)-((x(6)-x(5))/2));
and x1 until x6 are angles subject to the bound constraint from 0 to pi.
I want to minimize this function for a range of a=[0:0.01:1] and b=[0:0.01:1]. (That is, I want to minimize this function for each a,b=0, a,b=0.01,a,b=0.02 ... and so on). *Also , there is a normalize condition which is a^2+b^2+c^2=1.*
so this my code
Step 1: Write a file objfun.m.
function f= objfun (x,a,b,c)
a=.57;
b=.57; % i pick a number for a,b
c=sqrt(1-a*a-b*b);
p1=-sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)+((x(6)-x(5))/2))+sin(((x(4)-x(3))/2)+((x(6)-x(5))/2)-((x(2)-x(1))/2))+sin(((x(2)-x(1))/2)+((x(6)-x(5))/2)-((x(4)-x(3))/2))+sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)-((x(6)-x(5))/2));
p2=sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)+((x(6)-x(5))/2))+sin(((x(4)-x(3))/2)+((x(6)-x(5))/2)-((x(2)-x(1))/2))-sin(((x(2)-x(1))/2)+((x(6)-x(5))/2)-((x(4)-x(3))/2))+sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)-((x(6)-x(5))/2));
p3=sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)+((x(6)-x(5))/2))-sin(((x(4)-x(3))/2)+((x(6)-x(5))/2)-((x(2)-x(1))/2))+sin(((x(2)-x(1))/2)+((x(6)-x(5))/2)-((x(4)-x(3))/2))+sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)-((x(6)-x(5))/2));
p4=sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)+((x(6)-x(5))/2))+sin(((x(4)-x(3))/2)+((x(6)-x(5))/2)-((x(2)-x(1))/2))+sin(((x(2)-x(1))/2)+((x(6)-x(5))/2)-((x(4)-x(3))/2))-sin(((x(2)-x(1))/2)+((x(4)-x(3))/2)-((x(6)-x(5))/2));
%f=4*(p1+C13*p2+C12*p3+C23*p4);
f=p1+2*a*c*p2+2*a*b*p3+2*b*c*p4;
Step 2: Invoke constrained optimization routine.
x=[x(1),x(2),x(3),x(4),x(5),x(6)]; % angles;
lb=[0,0,0,0,0,0];
ub=[pi,pi,pi,pi,pi,pi];
x0=[pi/8;pi/3;0.7*pi;pi/2;.5;pi/4];
[x,fval]=fmincon(@objfun,x0,[],[],[],[],lb,ub)
the solution produced is
x=
2.5530
0.6431
2.5305
0.6195
2.5531
0.6421
fval=
-4.3546
can any one change the code so that, the optimization will run for 0:0.01:1 times, and save the optimal values for each a,b?

Answers (1)

Marc
Marc on 28 Nov 2015
Edited: Walter Roberson on 28 Nov 2015
You can try a for loop...
Something like this.
a = 0:0.01:1;
b = 0:0.01:1;
mSize = max(size(a));
xNew = zeros(mSize,6); % Preallocate for speed
fNew = zeros(mSize,1);
for n = 1:mSize
a1 = a(n);
b1 = b(n);
%%%here you put your c and fmincon like above
xNew(n,:) = x; % from fmincon
fNew(n) = feval; % from fmincon
end
I think this should work. May take a while. You may want to start with something like a = 0:0.1:1 before trying the rest.
The code above assumes a and b will always be equal. If you want different combinations, then you will need two for loops. One nested within.
  1 Comment
Raja Emlik
Raja Emlik on 28 Nov 2015
Thanks for your replay . In the problem there a normalize condition which is a^2+b^2+c^2=1 so i need to put if statement as well. could you please help me for different combinations, actually i did many code for that but it could not work as i have not used matlab. Thanks in advance.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!