Query regarding for loop

조회 수: 1 (최근 30일)
Amy Topaz
Amy Topaz 2022년 3월 14일
편집: Rena Berman 2022년 4월 8일
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 !

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by