Hi guys, I wrote the attached code and would like to make it run faster. Can somebody help me out? Best Regards, Peter
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
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" <schreiber.pete...@gmail.com> wrote in message <gs7tj1$gt...@fred.mathworks.com>... > Hi guys, > I wrote the attached code and would like to make it run faster. > Can somebody help me out? > Best Regards, > Peter
> 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
Simplify the math, then don't call plot and hold each time through.
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)
The math can be even further simplified, which speeds it up some more. The next step would be to start vectorizing to see if that makes a difference.
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)
For reducing computation time, two ways may be useful: Preallocation and Vectorization.
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.
"Peter Schreiber" <schreiber.pete...@gmail.com> wrote in message <gs7tj1$gt...@fred.mathworks.com>... > Hi guys, > I wrote the attached code and would like to make it run faster. > Can somebody help me out? > Best Regards, > Peter
> 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
Perhaps as follows. It requires no for-loops and only a single plot command