Help needed for below error
Show older comments
Kindly help with the below error for the below mentioned code. All other values are constant

scale1 = 100;
T1 = linspace(10, 2000, scale1);
Ta = rdivide(1,T1);
ND1 = 1e17;
NA1 = 1e5;
%Computing n and p values to be plotted
hole1 = ones(scale1,1)*0;
Eflevel1 = ones(scale1,1)*0;
electron1 = ones(scale1,1)*0;
NC1 = 2*Mc*(((mde*kb*T1(1:100))/(2*pi*(hbar^2))).^(3/2));
NV1 = 2*(((mdh*kb*T1(1:100))/(2*pi*(hbar^2))).^(3/2));
kbT1 = (kb*T1(1:100))/e;
ev1 = 0;
ea1 = ev1 + Eai;
eg1 = 1.17 - (4.73e-4*((T1(1:100)).^2))./(T1(1:100) + 636);
ec1 = eg1;
ed1 = ec1 + Edi;
x2 = [0 1.73];
for range1 = 1:prod(size(T1))
eq2 = @(EF1) ((NC1(range1))*exp(-(ec1(range1)-EF1)/kbT1(range1))) + ((NA1)/(1+4*exp(-(EF1-ea1(range1))/kbT1(range1)))) - (((NV1(range1))*exp(-(EF1-ev1(range1))/kbT1(range1))) + ((ND1)/(1+2*exp(-(ed1(range1)-EF1)/kbT1(range1)))));
Eflevel1(range1) = fsolve(eq2, x2);
y1 = Eflevel1(range1);
x2 = y1.';
s11 = Eflevel1(:,1,:);
electron1(range1) = (NC1(range1))*exp(-(ec1(range1)-Eflevel1(range1))/(kbT1(range1)));
s12 = electron1(:,1,:);
hole1(range1) = (NV1(range1))*exp(-(Eflevel1(range1)-ev1(range1))/(kbT1(range1)));
s13 = hole1(:,1,:);
end
3 Comments
John D'Errico
on 16 Mar 2022
So others do not deserve just as fast attention to their problems?
Amy Topaz
on 16 Mar 2022
Rik
on 29 Mar 2022
I recovered the removed content from the Google cache (something which anyone can do). Editing away your question is very rude. Someone spent time reading your question, understanding your issue, figuring out the solution, and writing an answer. Now you repay that kindness by ensuring that the next person with a similar question can't benefit from this answer.
Answers (2)
Walter Roberson
on 16 Mar 2022
x2 = [0 1.73];
row vector
Eflevel1(range1) = fsolve(eq2, x2);
vector is used as the example parameters for fsolve, so eq2 is going to be passed something the same length as x2.
eq2 = @(EF1) ((NC1(range1))*exp(-(ec1(range1)-EF1)/kbT1(range1))) + ((NA1)/(1+4*exp(-(EF1-ea1(range1))/kbT1(range1)))) - (((NV1(range1))*exp(-(EF1-ev1(range1))/kbT1(range1))) + ((ND1)/(1+2*exp(-(ed1(range1)-EF1)/kbT1(range1)))));
That vector becomes EF1 on input. You use that vector EF1 in several places, including in
/(1+4*exp(-(EF1-ea1(range1))/kbT1(range1)))
so the right hand side of the / is a row vector.
But remember that the / operator is matrix-right-divide, mrdivide, with A/B being similar to A*pinv(B) except with some size constraints. And the size constraints are such that row vector / row vector is not permitted .
6 Comments
Amy Topaz
on 16 Mar 2022
Walter Roberson
on 16 Mar 2022
No... you haven't defined how it should work, so I cannot recommend a correction.
If you switch to element-by-element processing using ./ instead of / then your eq2 would return a vector of two elements. fsolve() can deal with a vector of two elements being returned: it tries to adjust the values so that both values become 0. However, since nothing else in your expression modifies the expression differently for the two elements, it follows that the same value (0) would be returned if you managed to find a 0 for one of the elements and then set the other element to the same value. So at present, the calculations cannot differentiate between the two values passed in to the function.
Notice you are assigning the result of fsolve() to the scalar location Eflevel1(range1), implying that you expect fsolve() to return a scalar. But fsolve() is always going to return a non-scalar whenever the example input (x2) is non-scalar, since fsolve() returns the array of model coefficients that lead the function to have all-zero output.
Amy Topaz
on 16 Mar 2022
Amy Topaz
on 16 Mar 2022
Walter Roberson
on 16 Mar 2022
fzero can only deal with a single variable, not a vector of variables, so it would be an error to use fzero with that x2.
Amy Topaz
on 16 Mar 2022
Torsten
on 16 Mar 2022
Since it seems that you only solve for a single unknown, change
x2 = [0 1.73];
to
x2 = 1.73;
Then your code should be
for range1 = 1:prod(size(T1))
eq2 = @(EF1) NC1(range1)*exp(-(ec1(range1)-EF1)/kbT1(range1)) + NA1/(1+4*exp(-(EF1-ea1(range1))/kbT1(range1))) - (NV1(range1)*exp(-(EF1-ev1(range1))/kbT1(range1)) + (ND1/(1+2*exp(-(ed1(range1)-EF1)/kbT1(range1)))));
Eflevel1(range1) = fsolve(eq2, x2);
x2 = Eflevel1(range1);
end
y1 = Eflevel1;
x2 = y1.';
s11 = Eflevel1;
electron1 = NC1.*exp(-(ec1-Eflevel1)./kbT1);
s12 = electron1;
hole1= NV1.*exp(-(Eflevel1-ev1)./kbT1);
s13 = hole1;
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!