for habs=0:.25:1
for pabs=0:.2:1
for beta=0:pi/2:2*pi
for alpha=0:pi/5:2*pi
H=habs*(cos(beta)+i*sin(beta));
p=pabs*(cos(alpha)+i*sin(alpha));
W=abs(H)^2*abs(p)*(2*exp(i*(alpha-beta))+ exp(-i*(alpha-beta)))*exp(i*beta);
plot(real(W+H),imag(W+H),'o','MarkerSize',4)
hold on
end
end
end
end
rea=zeros(1650,1);
ima=zeros(1650,1);
k=0;
for habs=0:.25:1
for pabs=0:.2:1
for beta=0:pi/2:2*pi
for alpha=0:pi/5:2*pi
k=k+1;
H=habs*(cos(beta)+i*sin(beta));
p=pabs*(cos(alpha)+i*sin(alpha));
W=abs(H)^2*abs(p)*(2*exp(i*(alpha-beta))+ exp(-i*(alpha-beta)))*exp(i*beta);
%plot(real(W+H),imag(W+H),'o','MarkerSize',4)
rea(k,1)=real(W+H);
ima(k,1)=imag(W+H);
%hold on
end
end
end
end
scatter(rea,ima);
Note: The idea is using Vectorization for speeding up. You can continue try to speed up more.
"Peter Schreiber" <schreibe...@gmail.com> wrote in message <gs7tj1$gt8$1...@fred.mathworks.com>...
cnt = 1; % No pre-allocation for such a small problem.
for habs = 0:.25:1
for pabs = 0:.2:1
for beta = 0:pi/2:2*pi
for alpha = 0:pi/5:2*pi
H = habs*(cos(beta)+i*sin(beta));
p = pabs*(cos(alpha)+i*sin(alpha));
W = abs(H)^2*abs(p)*(2*exp(i*alpha)+ exp(-i*(alpha-2*beta)));
x(cnt) = real(W+H);
y(cnt) = imag(W+H);
cnt = cnt+1;
end
end
end
end
plot(x,y,'o','MarkerSize',4)
cnt = 1;
for habs = 0:.25:1
for pabs = 0:.2:1
for beta = 0:pi/2:2*pi
for alpha = 0:pi/5:2*pi
etb = exp(i*beta);
eta = exp(i*alpha);
x(cnt) = habs*(habs*abs(etb)^2*pabs*abs(eta)*(2*eta+ etb^2/eta) + etb);
cnt = cnt+1;
end
end
end
end
plot(real(x),imag(x),'o','MarkerSize',4)
Besides:
You can also use command profile to find the bottleneck your computation program.
For example:
profile on;
profile clear;
for habs=0:.25:1
for pabs=0:.2:1
for beta=0:pi/2:2*pi
for alpha=0:pi/5:2*pi
H=habs*(cos(beta)+i*sin(beta));
p=pabs*(cos(alpha)+i*sin(alpha));
W=abs(H)^2*abs(p)*(2*exp(i*(alpha-beta))+ exp(-i*(alpha-beta)))*exp(i*beta);
plot(real(W+H),imag(W+H),'o','MarkerSize',4)
hold on
end
end
end
end
profile report;
Through the report, you can find the bottleneck is using 'plot' and 'hold on' many times. Then you may think about how to use Preallocation and Vectorization to reduce the cpu time.
Perhaps as follows. It requires no for-loops and only a single plot command
[habs,pabs,beta,alpha]=ndgrid(0:.25:1,0:.2:1, 0:pi/2:2*pi,0:pi/5:2*pi);
H=habs.*(cos(beta)+i*sin(beta));
p=pabs.*(cos(alpha)+i*sin(alpha));
W=abs(H).^2.*abs(p).*(2*exp(i*(alpha-beta))+ exp(-i*(alpha-beta))).*exp(i*beta);
W=W(:);
H=H(:);