Integral of user-defined function

7 ビュー (過去 30 日間)
Vishakha Ramani
Vishakha Ramani 2023 年 6 月 4 日
回答済み: John D'Errico 2023 年 6 月 4 日
I have the following MATLAB code, and would like to evaluate the integral of compcdf_mgc function with respect to variable y. However, I'm getting the following error: "Unable to perform assignment because the left and right sides have a different number of elements."
rate = 1;
num_steps = 2;
num_proc = num_steps;
f = @(y) compcdf_mgc(num_steps, rate, num_proc, y);
integral_value = integral(f, 0, Inf);
function final = compcdf_mgc(num_steps, rate, c, y)
n = num_steps;
scale = 1/rate; % this is b = 1/lambda
compcdfvec = zeros(n, 1);
for i = 1:n
compcdfvec(i) = 1 - gamcdf(y, n+i, scale);
end
final = ((1/n)*sum(compcdfvec))^c;
end

採用された回答

Torsten
Torsten 2023 年 6 月 4 日
編集済み: Torsten 2023 年 6 月 4 日
Your function is not vectorized. Thus you have to use
integral_value = integral(f, 0, Inf, 'ArrayValued',true)
instead of
integral_value = integral(f, 0, Inf)
  1 件のコメント
Vishakha Ramani
Vishakha Ramani 2023 年 6 月 4 日
Thank you. This worked!

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

その他の回答 (1 件)

John D'Errico
John D'Errico 2023 年 6 月 4 日
Is your code vectorized? That is, what happens when a VECTOR of elements for y is passed in? (It does not appear to be.)
integral assumes a vectorized function.

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by