Query regarding for loop

2 次查看(过去 30 天)
Amy Topaz
Amy Topaz 2022-3-14
I have the below logic
T1 = linspace(10, 800, 10);
for range1 = 1:numel(T1)
a = 2*Mc*(((mde*kb*T1(range1))/(2*pi*(hbar^2)))^(3/2));
b = 2*(((mdh*kb*T1(range1))/(2*pi*(hbar^2)))^(3/2));
c = (kb*T1(range1))/e;
d = 1.17 - (8.096e-8*((T1(range1))^2))/(T1(range1) + 800);
ec1 = d;
ed1 = ec1 + Edi;
eq = @(EF1) ((a)*exp(-(ec1-EF1)/c)) + ((NA1)/(1+4*exp(-(EF1-ea1)/c))) - (((b)*exp(-(EF1-ev1)/c)) + ((ND1)/(1+2*exp(-(ed1-EF1)/c))));
x = [0 2];
Eflevel1(range1) = fzero(eq, x);
end
%How can I write the above lines without using for loop? Is that possible?
%Assume all other values as constants.
  2 个评论
AndresVar
AndresVar 2022-3-14
编辑:AndresVar 2022-3-14
Could you write the equation in vector form like Ax=b?
as it is now, you can put most stuff outside loop except fzero.
Rena Berman
Rena Berman 2022-4-8

(Answers Dev) Restored edit

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2022-3-14
Q = @(v) sym(v);
syms Edi EF1 e ea1 ev1 hbar kb Mc mde mdh NA1 ND1 T1
assume([Edi, EF1, e, ea1, ev1, hbar, kb, Mc, mde, mdh, NA1, ND1, T1] ~= 0)
Pi = Q(pi);
range1 = 1;
T1vals = linspace(10, 800, 10);
a = 2*Mc*(((mde*kb*T1(range1))/(2*Pi*(hbar^2)))^(3/2));
b = 2*(((mdh*kb*T1(range1))/(2*Pi*(hbar^2)))^(3/2));
c = (kb*T1(range1))/e;
d = Q(1.17) - (Q(8.096e-8)*((T1(range1))^2))/(T1(range1) + Q(800));
ec1 = d;
ed1 = ec1 + Edi;
eq = ((a)*exp(-(ec1-EF1)/c)) + ((NA1)/(1+4*exp(-(EF1-ea1)/c))) - (((b)*exp(-(EF1-ev1)/c)) + ((ND1)/(1+2*exp(-(ed1-EF1)/c))));
sol = solve(eq, EF1, 'returnconditions', true)
sol = struct with fields:
EF1: (T1*kb*(log(z1) + pi*k*2i))/e parameters: [k z1] conditions: ~in((e*((T1*kb*(log(z1) + pi*k*2i))/e - (T1*kb*log(-4*exp((e*ea1)/(T1*kb))))/e)*1i)/(2*T1*kb*pi), 'integer') & in(k, 'integer') & log(z1) + pi*k*2i ~= 0 & ~in((e*((T1*kb*(log(z1) + pi*k*2i))/e - (T1*kb*log(-(exp(-(764645580906253*T1*e)…
sc = subs(sol.conditions, sol.parameters(1), 0)
sc = 
sz = solve(sc, sol.parameters(2))
Warning: Solutions are only valid under certain conditions. To include parameters and conditions in the solution, specify the 'ReturnConditions' value as 'true'.
sz = 
sol_EF1 = subs(sol.EF1, {sol.parameters(1), sol.parameters(2)}, {0, sz})
sol_EF1 = 
  1 个评论
Walter Roberson
Walter Roberson 2022-3-14
The solutions involve roots of an equation of degree 4. I believe it is a polynomial, so I believe that explicit solutions are possible -- though they would be very long !

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by