Хелпикс

Главная

Контакты

Случайная статья





Текст программы. repeat. repeat



Текст программы

 

program Lab4;

{$APPTYPE CONSOLE}

uses

SysUtils;

 

type

Func=function (x:Real):Real;

 

function Sum(f:Func;a,b:Real;n:Integer):Real;

var

   dx:Real;

   i:Integer;

begin

   dx:=(b-a)/n;

   Result:=0;

   for i:=0 to n-1 do

   Result:=Result+dx*f(a+i*dx+dx/2);

end;

 

function Integr(f:Func;a,b,MaxError:Real):Real;

var

   n:Integer;

   Prev:Real;

begin

   n:=8;

   Result:=Sum(f,a,b,n);

repeat

   Prev:=Result;

   n:=n*2;

   Result:=Sum(f,a,b,n);

   until Abs(Result-Prev)<MaxError;

end;

 

procedure PrintIntegr(f,G:Func);

var

a,b:Real;

ch:Char;

begin

repeat

   Write('vvedite otrezok inegrirovaniya: ');

   ReadLn(a,b);

 

   Writeln('Priblizhennoe znachenie integrala'),

   Integr(f,a,b,1e-6):1:6,'+-',1e-6:1:6);

   if @G<>nil then

   Writeln('Tochnoe znachenie integrala '),

   G(b)-G(a):1:10);

 

   Write('Continued? (Y/N) ? ');

   Readln(ch);

until UpCase(ch)='N';

end;

 

   function f1(x:Real):Real;

begin f1:=x*sin(x) end;

   function G1(x:Real):Real;

begin G1:=sin(x)-x*cos(x) end;

 

   function f2(x:Real):Real;

begin f2:=sqr(cos(x)) end;

   function G2(x:Real):Real;

begin G2:=x/2+sin(2*x)/4 end;

 

   function f3(x:Real):Real;

begin f3:=sin(x)/x end;

 

   function f4(x:Real):Real;

begin f4:=exp(sqr(x)) end;

 

var

n:Integer;

Loop:Boolean;

begin

   Loop:=True;

   while Loop do

begin

   Writeln('1. integral of function x*sin(x)');

   Writeln('2. integral of function sqr(cos(x))');

   Writeln('3. integral of function sin(x)/x');

   Writeln('4. integral of function exp(sqr(x))');

   Writeln('5. exit');

   Write('viberite punkt menu: ');

   Readln(n);

   Writeln;

case n of

   1:PrintIntegr(f1,G1);

   2:PrintIntegr(f2,G2);

   3:PrintIntegr(f3,nil);

   4:PrintIntegr(f4,nil);

      5:Loop:=False;

end;

   Writeln;

end;

   readln;

end.

 

 end.

 

Тест

 

Функция a b Приближенное значение интеграла Точное значение интеграла
x sin x 0.301168±0.000001 0.3011686789
x sin x 1.741591±0.000001 1.7415910999
x sin x 1.369507±0.000001 1.3695063979
x sin x 3.111098±0.000001 3.1110974979
cos2 x 0.727325±0.000001 0.7273243567
cos2 x 0.810799±0.000001 0.8107993762
cos2 x 0.619347±0.000001 0.6193467493
cos2 x 1.430146±0.000001 1.4301461255
sin x/x 0.659330±0.000001
sin x/x 0.243239±0.000001
sin x/x 0.902570±0.000001
ex2 1.462652±0.000001
ex2 14.989976±0.000001
ex2 16.452628±0.000001

 

 



  

© helpiks.su При использовании или копировании материалов прямая ссылка на сайт обязательна.