필터 지우기
필터 지우기

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 CenterFile Exchange에서 Numerical 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