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 次查看(过去 30 天)
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 个评论
Geoff Hayes
Geoff Hayes 2014-7-20
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;
jayash
jayash 2014-7-20
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...

请先登录,再进行评论。

采纳的回答

Roger Stafford
Roger Stafford 2014-7-20
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.

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by