Problem in integration in an array inside integrand

조회 수: 2 (최근 30일)
Anil Kumar
Anil Kumar 2019년 6월 8일
댓글: Star Strider 2019년 6월 10일
I am getting the error named "Attempted to access R(2); index out of bounds because numel(R)=1." in the below code. can anyone help me? below is my code.
Nd = 1e24;
N = 100;
e = 12*1e-12;
% R = [10e-9,20e-9,30e-9,40e-9];
R = [1e-9,2e-9,3e-9,4e-9]
Et_Ef = -1.01*1.6*1e-19;
K = 1.3807e-23;
T = linspace(450,600,N);
Nt = linspace(1e14,1e17,N);
q = 1.6e-19;
Ld = 1.97*10e-9
for j = 1:numel(R)
for i= 1:N
funct = @(R,phi0) (4*pi*(Nd-Nd*exp(-phi0))*R.^2);
equation = @(phi0)integral(@(R)funct(R,phi0),0,R(j))-(4*pi*R(j)^2*Nt(i)/(1+2*exp((Et_Ef + K.*T(i) .*(phi0 + (1/6)*(R(j)/Ld)^2))./(K.*T(i)))));
%equation = @(phi0)integral(@(r)funct(r,phi0),0,R)-(4*pi*R^2*Nt(i)/(1+2*exp((Et_Ef + K*T *(phi0 + (1/6)*(R/Ld)^2))/(K*T))));
phi0val(j,i) = fzero(equation,40);
funneff= @(R)1./(4/3.*pi.*R(j).^3).*(Nd.*exp(-phi0val(j,i))).*R(j).^2
neff(j,i)= (1./(4./3.*pi.*R(j).^3)).*4.*pi.*integral(funneff,0,R(j),'Arrayvalued',true)
end
end
plot(neff(1,:),phi0val(1,:))
plot(neff(2,:),phi0val(2,:))
plot(neff(2,:),phi0val(2,:))
plot(neff(2,:),phi0val(2,:))

채택된 답변

Star Strider
Star Strider 2019년 6월 8일
편집: Star Strider 2019년 6월 8일
You are using ‘R’ in too many contexts. It is a vector, however it is also an argument to your anonymous functions, and anonymous functions pick up variables from the workspace. Changing the anonymous functions argument ‘R’ to ‘Rv’ allows your code to run:
neff = zeros(numel(R),N);
phi0val = zeros(numel(R),N);
for j = 1:numel(R)
for i= 1:N
funct = @(Rv,phi0) (4*pi*(Nd-Nd*exp(-phi0))*Rv.^2);
equation = @(phi0)integral(@(Rv)funct(Rv,phi0),0,R(j))-(4*pi*R(j)^2*Nt(i)/(1+2*exp((Et_Ef + K.*T(i) .*(phi0 + (1/6)*(R(j)/Ld)^2))./(K.*T(i)))));
%equation = @(phi0)integral(@(r)funct(r,phi0),0,R)-(4*pi*R^2*Nt(i)/(1+2*exp((Et_Ef + K*T *(phi0 + (1/6)*(R/Ld)^2))/(K*T))));
phi0val(j,i) = fzero(equation,40);
funneff= @(Rv)1./(4/3.*pi.*Rv.^3).*(Nd.*exp(-phi0val(j,i))).*Rv.^2;
neff(j,i)= (1./(4./3.*pi.*R(j).^3)).*4.*pi.*integral(funneff,0,R(j),'Arrayvalued',true);
end
end
figure
hold all
for k = 1:size(neff,1)
plot(neff(k,:),phi0val(k,:))
end
hold off
expstr = @(x) [x(:).*10.^ceil(-log10(abs(x(:)))) floor(log10(abs(x(:))))];
Rexp = expstr(R)
hl = legend(sprintfc('$R\\ =\\ %2d\\times10^{%d}$',Rexp));
hl.Interpreter = 'latex';
Check to be certain this does what you want to to do.
The Plot —
Problem in integration in an array inside integrand - 2019 06 08.png
EDIT — Added plot image.
  댓글 수: 2
Anil Kumar
Anil Kumar 2019년 6월 10일
Nicely explained ! thank you sir!
Star Strider
Star Strider 2019년 6월 10일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by