
Integral definida en delphi
Publicado por Guillermo (1 intervención) el 08/12/2014 19:14:13
Buenos dias amigos, tengo un problema en delphi que al hacer una función (function) para calcular la integral definida de la siguiente función (y=(x^2)*cos(2x)+3) en un intervalo (a,b) el programa se queda pegado, anexo el código para que lo analicen y me puedan ayudar (separé lo más que pude para ver si el error estaba por ahí pero no logré nada).
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Math;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function integral(a,b:extended):extended;
var
c,d,e,f,g,h,i,j,k,l,m,n,o,x1,x2,y1,y2,at:extended;
Begin
c:=(b-a)/10000;
x1:=a;
x2:=x1+c;
e:=0;
Repeat
f:=Power(x1,2);
g:=cos(2*x1);
h:=f*g;
y1:=h+3;
i:=Power(x2,2);
j:=cos(2*x2);
k:=i*j;
y2:=k+3;
l:=x2-x1;
m:=y2-y1;
n:=l*m;
at:=n/2;
if y1<y2 then
d:=((l)*y1)+at
else
d:=((l)*y2)+at;
e:=e+d;
x1:=x2;
x2:=x2+c;
Until x1=b;
Result:=e;
End;
procedure TForm1.Button1Click(Sender: TObject);
var
a,b,c:extended;
begin
a:=Strtofloat(Edit1.Text);
b:=strtofloat(Edit2.Text);
if b<a then
Showmessage('El límite inferior debe ser menor al límite superior')
else
begin
c:=Integral(a,b);
Edit3.Text:=Floattostr(c);
end;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Math;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function integral(a,b:extended):extended;
var
c,d,e,f,g,h,i,j,k,l,m,n,o,x1,x2,y1,y2,at:extended;
Begin
c:=(b-a)/10000;
x1:=a;
x2:=x1+c;
e:=0;
Repeat
f:=Power(x1,2);
g:=cos(2*x1);
h:=f*g;
y1:=h+3;
i:=Power(x2,2);
j:=cos(2*x2);
k:=i*j;
y2:=k+3;
l:=x2-x1;
m:=y2-y1;
n:=l*m;
at:=n/2;
if y1<y2 then
d:=((l)*y1)+at
else
d:=((l)*y2)+at;
e:=e+d;
x1:=x2;
x2:=x2+c;
Until x1=b;
Result:=e;
End;
procedure TForm1.Button1Click(Sender: TObject);
var
a,b,c:extended;
begin
a:=Strtofloat(Edit1.Text);
b:=strtofloat(Edit2.Text);
if b<a then
Showmessage('El límite inferior debe ser menor al límite superior')
else
begin
c:=Integral(a,b);
Edit3.Text:=Floattostr(c);
end;
end;
end.
Valora esta pregunta


0