Integration of a function that is built by a loop

1 回表示 (過去 30 日間)
Wolfgang Dr. Auf der Heyde
Wolfgang Dr. Auf der Heyde 2020 年 5 月 6 日
What is the Integral of
for k = 1 :500
y = y +(sin( 2*k* x))./ k
end
in the bounderies of zero to pi ?

採用された回答

Bjorn Gustavsson
Bjorn Gustavsson 2020 年 5 月 6 日
You should do two things!
1, write that function definition into a function, then integrate it from 0 to pi. Use the integral or quadgk functions for the integration.
2, solve the more general problem: for n equal to 1, 2, 3, to 500.
HTH
  3 件のコメント
Bjorn Gustavsson
Bjorn Gustavsson 2020 年 5 月 15 日
Herr Doktor Auf der Heyde,
Please be adviced to calculate the integral for an arbitrary term sin(2*n*x)/n from zero to π analytically by hand. Y is a sum of terms of simple continous functions on the intervall, therefore the integral of Y is equal to the sum of the integral of the terms.
I made a little illustrationing of this using this function:
function y = fivehundredsines( x,n_sines )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
if nargin < 2 || isempty(n_sines)
n_sines = 500;
end
y = 0;
for k = 1:n_sines
y = y +(sin( 2*k* x))./ k;
end
That one can run with:
x = linspace(0,pi,1001);
n = 1;
clf
Y =fivehundredsines(x,n);plot(x,Y),title([trapz(x,Y),integral(@(x)fivehundredsines(x,n),0,pi)]),xlabel(n),n = 2*n; hold on
Y =fivehundredsines(x,n);plot(x,Y),title([trapz(x,Y),integral(@(x)fivehundredsines(x,n),0,pi)]),xlabel(n),n = 2*n; hold on
Y =fivehundredsines(x,n);plot(x,Y),title([trapz(x,Y),integral(@(x)fivehundredsines(x,n),0,pi)]),xlabel(n),n = 2*n; hold on
Y =fivehundredsines(x,n);plot(x,Y),title([trapz(x,Y),integral(@(x)fivehundredsines(x,n),0,pi)]),xlabel(n),n = 2*n; hold on
HTH
Wolfgang Dr. Auf der Heyde
Wolfgang Dr. Auf der Heyde 2020 年 5 月 19 日
Pefect solution with minor modifications. The question arose from the Fejer_Jackson_Grönwall-Ungleichung-Math.Annalen1912-revisited www.mathe-kalender.de Prof.E.Wegert et.al.TU-Freiberg, you also may see www.complex-pictures.com Wolfg.AdH

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by