フィルターのクリア

Optimize the construction of a function for quadrature purposes

3 ビュー (過去 30 日間)
pluton schmidt
pluton schmidt 2023 年 2 月 15 日
編集済み: Torsten 2023 年 2 月 15 日
Hello,
I want to compute the integral of a function which is the linear combination of some known function family (here, Fourier functions):
MyInt = integral(@(t) myfun(t), 0, 2*pi);
My function myfun(t) is built as follows:
function Output = myfun(t)
Output = 0;
for p = 1:20
Output = Output + 1/p.*cos(p*t);
Output = Output + 1/(2*p).*sin(p*t);
end
I can't think of a way to vectorize what's above without the for loop? Can we do better?
Thank you

採用された回答

Torsten
Torsten 2023 年 2 月 15 日
編集済み: Torsten 2023 年 2 月 15 日
MyInt = integral(@myfun, 0, 2*pi)
MyInt = -5.5511e-16
function Output = myfun(t)
p = (1:20).';
Output = sum(1./p.*cos(p.*t) + 1./(2*p).*sin(p.*t),1);
end

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by