필터 지우기
필터 지우기

How to run the following code in parallel

조회 수: 1 (최근 30일)
Raja Emlik
Raja Emlik 2015년 12월 17일
댓글: Raja Emlik 2015년 12월 18일
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

답변 (1개)

Geoff Hayes
Geoff Hayes 2015년 12월 17일
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
Raja Emlik
Raja Emlik 2015년 12월 18일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Quantum Mechanics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by