Info
This question is closed. Reopen it to edit or answer.
How can I fix the Error in Summation: (Attempted to access f(2); index out of bounds because numel (f)=1).
1 view (last 30 days)
Show older comments
I'm attempting to compute a summation using Matlab (code below) corresponding to the attached system of equations. I get the following error:
Attempted to access f(2); index out of bounds because numel(f)=1.
Error in uniform_correction (line 16) f = (x^2)*f(i-2)/i/(i+2)
The following is the code:
function [] = uniform_correction()
u = input('linear attenuation coefficient:');
r = input('radius of cylinder:');
x = 2*u*r;
%N = 59;
%I = 25;
J = 0;
T = 0;
if (x<=18.5)
for i = 1:59
if(i==1)
f = -1*(4/(3*pi))*x;
elseif(i==2)
f = (x^2)/8;
else
f = (x^2)*f(i-2)/i/(i+2);
end
J = J + f;
end
else
for i = 1:25
if (i==1)
y = (3/(x^2))*(1/(x^2));
else
y = ((4*(i-1)-1)/(x^2))*y(i-1);
end
T = T + y;
end
end
J_n = J + 1;
T_n = T + (1/(x^2));
J_n
T_n
if (x<=18.5)
CF = J_n;
else
CF = (4/(pi*x))*(1-T_n);
end
CF
end
0 Comments
Answers (1)
Brendan Hamm
on 31 Mar 2015
You only ever assign a scalar value to f, and continuously overwrite the previous version which was there. Not sure what your intent is here, but I assume it should look more like:
if (x<=18.5)
for i = 1:59
if(i==1)
f(i) = -1*(4/(3*pi))*x; % Index f(i)
elseif(i==2)
f(i) = (x^2)/8; % Index f(i)
else
f(i) = (x^2)*f(i-2)/i/(i+2); % Index f(i)
end
J = J + f(i); % Index
end
else
for i = 1:25
if (i==1)
y(i) = (3/(x^2))*(1/(x^2)); % Index
else
y(i) = ((4*(i-1)-1)/(x^2))*y(i-1); %Index
end
T = T + y(i);
end
end
0 Comments
This question is closed.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!