How can i improve my code

1 view (last 30 days)
Tai-i
Tai-i on 30 Jul 2012
syms E Eg V g;
z=[ ];
y=[ ];
c=3 .*(10.^8);
h=4.1361e-015;
k=8.62E-5;
Tc=300;
for Ei=0.5:0.05:1.3;
z=[ ];
for Eg=1.4:0.01:3.2;
Ec=Eg-Ei;
a = quad( @(E)(E.^2) ./ (exp(E./0.5172)-1), Eg , 100);
aa= quad( @(E)(E.^2) ./ (exp(E./0.5172)-1), Ec , Eg);
for Ucv=Eg-.2:0.005:Eg-.01;
for Uiv=Ei-.1:.005:Ei-.01;
Pin = 1.4458e+026 ;
Uci=Ucv-Uiv;
b = quad(@(E)(E.^2) ./ (exp(( E-Ucv )./0.0259)-1),Eg,100);
bb= quad(@(E)(E.^2) ./ (exp(( E-Uci )./0.0259)-1),Ec,Eg);
if abs(quad( @(E)(E.^2) ./ (exp(E./0.5172)-1), Ei , Ec)- quad(@(E)(E.^2) ./ (exp(( E-Uiv )./0.0259)-1),Ei,Ec)-...
aa+bb) <= 10^-3
P = ( 2.*Ucv./6.3682e-027 ) .* ( a + aa - b - bb ) ./ Pin
z = [z P]
m = max(z)
end
end
end
end
y=[y m]
end
Ei=0.5:0.05:1.3;
plot(Ei,y,'b')
xlabel('Ei');
ylabel('η[%]');
it have to spend 4~5 hours , how can i improve so that more efficiency ?

Answers (3)

Jan
Jan on 30 Jul 2012
I did not work with symbolic math in Matlab yet. Therefore I have a silly question:
syms E Eg
...
for Eg=1.4:0.01:3.2
...
b = quad(@(E)(E.^2) ./ (exp(( E-Ucv )./0.0259)-1),Eg,100)
  1. Is is the symbolic type for Eg useful, when it is a counter in a FOR loop? Or is the type ignored and Eg is a double?
  2. When quad operates on an anonymous function, which contains a symbolic variable, is the quadrature computed symbolically?
  3. Isn't it possible and much faster to solve the integral over an exponential function manually?
  4. The repeated growing of z and y is a bad idea, look for "pre-allocation" in this forum. Even MLint should display a corresponding warning, which should be considered carefully. But I assume, that profile will show, that this is not the bottleneck, but the symbolic (?) quad s are more expensive.
  1 Comment
Walter Roberson
Walter Roberson on 30 Jul 2012
When a name is used in an anonymous function dummy argument, the situation is equivalent to using it as an input argument name in a "real" function -- that is, whatever value or type the name has in the surrounding expression is ignored.
When a name has been declared through syms and then that same name is used as the loop index variable in a "for" loop, the names existence as a symbolic variable is forgotten (except that if any constraints had been put on the name, the constraints might be properly erased.)

Sign in to comment.


Walter Roberson
Walter Roberson on 30 Jul 2012
One kind of thing to look for:
The code line
b = quad(@(E)(E.^2) ./ (exp(( E-Ucv )./0.0259)-1),Eg,100);
does not depend upon the value of Uiv, so you can calculate the value before you start the "for Uiv" loop.
  1 Comment
Tai-i
Tai-i on 30 Jul 2012
it doesn't have significant work ...

Sign in to comment.


Rahul Kashyap
Rahul Kashyap on 30 Jul 2012
try to make the divisions smaller. you have the following statements:
for Ei=0.5:0.05:1.3;
for Eg=1.4:0.01:3.2;
for Ucv=Eg-.2:0.005:Eg-.01;
for Uiv=Ei-.1:.005:Ei-.01;
replace the above lines with:
for Ei=0.5:0.5:1.3;
for Eg=1.4:0.1:3.2;
for Ucv=Eg-.2:0.5:Eg-.01;
for Uiv=Ei-.1:.5:Ei-.01;
this will decrease the iterations. although, its a trade off between accuracy but you should give it a try. if we use your code, the loop will run 17x181x39x19 times, but if you try my statements it will run for 19x2 times. give it a try :)

Tags

Community Treasure Hunt

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

Start Hunting!