Array of integrals within loop

Hello everyone!
For the last few days, I am stuck with a problem with parametrized integration in GNU Octave. I would like to to evaluate an integral of some f(x) function that contains an exponential constant parameter alpha:
eq1 = @(x) 1 ./ (x.^ 2 + 2 .* x * exp(alpha) .+ 1);
eq2 = integral(f,0,inf)
The problem comes when I want to check and plot how the result will look over the defined range of the parameter alpha, so, I would like to get an array of eq2(alpha).
I have defined the range, k. Let's assume alpha will change from -1.0 to 1.0 by 0.1:
k = -1.0:0.1:1.0;
and then alpha will be a matrix with k-values:
for n = length(k)
alpha(k) = n;
end
I would expect the outcome in form of matrix containing calculated integrals with diffrent value of alpha(k). Unfortunately, I am not able to put function
eq1(alpha(k)) = @(x) 1 ./ (x.^ 2 .+ x .* exp(alpha(n)) .+ 1)
within the for loop and integrate it as
eq2(k) = int(eq1(k))
I tried to get a symbolical equation and it works, but this form is not satisfying me enough. I also tried to create a function with loop, but also without success. Do you have in mind the solution that could help me construct and evaluate such an array of integrals? Thank you in advance.

2 件のコメント

Jan
Jan 2022 年 4 月 8 日
編集済み: Jan 2022 年 4 月 8 日
Is this a typo?
for n = length(k)
% 1:length(k) is required
alpha(k) = n;
end
But you cannot use the floating point values of the vector k as an index.
There is no .+ operator.
You did not define f in eq2 = integral(f,0,inf) .
Please post some running code. "Does not satisfy me enough" and "but also without success" are not clear enough. We cannot guess, what the problem is.
Kamil Mickiewicz
Kamil Mickiewicz 2022 年 4 月 11 日
You are right, I am sorry, I forgot to define the range of for loop. I apologize for not putting another line of code.
I was defining a number of iterations as length(alpha) instead of using numel and just put the integral within the loop, so it was looking like this:
k = -1.0:0.1:0.0;
for n = 1:length(k)
alpha(n) = n;
eq1(n) = @(x) 1 ./ (x.^ 2 + 2 .* x * exp(alpha(n)) + 1); % in this case eq1 should not be dependent on (n).
eq2(n) = integral(eq1,0,inf);
end
eq2
After your comment I understood why it had no chance to work correctly.

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

回答 (1 件)

Jan
Jan 2022 年 4 月 8 日

1 投票

alpha = -1.0:0.1:1.0;
for k = 1:numel(alpha)
f = @(x) 1 ./ (x.^ 2 + 2 .* x * exp(alpha(k)) + 1);
eq2(k) = integral(f, 0, inf);
end

1 件のコメント

Kamil Mickiewicz
Kamil Mickiewicz 2022 年 4 月 11 日
Dear Jan, thank you so much! I did not consider using the numel function, this is what I was looking for!

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

カテゴリ

質問済み:

2022 年 4 月 8 日

コメント済み:

2022 年 4 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by