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)

 採用された回答

per isakson
per isakson 2020 年 4 月 25 日

0 投票

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() ?

3 件のコメント

Ryan Mulligan
Ryan Mulligan 2020 年 4 月 25 日
I'm still getting the same error.
I was just testing with R = .36 and T = 1
The full values of T however are a column vector of [1;.4861;.2609;.1537;.1043;.0878]
per isakson
per isakson 2020 年 4 月 25 日
編集済み: per isakson 2020 年 4 月 25 日
"I was just testing with R = .36 and T = 1"
With these input values the error should not come as a surprise. R(1:4) will throw the error. And on the following line R(4:end) will return an empty double.
This returns a numerical result
>> nfin=FinEfficiency( 0.36*ones(50,1),(301:350)')'
nfin =
Columns 1 through 10
0.13192 0.13149 0.13105 0.13062 ...
Ryan Mulligan
Ryan Mulligan 2020 年 4 月 25 日
Ah that would make sense, plugged in vectors and it all worked fine haha, thank you for the help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2020 年 4 月 25 日

編集済み:

2020 年 4 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by