フィルターのクリア

How do I integrate this multivariable function over just one variable?

8 ビュー (過去 30 日間)
L'O.G.
L'O.G. 2022 年 3 月 8 日
コメント済み: Walter Roberson 2022 年 3 月 8 日
How do you do you do the following like . (My actual function is more complicated, but this should suffice, I think.) Here, t, r, and x are all variables, with x just being used for purposes of the integration.
I tried something like
a = 0;
b = 2;
for t = 1:10
sum = 0;
for r = 1:10
f = @(x) sin(r*t*x.).^2;
sum = sum + f;
end
0.5*integral(sum,a,b)
end
But I can't add the function handle and am not sure if the procedure is even correct. How do I do this? For every t there should be many r.

採用された回答

Walter Roberson
Walter Roberson 2022 年 3 月 8 日
Well, you can
a = 0;
b = 2;
for t = 1:10
sum = @(x) zeros(size(x));
for r = 1:10
f = @(x) sin(r*t*x).^2;
sum = @(x) sum(x) + f(x);
end
0.5*integral(sum,a,b)
end
ans = 5.0512
ans = 4.9580
ans = 5.0476
ans = 5.0045
ans = 4.9769
ans = 5.0196
ans = 4.9978
ans = 4.9824
ans = 5.0096
ans = 4.9951
This is not a great integral; t and r should be finer steps, and you should be taking into account dt and dr and you should probably be using trapz() or cumtrapz()
If you can use symbolic work it goes much easier:
syms r t x
a = 0;
b = 2;
F(t) = int(sin(r*t*x).^2, x, a, b)
F(t) = 
To be equivalent to your proposed code this should probably be integrated over r = 1 to 10
syms r t x
a = 0;
b = 2;
F(t) = int(int(sin(r*t*x).^2, x, a, b),r,1,10)
F(t) = 
fplot(F, [1 10])
  2 件のコメント
L'O.G.
L'O.G. 2022 年 3 月 8 日
Thank you. Is there a cleaner way to do this using trapz or cumtrapz, like you alluded to?
Walter Roberson
Walter Roberson 2022 年 3 月 8 日
Evaluate the function over a (possibly multidimensional) grid, trapz() or cumtrapz() over appropriate dimensions.
This would mostly be of use if you wanted to be able to output that data separately; otherwise you would just use integral() or integral2() or integral3() or https://www.mathworks.com/matlabcentral/fileexchange/47919-integraln-m

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by