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:MI2 = I2 + f(i)+f(i+1);endI2 = 4*I2;for i = 3:2:M-1I3 = I3 + f(i);endI3 = 2*I2;endI = (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 approximationI2 = 0; % Initialization variable for second term of approximationI3 = 0; % Initialization variable for third term of approximationI4 = f(M); % Last term of approximationif(length(f)>1)for i = 2:3:M-1I2 = I2 + f(i)+f(i+1);endI2 = 3*I2;for i = 4:3:M-1I3 = I3 + f(i);endI3 = 2*I2;endI = (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>2nfin=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)elsenfin=2*(Integral_Numerical_1_potential(R(1:4),3,a,b)+Integral_Numerical_2(R(4:end),M-3,a,b))/(1-a^2)endend
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