What is the modification needed in the below code

์กฐํšŒ ์ˆ˜: 1 (์ตœ๊ทผ 30์ผ)
Amy Topaz
Amy Topaz 2022๋…„ 3์›” 17์ผ
๋Œ“๊ธ€: Rena Berman 2022๋…„ 4์›” 8์ผ
Are the below code same? I am getting different results.
%First
scale0 = 1500;
Nai = linspace(1e13,1e19,scale0); %๐‘๐‘Ž:1ร—1013โˆ’1ร—1019๐‘mโˆ’3
Ndi = linspace(1e13,1e19,scale0); %๐‘๐‘‘:1ร—1013โˆ’1ร—1019๐‘mโˆ’3
Na = ones(scale0,1)*Nai;
Nd = Ndi.'*ones(1,scale0);
y = 0;
z = 0;
%Computing Fermi Energy Level
Ef = ones(scale0,1)*0;
while y < 1500
y = y + 1;
while z < 1500
z = z + 1;
eq1 = @(Ef) ((Nc)*exp(-(Ec-Ef)/(kbT))) + ((Na(y,z))/(1 + 4*exp(-(Ef-Ea)/(kbT)))) - ((Nv)*exp(-(Ef-Ev)/(kbT)) + ((Nd(y,z))/(1 + 2*exp(-(Ed-Ef)/(kbT)))));
x1 = [0 10];
Ef(y,z) = fzero(eq1,x1);
end
end
%Second
scale0 = 1500;
Nai = linspace(1e13,1e19,scale0); %๐‘๐‘Ž:1ร—1013โˆ’1ร—1019๐‘mโˆ’3
Ndi = linspace(1e13,1e19,scale0); %๐‘๐‘‘:1ร—1013โˆ’1ร—1019๐‘mโˆ’3
Na = ones(scale0,1)*Nai;
Nd = Ndi.'*ones(1,scale0);
%Computing Fermi Energy Level
Ef = ones(scale0,1)*0;
for y=1:prod(size(Nai))
for z=1:prod(size(Ndi))
eq1 = @(Ef) ((Nc)*exp(-(Ec-Ef)/(kbT))) + ((Na(y,z))/(1 + 4*exp(-(Ef-Ea)/(kbT)))) - ((Nv)*exp(-(Ef-Ev)/(kbT)) + ((Nd(y,z))/(1 + 2*exp(-(Ed-Ef)/(kbT)))));
x1 = [0 10];
Ef(y,z) = fzero(eq1,x1);
end
end
  ๋Œ“๊ธ€ ์ˆ˜: 1
Rena Berman
Rena Berman 2022๋…„ 4์›” 8์ผ
(Answers Dev) Restored edit

๋Œ“๊ธ€์„ ๋‹ฌ๋ ค๋ฉด ๋กœ๊ทธ์ธํ•˜์‹ญ์‹œ์˜ค.

์ฑ„ํƒ๋œ ๋‹ต๋ณ€

Walter Roberson
Walter Roberson 2022๋…„ 3์›” 17์ผ
Look at the first set of code. You initialize z = 0 outside both loops. In the inner loop you add 1 to each z, ending when z >= 1500. Now consider the second iteration of the while y loop, when y increments from 1 to 2. z is still 1500 from the previous while z<1500 loop, because you never reset z to 0. So for the rest of the y iterations, you do no z work.
Side note:
prod(size(Nai))
can be written more efficiently as
numel(Nai)
  ๋Œ“๊ธ€ ์ˆ˜: 3
Walter Roberson
Walter Roberson 2022๋…„ 3์›” 17์ผ
Initialize z within the first while loop.
Amy Topaz
Amy Topaz 2022๋…„ 3์›” 17์ผ
thank you

๋Œ“๊ธ€์„ ๋‹ฌ๋ ค๋ฉด ๋กœ๊ทธ์ธํ•˜์‹ญ์‹œ์˜ค.

์ถ”๊ฐ€ ๋‹ต๋ณ€ (0๊ฐœ)

์นดํ…Œ๊ณ ๋ฆฌ

Help Center ๋ฐ File Exchange์—์„œ MATLAB์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ

ํƒœ๊ทธ

Community Treasure Hunt

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

Start Hunting!

Translated by