MATLAB: Function that calls functions

function in a functionindex-errorintegration

These first two codes are codes for an integration…
Integral 1….
function I=Integral_Numerical_1_potential(f,M,a,b)
h = (b-a)/2;
I1 = f(1); % First term of approximation

I2 = 0; % Initialization variable for second term of approximation

I3 = 0; % Initialization variable for third term of approximation

I4 = f(M); % Last term of approximation

if(length(f)>1)
for i = 2:2:M
I2 = I2 + f(i)+f(i+1);
end
I2 = 4*I2;
for i = 3:2:M-1
I3 = I3 + f(i);
end
I3 = 2*I2;
end
I = (h/3)*(I1+I2+I3+I4);
end
Integral 2…..
function I=Integral_Numerical_2(f,M,a,b)
h = (b-a)/2;
I1 = f(1); % First term of approximation
I2 = 0; % Initialization variable for second term of approximation
I3 = 0; % Initialization variable for third term of approximation
I4 = f(M); % Last term of approximation
if(length(f)>1)
for i = 2:3:M-1
I2 = I2 + f(i)+f(i+1);
end
I2 = 3*I2;
for i = 4:3:M-1
I3 = I3 + f(i);
end
I3 = 2*I2;
end
I = (3*h/8)*(I1+I2+I3+I4);
end
They are both called by the following function on the basis of if the value of m is even integral 1 is used, if the value of m is divisible by 3 it uses integral 2 and if neither of those apply it combines both integrals to find the answer…..
function nfin=FinEfficiency(R,T)
M=length(R)-1;
a=T;
b=1;
nfin=0;
if mod(M,2)==0 && M>2
nfin=2*Integral_Numerical_1_potential(R,M,a,b)/(1-a^2)
elseif mod(M,3)==0 && M>3
nfin=2*Integral_Numerical_2(R,M,a,b)/(1-a^2)
else
nfin=2*(Integral_Numerical_1_potential(R(1:4),3,a,b)+Integral_Numerical_2(R(4:end),M-3,a,b))/(1-a^2)
end
end
I keep getting an error in this last function saying index exceeds the number of array elements (1) in line 12 whenever I try to run it and I'm not sure how to fix it.
line 12 = nfin=2*(Integral_Numerical_1_potential(R(1:4),3,a,b)+Integral_Numerical_2(R(4:end),M-3,a,b))/(1-a^2)

Best Answer

Caveat: I'm guessing.
Replace
nfin=2*(Integral_Numerical_1_potential(R(1:4),3,a,b)+Integral_Numerical_2(R(4:end),M-3,a,b))/(1-a^2)
by
i1 = Integral_Numerical_1_potential(R(1:4),3,a,b);
i2 = Integral_Numerical_2(R(4:end),M-3,a,b);
nfin = 2*( i1 + i2 )./(1-a.^2);
What values of R and T do you use when calling FinEfficiency() ?