Is there any way that I can optimise this code. It takes lot of time to run and for larger iteration like 'nc=10e6' its stops stating runtime error.

1 view (last 30 days)
jayash
jayash on 20 Jul 2014
Commented: jayash on 20 Jul 2014
clear all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Simulation of transmission signal decay from a high Finesse Fabry-Perot cavity
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Author-Jayash
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nc=1000000; % no. of cycles inside the cavity
t=1;
Finesse=140000;
%x=-5e4:1:5e4;
ni=1;
f=1/t;
while ni < nc
syms x n r fsr
ESm = symsum((r)^(2*n - 2).*exp(-1i*(x./(2*fsr))*(n - 1)),n,0,ni); % summation of signal(s) to be deducted in each cycle.
R = 0.9999775603; % Reflectivity of cavity mirrors
T = 1 - R;
x = sqrt(R);
r=x;
fsr=10e9;
Itn=1./(1+(Finesse.*((sin(f./(2*fsr)))^2))); % Transmission Intensity from Cavity
ESe = T.*ESm;
ISe = ESe.*conj(ESe);
Itn1=Itn-ISe; % Decay in each cycle
%plot(Itn1,t);
plot(t,double(subs(Itn1)),'*');
hold on
ns=ni+10000;
ni=ns;
t=t+ni.*(10e-9);
end
  2 Comments
jayash
jayash on 20 Jul 2014
lot of time implied hours,I mean more than 5-6 hours for nc=10e6. For nc=1000 it takes 2 mins.
There was some typo mistakes while pasting the code.
yeah my bad it is ni=ni+10000...

Sign in to comment.

Accepted Answer

Roger Stafford
Roger Stafford on 20 Jul 2014
The use of 'symsum' repeatedly in your while-loop could be slowing you down. After all, what you have there is a geometric series and there is an easy formula for its sum. See:
http://en.wikipedia.org/wiki/Geometric_series
Just use that formula for 'ESm' instead of calling on 'symsum' and see if it doesn't go a lot faster.
In fact I would remove all references to symbolic variables in your code. They can slow down computations considerably.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by