Program Fourier_Series;
{:::::: Программа, изображающая частичную сумму рядов Фурье :::::}
{$N+}
Uses Graph, CRT;
const
Sx : Extended = 24.0;
Sy : Extended = 30.0;
PiGra : Extended = 4.0;
var
D, R, e, YTx : Integer;
x, y, Pi_, _Pi_ : Extended;
{-----------------------}
Procedure BuildScreen;
var
i : Byte;
begin
SetBkColor(Black);
ClearViewPort;
SetColor(DarkGray);
MoveTo(0, 240);
LineTo(640, 240);
LineTo(630, 238);
LineTo(630, 242);
LineTo(640, 240);
MoveTo(320, 480);
LineTo(320, 0);
LineTo(318, 10);
LineTo(322, 10);
LineTo(320,0);
SetColor(LightGray);
for i := 1 to 32 do Line(i*20, 239, i*20, 241);
for i := 1 to 24 do Line(319, i*20, 321, i*20);
SetColor(Brown);
OutTextXY(324, 230, '0');
OutTextXY(632, 230, 'x');
OutTextXY(324, 3, 'y');
end;
Function f(x:Extended):Extended;
begin
if x<0 then f := -Pi_ else
if x>0 then f := Pi_
else f := 0; {Signum}
{ f := x;{////}
end;
{=======+++ n-е члены ряда +++==============}
Function Series(k, x : Extended) : Extended;
var
a : Extended;
begin
a :=3.5798;
{ Series := 2*cos((2*k+1)*x); {delta-функция }
{ Series := -4.0/(Pi_*sqr(2*k+1))*cos((2*k+1)*x); { 0^ /\/\/\/\/\ Надо : -Pi_/2.0 в FourierSumma}
{ Series := 4*sin((2*k+1)*x)/(2*k+1);{ (Меандр - Signum "-_-_-_-_")}
{ Series := 2.0*cos(Pi_*(k+1))*sin(k*x)/k; { ///%//// f(x)=x период. 2*Pi, -Pi..Pi}
{ Series := -2.0*sin(k*x)/k; { ///|//// f(x)=x период. 2*Pi, -0..2*Pi}
{ Series := sin(a*Pi_)*2*k*sin(k*x)/(Pi_*(a*a-k*k)); {sin(ax)}
{ Series := cos(Pi_*k)+k*sin(k*x)/(k*k+1); {x- функция}
Series := -k*(cos(k*x)-sin(k*x))/(k*k+1); {}
end;
{=====+++ Частичная сумма ряда с n1-ого по n2-ый член + свободный член +++=====}
Function FourierSumma(n1, n2 : Longint; x : Extended) : Extended;
var
S : Extended;
i : Longint;
begin
S := 0;
for i := n1 to n2 do
S := S + Series(1.0*i, x);
FourierSumma:=S{-Pi_/2.0};
end;
begin
D:=Detect;
InitGraph(D,R,'');
e:=GraphResult;
if e <> 0 then
begin
WriteLn('Ошибка графики(', e, '):');
WriteLn(GraphErrorMsg(e));
Halt;
end;
Pi_ := Pi;
_Pi_ := Pi_*PiGra;
BuildScreen;
x := -_Pi_;
While not KeyPressed and (x<=_Pi_)do
begin
y := FourierSumma(0, 50, x);
x := x + 0.001;
PutPixel(Trunc(320.0+x*Sx), Trunc(240.0-y*Sy), Yellow);
end;
ReadKey;
CloseGraph;
x := -Pi_;
YTx := 1;
While (x<=Pi_)do
begin
y := FourierSumma(0, 50, x);
x := x + 0.01;
Write('Фурье= ', y:5:5, ' Точ. зн.=', f(x):5:5, ' Дельта= ', Abs(f(x)-y):5:5, ' X=', x:5:5);
Write(' ', Abs(100*(f(x)-y)/f(x)):2:3, '%');
inc(YTx);
if YTx > 25 then
begin
YTx := 1;
if ReadKey=#27 then Halt;
end;
WriteLn;
end;
end.