Parallel optimization with extra parameters (fmincon) using for loop and if then statements

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)
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
```

What I have to do to make this code work? I need some help on structuring the main file please .

As your error message states, there is a problem with this line in the main code:

```if a^2+b^2+c^2=1 Then c=sqrt(1-a*a-b*b)
```

There are a few problems here. First, this is not valid MATLAB syntax for an if clause. For the correct syntax, see the documentation.

Secondly, a single = in MATLAB is an assignment operator, not a comparison. For testing equality, you need to use ==, for example: a==b will produce true if a is equal to b and false otherwise.