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.

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

When you say that it takes a lot of time to run, what exactly does that mean? More than a few minutes, hours, more?
When nc=1000000, you say that its stops stating runtime error? Please include the full error message that you observe.
Note that there are a couple of errors in the above code
fsr=
if true
% code
end10e9;
What should fsr be initialized to? Why is there a 10e9?
The code
ns=ni+10000;
ni=ns;
can be replaced by
ni = ni + 10000;
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

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)

Asked:

on 20 Jul 2014

Commented:

on 20 Jul 2014

Community Treasure Hunt

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

Start Hunting!