How to evaluate a function that has array and summation at the same time?

3 views (last 30 days)
K=[5;6;7; 8; 9; 10; 11; 12; 13; 14; 15];
nt=2;
Var=1;
Yr=[0;10^(-6.7/10); 10^(-4.7/10);10^(-2.3/10);10^(0.2/10);10^(2.4/10);10^(4.3/10);10^(5.9/10);10^(8.1/10);10^(10.3/10);10^(11.7/10);10^(14.1/10);10^(16.3/10);10^(18.7/10);10^(21/10);10^(22.7/10)];
Eff=[0;0.55;0.835;1.12;1.35;1.69;1.98;2.56;3.045;3.43;3.815;4.2;4.585;4.97;5.355;5.75];
I cannot do this function in matlab I tried using for loop and summation outside but it is not working as I needed it as a whole function with the parameter given above please can anyone help me?

Answers (1)

Shaunak
Shaunak on 19 Feb 2025
Hi Radwa,
It is my understanding that you're trying to evaluate a function in MATLAB that involves both arrays and summation, but you're encountering difficulties in combining the for loop and summation into a complete function. To address this, you can use vectorization and for loops to efficiently handle arrays and summations in your code.
Here is an sample implementation of the function in MATLAB:
function Gamma = evaluateFunction(nt, Var, Yr, K)
% Initialize result
Gamma = 0;
R = length(Yr) - 1;
% Loop over each K
for k = K
sum_zeta = 0;
for r = 1:R
gamma_r = Yr(r);
gamma_r1 = Yr(r+1);
% Calculate terms
term1 = (1 - exp(-nt * gamma_r1 * Var) / (1 + gamma_r1)^(nt-1))^k;
term2 = (1 - exp(-nt * gamma_r * Var) / (1 + gamma_r)^(nt-1))^k;
% Compute zeta_r
zeta_r = term1 - term2;
sum_zeta = sum_zeta + zeta_r;
end
% Compute Gamma_k
Bw = 1; % Define Bw as needed
Gamma_k = Bw * nt * (1 - (1 - 1/nt)^k) * sum_zeta;
Gamma = Gamma + Gamma_k;
end
end
% Example usage:
nt = 2;
Var = 1;
Yr = [0, 10^(-6.7/10), 10^(-4.7/10), 10^(-2.3/10), 10^(0.2/10), 10^(2.4/10), 10^(4.3/10), 10^(5.9/10), 10^(8.1/10), 10^(10.3/10), 10^(11.7/10), 10^(14.1/10), 10^(16.3/10), 10^(18.7/10), 10^(21/10), 10^(22.7/10)];
K = 5:15;
Gamma = evaluateFunction(nt, Var, Yr, K);
disp('Gamma:');
Gamma:
disp(Gamma);
21.8751
Feel free to modify the code as per your needs.
You can use the following MathWorks documentation for additional reference:
Hope this helps!

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!