How to run the following code in parallel

2 views (last 30 days)
Raja Emlik
Raja Emlik on 17 Dec 2015
Commented: Raja Emlik on 18 Dec 2015
I want to make this code to run in parallel for each value of a, and b= 0:0.001:1, and there is a normalized condition that a^2+b^2+c^=1, so c=sqrt(1-a*a-b*b), in order to make plot 3D (fmax vs a, and b) . as this code run perfectly for just one value as you can see below. Any help would be appreciated. Thanks
m=0;
th1max=0;
th2max=0;
th3max=0;
th4max=0;
th5max=0;
th6max=0;
%
step = 0.3;
for th1=(0:step:2)*pi
for th2=(0:step:2)*pi
for th3=(0:step:2)*pi
for th4=(0:step:2)*pi
for th5=(0:step:2)*pi
for th6=(0:step:2)*pi
a=.57;b=.57;c=sqrt(1-a*a-b*b);
p1=-sin(((th2-th1)/2)+((th4-th3)/2)+((th6-th5)/2))+sin(((th4-th3)/2)+((th6-th5)/2)-((th2-th1)/2))+sin(((th2-th1)/2)+((th6-th5)/2)-((th4-th3)/2))+sin(((th2-th1)/2)+((th4-th3)/2)-((th6-th5)/2));
p2=sin(((th2-th1)/2)+((th4-th3)/2)+((th6-th5)/2))+sin(((th4-th3)/2)+((th6-th5)/2)-((th2-th1)/2))-sin(((th2-th1)/2)+((th6-th5)/2)-((th4-th3)/2))+sin(((th2-th1)/2)+((th4-th3)/2)-((th6-th5)/2));
p3=sin(((th2-th1)/2)+((th4-th3)/2)+((th6-th5)/2))-sin(((th4-th3)/2)+((th6-th5)/2)-((th2-th1)/2))+sin(((th2-th1)/2)+((th6-th5)/2)-((th4-th3)/2))+sin(((th2-th1)/2)+((th4-th3)/2)-((th6-th5)/2));
p4=sin(((th2-th1)/2)+((th4-th3)/2)+((th6-th5)/2))+sin(((th4-th3)/2)+((th6-th5)/2)-((th2-th1)/2))+sin(((th2-th1)/2)+((th6-th5)/2)-((th4-th3)/2))-sin(((th2-th1)/2)+((th4-th3)/2)-((th6-th5)/2));
f=p1+2*a*c*p2+2*a*b*p3+2*b*c*p4;
if f>m
m=f;
th1max=th1;
th2max=th2;
th3max=th3;
th4max=th4;
th5max=th5;
th6max=th6;
end
%display (f);
end
end
end
end
end
end
m
th1max
th2max
th3max
th4max
th5max
th6max
**The result***
m =
4.3530
th1max =
0
th2max =
1.8850
th3max =
0
th4max =
1.8850
th5max =
1.8850
th6max =
3.7699

Answers (1)

Geoff Hayes
Geoff Hayes on 17 Dec 2015
Raja - is the error message
Error using *
Inner matrix dimensions must agree.
Error in *** (line 29)
f=p1+2*a*c*p2+2*a*b*p3+2*b*c*p4;
If so, then the problem is with the a and b variables. Both are 1x1001 arrays and so the multiplication of
a*b
will generate the error. You may want to do element-wise multiplication for these two (please verify) in which case this line of code would be written as
f=p1+2*a*c*p2+2*a.*b*p3+2*b*c*p4;
That will fix the error but may not be exactly what you have intended since now f is a 1x1001 array and the subsequent lines where you assign
m=f;
and then
z(i,j) = m;
will fail with the
Subscripted assignment dimension mismatch.
error.
Please format your above code so that is readable and add comments where necessary to describe what the code is attempting.
  1 Comment
Raja Emlik
Raja Emlik on 18 Dec 2015
Thanks for your response . Actually i did that but i am not sure to run in parallel, i edited the code run for one values of a,b see the Q .. could you please help me how to do it to run in parallel Thanks

Sign in to comment.

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!