Clear Filters
Clear Filters

Problem in integration in an array inside integrand

2 views (last 30 days)
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,:))

Accepted Answer

Star Strider
Star Strider on 8 Jun 2019
Edited: Star Strider on 8 Jun 2019
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.

More Answers (0)

Categories

Find more on Programming 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!