Parallel optimization with extra parameters (fmincon) using for loop and if then statements
1 次查看(过去 30 天)
显示 更早的评论
I am trying to do parallel processing optimization (fmincon) using for loops for each a,b=0:.01:1 includes if then statement because I have a normalized condition which is a^2+b^2+c^2=1 then c=sqrt(1-a^2-b^2). I am getting confused on the coding. I tried this code (see below), but it could not work. I wrote a file objfun.m.
function f= objfun (x,a,b,c)
load test.mat a 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=p1+2*a*c*p2+2*a*b*p3+2*b*c*p4;
I wrote the main file
%x=[x(1),x(2),x(3),x(4),x(5),x(6)]; % angles;
clc;
p=0:0.1:1;q=0:0.1:1;
for m=1:length(p)
for n=1:length(q)
a=p(m);b=q(n);
if a^2+b^2+c^2=1 Then c=sqrt(1-a*a-b*b)
save test.mat a b
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);
clear a b
opt_val(m,n)=fval;
end
end
end
I got this error (Error: File: main.m Line: 7 Column: 23 The expression to the left of the equals sign is not a valid target for an assignment.) What I have to do to make this code work? I need some help on structuring the main file please .
0 个评论
回答(1 个)
Alan Weiss
2015-11-30
At the very least you need to have an objective function that takes exactly one input argument. Perhaps
fun = @(x)objfun(x,a,b,c)
(define fun after each time you define a, b, and c). Then call
[x,fval] = fmincon(fun,x0,...
Alan Weiss
MATLAB mathematical toolbox documentation
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!