reducing running time for matlab for loops - matlab

my matlab code bellow takes a very long time to run. How could I compose it to take just seconds to run? I would be thankful if you could help me. parameters i, k_hat and alpha are constants.
%%%%%%%%%%%%%%%%%%%
p_desired=zeros(1,length(alpha));
p_ICI=zeros(1,length(alpha));
p_MUI=zeros(1,length(alpha));
tic
for ss=1:length(alpha)
uN=2*pi/N*Ts*abs(sin(alpha(ss)));
uM=2*pi/M*Ts*abs(sin(alpha(ss)));
for j=0:M/N-1
for k=0:N-1
for q=0:N-1
for qq=0:N-1
for q_hat=0:N-1
for qq_hat=0:N-1
for n=0:M-1
for m=-Ng:M-1
for n_hat=0:N-1
for m_hat=-Ng:N-1
if (n-m-n_hat+m_hat==0)
if (i==j && k==k_hat)
p_desired(ss)=p_desired(ss)+((sin(-alpha(ss))-1i*cos(-alpha(ss)))/N)^.5*exp(1i/2*k_hat^2*cot(-alpha(ss))*uN^2)*exp(1i/2*q^2*cot(-alpha(ss))*Ts^2)*exp(-1i*2*pi*q*k_hat/N)...
*((sin(-alpha(ss))+1i*cos(-alpha(ss)))/N)^.5*exp(-1i/2*k_hat^2*cot(-alpha(ss))*uN^2)*exp(-1i/2*q_hat^2*cot(-alpha(ss))*Ts^2)*exp(+1i*2*pi*q_hat*k_hat/N)...
* ((sin(alpha(ss))-1i*cos(alpha(ss)))/N)^.5*exp(1i/2*qq^2*cot(alpha(ss))*uN^2)*exp(1i/2*k_hat^2*cot(alpha(ss))*Ts^2)*exp(-1i*2*pi*qq*k_hat/N)...
*((sin(alpha(ss))+1i*cos(alpha(ss)))/N)^.5*exp(-1i/2*qq_hat^2*cot(alpha(ss))*uN^2)*exp(-1i/2*k_hat^2*cot(alpha(ss))*Ts^2)*exp(+1i*2*pi*qq_hat*k_hat/N)...
*((sin(alpha(ss))-1i*cos(alpha(ss)))/M)^.5*exp(1i/2*(q+N*(i-1))^2*cot(alpha(ss))*uM^2)*exp(1i/2*n^2*cot(alpha(ss))*Ts^2)*exp(-1i*2*pi*(q+N*(i-1))*n/M)...
*((sin(alpha(ss))+1i*cos(alpha(ss)))/M)^.5*exp(-1i/2*(q_hat+N*(i-1))^2*cot(alpha(ss))*uM^2)*exp(-1i/2*n_hat^2*cot(alpha(ss))*Ts^2)*exp(+1i*2*pi*(q_hat+N*(i-1))*n_hat/M)...
*((sin(-alpha(ss))-1i*cos(-alpha(ss)))/M)^.5*exp(1i/2*m^2*cot(-alpha(ss))*uM^2)*exp(1i/2*(qq+N*(i-1))^2*cot(-alpha(ss))*Ts^2)*exp(-1i*2*pi*m*(qq+N*(i-1))/M)...
*((sin(-alpha(ss))+1i*cos(-alpha(ss)))/M)^.5*exp(-1i/2*m_hat^2*cot(-alpha(ss))*uM^2)*exp(-1i/2*(qq_hat+N*(i-1))^2*cot(-alpha(ss))*Ts^2)*exp(+1i*2*pi*m_hat*(qq_hat+N*(i-1))/M)...
*exp(1i*2*pi/M*(n-n_hat)*e)*exp(-abs(n-m)/gamma)* besselj(0,2*pi*abs(n-n_hat)*fd);
elseif (i==j && k~=k_hat)
p_ICI(ss)=p_ICI(ss)+((sin(-alpha(ss))-1i*cos(-alpha(ss)))/N)^.5*exp(1i/2*k_hat^2*cot(-alpha(ss))*uN^2)*exp(1i/2*q^2*cot(-alpha(ss))*Ts^2)*exp(-1i*2*pi*q*k_hat/N)...
*((sin(-alpha(ss))+1i*cos(-alpha(ss)))/N)^.5*exp(-1i/2*k_hat^2*cot(-alpha(ss))*uN^2)*exp(-1i/2*q_hat^2*cot(-alpha(ss))*Ts^2)*exp(+1i*2*pi*q_hat*k_hat/N)...
* ((sin(alpha(ss))-1i*cos(alpha(ss)))/N)^.5*exp(1i/2*qq^2*cot(alpha(ss))*uN^2)*exp(1i/2*k^2*cot(alpha(ss))*Ts^2)*exp(-1i*2*pi*qq*k/N)...
*((sin(alpha(ss))+1i*cos(alpha(ss)))/N)^.5*exp(-1i/2*qq_hat^2*cot(alpha(ss))*uN^2)*exp(-1i/2*k^2*cot(alpha(ss))*Ts^2)*exp(+1i*2*pi*qq_hat*k/N)...
*((sin(alpha(ss))-1i*cos(alpha(ss)))/M)^.5*exp(1i/2*(q+N*(i-1))^2*cot(alpha(ss))*uM^2)*exp(1i/2*n^2*cot(alpha(ss))*Ts^2)*exp(-1i*2*pi*(q+N*(i-1))*n/M)...
*((sin(alpha(ss))+1i*cos(alpha(ss)))/M)^.5*exp(-1i/2*(q_hat+N*(i-1))^2*cot(alpha(ss))*uM^2)*exp(-1i/2*n_hat^2*cot(alpha(ss))*Ts^2)*exp(+1i*2*pi*(q_hat+N*(i-1))*n_hat/M)...
*((sin(-alpha(ss))-1i*cos(-alpha(ss)))/M)^.5*exp(1i/2*m^2*cot(-alpha(ss))*uM^2)*exp(1i/2*(qq+N*(i-1))^2*cot(-alpha(ss))*Ts^2)*exp(-1i*2*pi*m*(qq+N*(i-1))/M)...
*((sin(-alpha(ss))+1i*cos(-alpha(ss)))/M)^.5*exp(-1i/2*m_hat^2*cot(-alpha(ss))*uM^2)*exp(-1i/2*(qq_hat+N*(i-1))^2*cot(-alpha(ss))*Ts^2)*exp(+1i*2*pi*m_hat*(qq_hat+N*(i-1))/M)...
*exp(1i*2*pi/M*(n-n_hat)*e)*exp(-abs(n-m)/gamma) *besselj(0,2*pi*abs(n-n_hat)*fd);
elseif (i~=j)
p_MUI(ss)=p_MUI(ss)+((sin(-alpha(ss))-1i*cos(-alpha(ss)))/N)^.5*exp(1i/2*k_hat^2*cot(-alpha(ss))*uN^2)*exp(1i/2*q^2*cot(-alpha(ss))*Ts^2)*exp(-1i*2*pi*q*k_hat/N)...
*((sin(-alpha(ss))+1i*cos(-alpha(ss)))/N)^.5*exp(-1i/2*k_hat^2*cot(-alpha(ss))*uN^2)*exp(-1i/2*q_hat^2*cot(-alpha(ss))*Ts^2)*exp(+1i*2*pi*q_hat*k_hat/N)...
* ((sin(alpha(ss))-1i*cos(alpha(ss)))/N)^.5*exp(1i/2*qq^2*cot(alpha(ss))*uN^2)*exp(1i/2*k^2*cot(alpha(ss))*Ts^2)*exp(-1i*2*pi*qq*k/N)...
*((sin(alpha(ss))+1i*cos(alpha(ss)))/N)^.5*exp(-1i/2*qq_hat^2*cot(alpha(ss))*uN^2)*exp(-1i/2*k^2*cot(alpha(ss))*Ts^2)*exp(+1i*2*pi*qq_hat*k/N)...
*((sin(alpha(ss))-1i*cos(alpha(ss)))/M)^.5*exp(1i/2*(q+N*(i-1))^2*cot(alpha(ss))*uM^2)*exp(1i/2*n^2*cot(alpha(ss))*Ts^2)*exp(-1i*2*pi*(q+N*(i-1))*n/M)...
*((sin(alpha(ss))+1i*cos(alpha(ss)))/M)^.5*exp(-1i/2*(q_hat+N*(i-1))^2*cot(alpha(ss))*uM^2)*exp(-1i/2*n_hat^2*cot(alpha(ss))*Ts^2)*exp(+1i*2*pi*(q_hat+N*(i-1))*n_hat/M)...
*((sin(-alpha(ss))-1i*cos(-alpha(ss)))/M)^.5*exp(1i/2*m^2*cot(-alpha(ss))*uM^2)*exp(1i/2*(qq+N*(j-1))^2*cot(-alpha(ss))*Ts^2)*exp(-1i*2*pi*m*(qq+N*(j-1))/M)...
*((sin(-alpha(ss))+1i*cos(-alpha(ss)))/M)^.5*exp(-1i/2*m_hat^2*cot(-alpha(ss))*uM^2)*exp(-1i/2*(qq_hat+N*(j-1))^2*cot(-alpha(ss))*Ts^2)*exp(+1i*2*pi*m_hat*(qq_hat+N*(j-1))/M)...
*exp(1i*2*pi/M*(n-n_hat)*e)*exp(-abs(n-m)/gamma)* besselj(0,2*pi*abs(n-n_hat)*fd);
end
else
p_desired(ss)=p_desired(ss);
p_ICI(ss)=p_ICI(ss);
p_MUI(ss)=p_MUI(ss);
end
end
end
end
end
end
end
end
end
end
end
end
toc

Remove "for ss=1:length(alpha)" and corresponding "end"
Convert all "alpha(ss)" to "alpha"
Convert all "p_desired(ss)" to "p_desired"
Convert all "p_ICI(ss)" to "p_ICI"
Convert all "p_MUI(ss)" to "p_MUI"
Convert all "*", "/" and "^" to ".*", './' and ".^" respectively.
Convert "elseif (i~=j)" to "else"
Remove below code:
else
p_desired(ss)=p_desired(ss);
p_ICI(ss)=p_ICI(ss);
p_MUI(ss)=p_MUI(ss);

Related

"Undefined operator '*' for input arguments of type 'function_handle'." for matlab

I want to code a FEM programme and here is my code
N1=2; %横向分解成2个元素
N2=2; %纵向分解成2个元素
N=2*N1*N2;
%三角元
top=1;
bottom=0;
left=0;
right=1;
h1=(right-left)/N1;
h2=(top-bottom)/N2;
T=zeros(3,2*N1*N2);%生成初始T矩阵
N10=N1+1;
N20=N2+1;
%先把每个element的行列转化为每个element对应的起始node的坐标,然和把坐标转化为index
for i=1:N2
for j=1:2*N1
if mod(j,2)==1
T(:,2*N1*(i-1)+j)=[(i-1)*N10+ceil(j/2);i*N10+ceil(j/2);(i-1)*N10+ceil(j/2)+1];
else
T(:,2*N1*(i-1)+j)=[(i-1)*N10+j/2+1;i*N10+j/2;i*N10+j/2+1];
end
end
end
P=zeros(2,(N1+1)*(N2+1));
for i=1:N1+1
for j=1:N2+1
P(:,(i-1)*(N1+1)+j)=[left+(i-1)*h1;bottom+(j-1)*h2];
end
end
N1_1= 2*N1;
N2_1= 2*N1;
N_1= 2*N1_1*N2_1;
%三角元
h1_1=(right-left)/N1_1;
h2_1=(top-bottom)/N2_1;
N10_1=N1_1+1;
N20_1=N2_1+1;
P_b=zeros(2,(N1_1+1)*(N2_1+1));
for i=1:N1_1+1
for j=1:N2_1+1
P_b(:,(i-1)*(N1_1+1)+j)=[left+(i-1)*h1_1;bottom+(j-1)*h2_1];
end
end
boundarynodes=zeros(2,N2_1+1+N1_1+N2_1+N1_1-1);
for j=1:2*N2_1+2*N1_1
boundarynodes(1,j)=-1;
end
for i=1:N1_1+1
boundarynodes(2,i)=(i-1)*(N1_1+1)+1;
end
for i=N1_1+2:N1_1+1+N2_1
boundarynodes(2,i)=(N1_1+1-1)*(N1_1+1)+i-(N1_1);
end
for i=N1_1+1+N2_1+N1_1:-1:N1_1+N2_1+2
boundarynodes(2,i)=((-i+2*N1_1+N2_1+2)-1)*(N1_1+1)+N1_1+1;
end
for i=N1_1+N2_1+N2_1+N1_1:-1:N1_1+2+N2_1+N1_1
boundarynodes(2,i)=-i+2*N1_1+2*N2_1+2;
end
%建立坐标到index函数
T_b=zeros(6,2*N1*N2);
c2i=#(i,j) (i-1)*(N1_1+1)+j;
for i=1:N1
for j=1:2*N2
if mod(j,2)==1
i_0=2*i-1;
j_0=2*ceil(j/2)-1;
T_b(:,2*N1*(i-1)+j)=[c2i(i_0,j_0);c2i(i_0+2,j_0);c2i(i_0,j_0+2);c2i(i_0+1,j_0);c2i(i_0+1,j_0+1);c2i(i_0,j_0+1)];
else
i_0=2*i-1;
j_0=2*(j/2+1)-1;
T_b(:,2*N1*(i-1)+j)=[c2i(i_0,j_0);c2i(i_0+2,j_0-2);c2i(i_0+2,j_0);c2i(i_0+1,j_0-1);c2i(i_0+2,j_0-1);c2i(i_0+1,j_0)];
end
end
end
A=sparse((N1_1+1)*(N2_1+1),(N1_1+1)*(N2_1+1));
for n=1:N
y_n2=P_b(2,T_b(2,n));
y_n3=P_b(2,T_b(3,n));
y_n1=P_b(2,T_b(1,n));
y_n4=P_b(2,T_b(4,n));
y_n5=P_b(2,T_b(5,n));
y_n6=P_b(2,T_b(6,n));
x_n3=P_b(1,T_b(3,n));
x_n2=P_b(1,T_b(2,n));
x_n1=P_b(1,T_b(1,n));
x_n4=P_b(1,T_b(4,n));
x_n5=P_b(1,T_b(5,n));
x_n6=P_b(1,T_b(6,n));
Y_n2=P(2,T(2,n));
Y_n3=P(2,T(3,n));
Y_n1=P(2,T(1,n));
X_n3=P(1,T(3,n));
X_n2=P(1,T(2,n));
X_n1=P(1,T(1,n));
xmin=X_n1;
xmax=xmin+h1;
J_n=(X_n2-X_n1)*(Y_n3-Y_n1)-(X_n3-X_n1)*(Y_n2-Y_n1);
x_hat=#(x,y) ((Y_n3-Y_n1)*(x-X_n1)-(X_n3-X_n1)*(y-Y_n1))/J_n;
y_hat=#(x,y) ((X_n2-X_n1)*(y-Y_n1)-(Y_n2-Y_n1)*(x-X_n1))/J_n;
if mod(n,2)==1
ymin= Y_n1;
ymax=#(x) Y_n3+((Y_n3-Y_n2)/(X_n3-X_n2))*(x-X_n3);
else
ymin=#(x) Y_n2+((Y_n2-Y_n1)/(X_n2-X_n1))*(x-x_n2);
ymax= Y_n1;
end
for i=1:6
for j=1:6
fun=#(x,y)...
(p_i_x(x_hat(x,y),y_hat(x,y),i))*((Y_n3-Y_n1)/J_n)+...
(p_i_y(x_hat(x,y),y_hat,i))*((Y_n1-Y_n2)/J_n)*...
(p_i_x(x_hat(x,y),y_hat(x,y),j)*((Y_n3-Y_n1)/J_n)+...
(p_i_y(x_hat(x,y),y_hat(x,y),j))*((Y_n1-Y_n2)/J_n))+...
((p_i_x(x_hat(x,y),y_hat(x,y),i))*((X_n1-X_n3)/J_n)+...
(p_i_y(x_hat(x,y),y_hat(x,y),i))*((X_n2-X_n1)/J_n))*...
((p_i_x(x_hat(x,y),y_hat(x,y),j))*((X_n1-X_n3)/J_n)+...
(p_i_y(x_hat(x,y),y_hat(x,y),j))*(X_n2-X_n1)/J_n);
r=integral2(fun,xmin,xmax,ymin,ymax);
A(T_b(j,n),T_b(i,n))=A(T_b(j,n),T_b(i,n))+r;
end
end
end
please ignore the chinese comment!
the above main programme also use the following two programmes
function r=p_i_x(x,y,i)
i_x=[4*x+4*y-3,4*x-1,0,-8*x-4*y+4,4*y,-4*y];
r=i_x(i);
end
function r=p_i_y(x,y,i)
i_y=[4*x+4*y-3,0,4*y-1,-4*x,4*x,-8*y-4*x+4];
r=i_y(:,i);
end
when I try to execute fun(0,1), "Undefined operator '*' for input arguments of type 'function_handle'." occurs I don't know how to fix this and other answers don't help, how to fix this?

Why is the error 'not enough input arguments' occuring

I was trying to simulate this piece of code as part of simulation when this error occured.I am not able to understand how num_cycles is calculated in the above code.The code is part of a set of equations governing the time delay required for gate signal for a mosfet
function [Tsa,Tsb]=gentimeseries(m1,tp,num_cycles)
num_angles=(360/5)+1;
ts=(tp/(num_angles*50));
ts
tsa=[];
tsb=[];
alpha1=0;
ta1=0;
tb1=0;
tsa=[tsa;[ta1,0]];
tsb=[tsb;[tb1,0]];
while alpha1<=360
[wa,da,wb,db]=svp(m1,alpha1);
da=da*ts/100;
db=db*ts/100;
wa=wa*ts/100;
wb=wb*ts/100;
i=1;
while i<=50
ta1=ta1+da;
tsa=[tsa;[ta1,1]];
ta1=ta1+wa;
tsa=[tsa;[ta1,0]];
ta1=ta1+da;
tsa=[tsa;[ta1,0]];
i=i+1;
end
i=1;
while i<=50
tb1=tb1+db;
tsb=[tsb;[tb1,1]];
tb1=tb1+wb;
tsb=[tsb;[tb1,0]];
tb1=tb1+db;
tsb=[tsb;[tb1,0]];
i=i+1;
end
alpha1=alpha1+5;
end
x=tsa(:,2);
y=tsa(:,1);
plot(y,x,'.')
Tsa=timeseries(tsa(:,2),tsa(:,1));
Tsb=timeseries(tsb(:,2),tsb(:,1));
n_cycle=1;
while n_cycle<=num_cycles
tta=Tsa;
tta.time=tta.time+ta1;
Tsa=Tsa.append(tta);
ttb=Tsb;
ttb.time=ttb.time+tb1;
Tsb=Tsb.append(ttb);
ta1=ta1+ta1;
tb1=tb1+tb1;
n_cycle=n_cycle+1;
end
end

Syntax error when running matlab code

I downloaded some matlab code, when trying to run a test file, it ends finding a syntax error, this one here:
parse error near line 151 of file /media/34GB/escola/efficientLBP/pixelwiseLBP.m
syntax error
>>>> [minLBP, ~]=sortNeighbours( binaryWord, weigthVec,...
^
The code it is:
else % if iChan==1 || isChanWiseRot
[minLBP, ~]=sortNeighbours( binaryWord, weigthVec,...
iCircShiftMinLBP(iRow, iCol) );
What is wrong here? Thanks for the time.
P.S: EDITED
Here is the extra code requested:
for iRow=( filtDimsR(1)+1 ):( padImgSize(1)-filtDimsR(1) )
for iCol=( filtDimsR(2)+1 ):( padImgSize(2)-filtDimsR(2) )
subImg=chanImgPad(iRow+( -filtDimsR(1):filtDimsR(1) ),...
iCol+( -filtDimsR(2):filtDimsR(2) ));
% find differences between current pixel, and it's neighours
diffVec=sum(sum( filtR.*repmat(subImg,[1, 1, nNeigh]) ));
diffVec=roundnS(diffVec, nEps);
binaryWord=( diffVec(:)>=0 );
if isRotInv
if iChan==1 || isChanWiseRot % go through all posible binary
% word combination, finding minimal LBP
[minLBP, iCircShiftMinLBP(iRow, iCol)]=...
sortNeighbours(binaryWord, weigthVec);
else % if iChan==1 || isChanWiseRot
[minLBP, ~]=sortNeighbours( binaryWord, weigthVec,...
iCircShiftMinLBP(iRow, iCol) );
end % if iChan==1 || isChanWiseRot
else
minLBP=weigthVec*binaryWord;
end % if isRotInv
currChanLBP(iRow, iCol)=cast( minLBP, outClass); % convert to decimal.
end % for iCol=(1+filtDimsR(2)):(imgSize(2)-filtDimsR(2))
% Present waitbar- a bar with progress, time passed and time remaining
waitbarTimeRemaining(hWaitbar, hTicPixelwiseLBP,...
(( iRow-filtDimsR(1) )+nRows*(iChan-1))/(nClrChans*nRows));
end % for iRow=(1+filtDimsR(1)):(imgSize(1)-filtDimsR(1))

Plotting 3 variables against 1 variable

I have a code that computes the max value. this code consists of four variables www is the function of a,b, and c labaled xx, yy, and zz respectively, so my question is how can i plot www against xx,yy, and zz? Thanks for helping
objfun file
function f=W4qubit(x,a,b,c,d)
c1=-cos(x(1))*(cos(x(5))*(cos(x(9))*(cos(x(13))-cos(x(15)))-cos(x(11))*(cos(x(13))+cos(x(15))))+...
cos(x(7))*(cos(x(11))*(cos(x(15))-cos(x(13)))-cos(x(9))*(cos(x(13))+cos(x(15)))))-...
cos(x(3))*(cos(x(5))*(cos(x(11))*(cos(x(15))-cos(x(13)))-cos(x(9))*(cos(x(13))+cos(x(15))))-...
cos(x(7))*(cos(x(9))*(cos(x(13))-cos(x(15)))-cos(x(11))*(cos(x(13))+cos(x(15)))));
c2=cos(x(1))*(cos(x(5))*(sin(x(9))*(sin(x(13))*cos(x(10)-x(14))-sin(x(15))*cos(x(10)-x(16)))-...
sin(x(11))*(sin(x(13))*cos(x(12)-x(14))+sin(x(15))*cos(x(12)-x(16))))+...
cos(x(7))*(sin(x(11))*(sin(x(15))*cos(x(12)-x(16))-sin(x(13))*cos(x(12)-x(14)))-...
sin(x(9))*(sin(x(13))*cos(x(10)-x(14))+sin(x(15))*cos(x(10)-x(16)))))+...
cos(x(3))*(cos(x(5))*(sin(x(11))*(sin(x(15))*cos(x(12)-x(16))-sin(x(13))*cos(x(12)-x(14)))-...
sin(x(9))*(sin(x(13))*cos(x(10)-x(14))+sin(x(15))*cos(x(10)-x(16))))-...
cos(x(7))*(sin(x(9))*(sin(x(13))*cos(x(10)-x(14))-sin(x(15))*cos(x(10)-x(16)))-...
sin(x(11))*(sin(x(13))*cos(x(12)-x(14))+sin(x(15))*cos(x(12)-x(16)))));
c3=cos(x(1))*(sin(x(5))*(cos(x(9))*(sin(x(13))*cos(x(6)-x(14))-sin(x(15))*cos(x(6)-x(16)))-...
cos(x(11))*(sin(x(13))*cos(x(6)-x(14))+sin(x(15))*cos(x(6)-x(16))))+...
sin(x(7))*(cos(x(11))*(sin(x(15))*cos(x(8)-x(16))-sin(x(13))*cos(x(8)-x(14)))-...
cos(x(9))*(sin(x(13))*cos(x(8)-x(14))+sin(x(15))*cos(x(8)-x(16)))))+...
cos(x(3))*(sin(x(5))*(cos(x(11))*(sin(x(15))*cos(x(6)-x(16))-sin(x(13))*cos(x(6)-x(14)))-...
cos(x(9))*(sin(x(13))*cos(x(6)-x(14))+sin(x(15))*cos(x(6)-x(16))))-...
sin(x(7))*(cos(x(9))*(sin(x(13))*cos(x(8)-x(14))-sin(x(15))*cos(x(8)-x(16)))-...
cos(x(11))*(sin(x(13))*cos(x(8)-x(14))+sin(x(15))*cos(x(8)-x(16)))));
c4=cos(x(1))*(sin(x(5))*(sin(x(9))*cos(x(6)-x(10))*(cos(x(13))-cos(x(15)))-sin(x(11))*cos(x(6)-x(12))*(cos(x(13))+cos(x(15))))+...
sin(x(7))*(sin(x(11))*cos(x(8)-x(12))*(cos(x(15))-cos(x(13)))-sin(x(9))*cos(x(8)-x(10))*(cos(x(13))+cos(x(15)))))+...
cos(x(3))*(sin(x(5))*(sin(x(11))*cos(x(6)-x(12))*(cos(x(15))-cos(x(13)))-sin(x(9))*cos(x(6)-x(10))*(cos(x(13))+cos(x(15))))-...
sin(x(7))*(sin(x(9))*cos(x(8)-x(10))*(cos(x(13))-cos(x(15)))-sin(x(11))*cos(x(8)-x(12))*(cos(x(13))+cos(x(15)))));
c5=sin(x(1))*(cos(x(5))*(cos(x(9))*(sin(x(13))*cos(x(2)-x(14))-sin(x(15))*cos(x(2)-x(16)))-...
cos(x(11))*(sin(x(13))*cos(x(2)-x(14))+sin(x(15))*cos(x(2)-x(16))))+...
cos(x(7))*(cos(x(11))*(sin(x(15))*cos(x(2)-x(16))-sin(x(13))*cos(x(2)-x(14)))-...
cos(x(9))*(sin(x(13))*cos(x(2)-x(14))+sin(x(15))*cos(x(2)-x(16)))))+...
sin(x(3))*(cos(x(5))*(cos(x(11))*(sin(x(15))*cos(x(4)-x(16))-sin(x(13))*cos(x(4)-x(14)))-...
cos(x(9))*(sin(x(13))*cos(x(4)-x(14))+sin(x(15))*cos(x(4)-x(16))))-...
cos(x(7))*(cos(x(9))*(sin(x(13))*cos(x(4)-x(14))-sin(x(15))*cos(x(4)-x(16)))-...
cos(x(11))*(sin(x(13))*cos(x(4)-x(14))+sin(x(15))*cos(x(4)-x(16)))));
c6=sin(x(1))*(cos(x(5))*(sin(x(9))*cos(x(2)-x(10))*(cos(x(13))-cos(x(15)))-sin(x(11))*cos(x(2)-x(12))*(cos(x(13))+cos(x(15))))+...
cos(x(7))*(sin(x(11))*cos(x(2)-x(12))*(cos(x(15))-cos(x(13)))-sin(x(9))*cos(x(2)-x(10))*(cos(x(13))+cos(x(15)))))+...
sin(x(3))*(cos(x(5))*(sin(x(11))*cos(x(4)-x(12))*(cos(x(15))-cos(x(13)))-sin(x(9))*cos(x(4)-x(10))*(cos(x(13))+cos(x(15))))-...
cos(x(7))*(sin(x(9))*cos(x(4)-x(10))*(cos(x(13))-cos(x(15)))-sin(x(11))*cos(x(4)-x(12))*(cos(x(13))+cos(x(15)))));
c7=sin(x(1))*(sin(x(5))*cos(x(2)-x(6))*(cos(x(9))*(cos(x(13))-cos(x(15)))-cos(x(11))*(cos(x(13))+cos(x(15))))-...
sin(x(7))*cos(x(2)-x(8))*(cos(x(11))*(cos(x(15))-cos(x(13)))-cos(x(9))*(cos(x(13))+cos(x(15)))))+...
sin(x(3))*(sin(x(5))*cos(x(4)-x(6))*(cos(x(11))*(cos(x(15))-cos(x(13)))-cos(x(9))*(cos(x(13))+cos(x(15))))-...
sin(x(7))*cos(x(4)-x(8))*(cos(x(9))*(cos(x(13))-cos(x(15)))-cos(x(11))*(cos(x(13))+cos(x(15)))));
A2=2*a*b;
A3=2*a*c;
A4=2*b*c;
A5=2*a*d;
A6=2*b*d;
A7=2*c*d;
f1=c1+A2*c2+A3*c3+A4*c4+A5*c5+A6*c6+A7*c7;
f=-(f1^2);
my main file of the code
clear
close
clc
%x=[x(1),x(2),x(3),x(4),x(5),x(6),x(7),x(8),x(9),x(10),x(11),x(12),x(13),x(14),x(15),x(16)]; % angles;
lb=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
ub=[pi,2*pi,pi,2*pi,pi,2*pi,pi,2*pi,pi,2*pi,pi,2*pi,pi,2*pi,pi,2*pi];
options = optimoptions(#fmincon,'TolX',10^-12,'MaxIter',1500,'MaxFunEvals',10^8,'Algorithm','sqp','TolFun',10^-8);
a=0:0.1:1;
b=0:0.1:1;
c=0:0.1:1;
w=NaN(length(a),length(b),length(c));
ww=NaN(length(a),length(b),length(c));
www=NaN(length(a),length(c));
for k=1:100
x0=rand([1,16]).*ub*.9986;%7976
for i=1:length(a)
for j=1:length(b)
for l=1:length(c)
dhelp=1-(a(i)^2)-(b(j)^2)-(c(l)^2);
if (dhelp>0 || dhelp==0)
d=sqrt(dhelp);
[~,fval]=fmincon(#(x)W4qubit(x,a(i),b(j),c(l),d),x0,[],[],[],[],lb,ub,[],options);
w(i,j,l)=sqrt(-fval);
else
w(i,j,l)=nan;
end
ww=max(w,ww);
end
end
end
end
www=max(ww,[],3);
yy=b.^2;xx=a.^2;zz=c.^2;
meshc(xx,yy,www)
grid on
zlabel('\fontname{Times New Roman} M_{max}')
xlabel('\fontname{Times New Roman}\alpha^2')
ylabel('\fontname{Times New Roman}\gamma^2')
%title('fontname{Times New Roman} Maximum of the Svetlichny operator. Method 1 (alpha|0001>+beta|0010>+gamma|1000>)')
Not sure, but doesn't
plot(www,[xx;yy;zz]);
do the job for you? I do not have the optimization toolbox, so I can't test your script. But in principle, this should work.

Compile error of the .f source code in gfortran; reading a text file into 2D array

I have a problem with the following source. When it is compiled in gfortran, it does not work properly and then two error message showed up.
How do I solve this problem?
Any comment would be very helpful.
Thanks in advance.
program driver
integer i,ln,n,e,count,x,a,b,total
character driverid*12,var*12,ch*12
parameter (n=720321)
c parameter (n=55062)
dimension var(n),a(n),b(n)
write(*,*) 'input run id(text)'
read(*,55) driverid
55 format(a)
ln=index(driverid,' ')-1
open(6,file=driverid(1:ln)//'.out',form='formatted'
+,status='unknown')
open(1,file=driverid(1:ln)//'.txt',status='old')
do i=1,2
read(1,*)
end do
read(1,*) (var(i),i=1,n)
close(1)
total=0
count=1
do i=1,n
b(i)=0
read(var(i),*,iostat=e) x
if (e .eq. 0) then
a(count)=x
count=count+1
else
ln=index(var(i),' ')-1
if (var(i)(ln-1:ln-1) .eq. 'r') then
var(i)=var(i)(1:ln-2)
else
var(i)=var(i)(1:ln-1)
end if
read(var(i),'(i)') b(count-1)
end if
end do
do i=1,count
total=total+1+b(i)
end do
do i=1,total
write(6,'(10i)') (a(i),j=1,b(i))
end do
close(6)
end
Error message is following as
$ gfortran driver.f
driver.f:43.23:
read(var(i),'(i)') b(count-1)
1
Error: Nonnegative width required in format string at (1)
driver.f:53.20:
write(6,'(10i)') (a(i),j=1,b(i))
1
Error: Nonnegative width required in format string at (1)
When specifying a format for an integer, you must specify the field width. If you want to have it flexible, you can set it to zero:
write(6, "(10I0)") ...