Линейная Интерполяция
Добого времени суток, очередная проблема, опять с делфи, и опять как обычно накосячила с кодом. В общем, идея задания такова, что дана таблица значения Х и У, и даны два значения Х0, к которым нужно найти У0 соответственно. Составила блок схему - идея ясна, начала писать код, с горем пополам что-то написала, но он не считает! Помогите разобраться.
Код:
unit Unit_int;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
GroupBox1: TGroupBox;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
Edit1: TEdit;
Edit2: TEdit;
GroupBox2: TGroupBox;
Edit3: TEdit;
Label1: TLabel;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
matr1 = array[0..9] of double;
Matr2 = array [0..9] of double;
const
X:matr1 =(-1.35,-1.05,-0.75,-0.45,-0.15,0.15,0.45,0.75,1.05,1.35);
Y:matr2 =(-0.41,-0.25,-0.47,-0.41,-0.15,0.15,0.49,1.03,1.85,2.29);
var
Form1: TForm1;
implementation
function Find (X:matr1; x0:real):integer;
var
i:integer;
begin
i:=0;
while X[i]< x0 do
i:=i+1;
find:=i;
end;
Function Lint(x0: real):real;
var
X:matr1;
Y:matr2;
k,c,Ya,Yb,Xa,Xb: real;
A,B:integer;
begin
B:=find(X,x0);
A:=B-1;
Xa:=X[A];
Xb:=X[b];
Ya:= Y[A];
Yb:= Y[b];
k:= (Yb-Ya)/(Xb-Xa);
c:= Ya-k*Xa;
Lint:= k*x0+c;
end;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
decimalseparator:='.';
for i:=0 to 9 do
begin
StringGrid1.Cells[i+1,0]:=floattostr(X[i]);
StringGrid1.Cells[i+1,1]:=floattostr(Y[i]);
end;
StringGrid1.Cells[0,0]:=' x';
StringGrid1.Cells[0,1]:=' y';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
x0,y0, Xa,Xb,Ya,Yb: real;
A,B: integer;
begin
if RadioButton1.Checked then
x0 := strtofloat(edit1.text);
if RadioButton2.Checked then
x0:= strtofloat(edit2.text);
// B:=find(X,x0);
//A:=B-1;
{ Xa:=X[A];
Xb:=X[b];
Ya:= Y[A];
Yb:= Y[b]; }
y0:=Lint(x0);
//y0:=floattostrf(edit3.text);
edit3.Text:=floattostrF(y0,fffixed,6,4);
end;
end.
|