Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Отчет Расчетка ВЫЧМАТ.docx
Скачиваний:
0
Добавлен:
01.03.2025
Размер:
3.19 Mб
Скачать

3.6 Интерполяция сплайнами

3.6.1 Листинг

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls, Grids, ExtCtrls, jpeg;

type

TForm1 = class(TForm)

StringGrid1: TStringGrid;

Edit1: TEdit;

Image1: TImage;

Button2: TButton;

Button1: TButton;

Memo1: TMemo;

Label1: TLabel;

Label2: TLabel;

Edit2: TEdit;

Button3: TButton;

Panel1: TPanel;

Panel2: TPanel;

Label3: TLabel;

Label4: TLabel;

lbl7: TLabel;

bvl1: TBevel;

Image2: TImage;

lbl10: TLabel;

lbl1: TLabel;

lbl2: TLabel;

lbl3: TLabel;

lbl4: TLabel;

lbl5: TLabel;

lbl6: TLabel;

Image3: TImage;

lbl9: TLabel;

procedure Button2Click(Sender: TObject);

procedure Button1Click(Sender: TObject);

procedure Button3Click(Sender: TObject);

procedure FormCreate(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

type

Mas=array[0..100] of Extended;

var

Form1: TForm1;

Xmax,Xmin,Ymax,Ymin:extended;

i,j,n,k,raz,nn : integer;

Q,P :Mas;

xx,a,b,c,d,y,cc,h,aa,bb:Mas;

st:string;

R:real;

yy:real;

PX,PY,Lg,Rg:integer;

x,XXmax,XXmin: extended;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

begin

n:=StrToInt(Edit1.text);

StringGrid1.ColCount:=n;

StringGrid1.Width:=(n)*67;

StringGrid1.Visible:=true;

end;

function S(x: Extended; i:Integer; a,y,xx,c,b,d:Mas ; var Xmax,Xmin,Ymax,Ymin:extended):Extended;

var

Sum, Mng: extended;

begin

Sum:=0;

Sum:=a[i]+b[i]*(x-xx[i-1])+c[i]*(x-xx[i-1])*(x-xx[i-1])+d[i]*(x-xx[i-1])*(x-xx[i-1])*(x-xx[i-1]);

S:=Sum;

Xmax:=10;

Xmin:=-10;

Ymax:=10;

Ymin:=-10;

end;

procedure TForm1.Button2Click(Sender: TObject);

begin

memo1.Text:='';

Panel1.Visible:=true;

Panel2.Visible:=true;

nn:=StrToInt(Edit1.text);

n:=nn-1;

for j:=0 to n do

begin

xx[j]:=StrToFloat(StringGrid1.Cells[j,0]);

y[j]:=StrToFloat(StringGrid1.Cells[j,1]);

end;

for i:=1 to n do

h[i]:=xx[i]-xx[i-1];

c[1]:=0;

aa[2]:=0;

Q[1]:=0;

p[1]:=0;

for i:=2 to n do

begin // Нахождение коэф с[i] методом прогонки

bb[i]:=2*(h[i-1]+h[i]);

cc[i]:=h[i];

aa[i+1]:=h[i];

d[i]:=3*((y[i]-y[i-1])/h[i]-(y[i-1]-y[i-2])/h[i-1]); {y[0]}

P[i]:=(-cc[i])/(bb[i]+aa[i]*P[i-1]);

Q[i]:=(d[i]-aa[i]*Q[i-1])/(bb[i]+aa[i]*P[i-1]);

end;

c[n+1]:=0;

for i:=n downto 2 do

begin

c[i]:=P[i]*c[i+1]+Q[i];

end;

c[n+1]:=0;

for i:=1 to n do

begin

b[i]:=((y[i]-y[i-1])/h[i]-(1/3)*h[i]*(c[i+1]+2*c[i]));

d[i]:=(c[i+1]-c[i])/(3*h[i]);

a[i]:=y[i-1];

end;

for i:=1 to n do

begin

With Form1.Memo1.Lines do

begin

Add('a = '+FloatToStrF(a[i],fffixed,4,2));

Add('b = '+FloatToStrF(b[i],fffixed,4,2));

Add('c = '+FloatToStrF(c[i],fffixed,4,2));

Add('d = '+FloatToStrF(d[i],fffixed,4,2));

Add('----------------');

end;

end;

image1.Canvas.MoveTo(0,Image1.height div 2);

image1.Canvas.LineTo(Image1.width,Image1.height div 2);

image1.Canvas.MoveTo(Image1.width div 2,0);

image1.Canvas.LineTo(Image1.width div 2,Image1.height);

for i:=1 to n do

begin

XXmax:=xx[i];

XXmin:=xx[i-1];

x:=XXmin;

yy:=(S(x,i,a,y,xx,c,b,d,Xmax,Xmin,Ymax,Ymin));

image1.Canvas.MoveTo(Trunc((XXmin-Xmin)*(Image1.Width/(Xmax-Xmin))),trunc(Image1.Height-(yy-Ymin)*Image1.height/(Ymax-Ymin)));

Image1.Canvas.Pixels[Trunc((XXmin-Xmin)*(Image1.Width/(Xmax-Xmin))),trunc(Image1.Height-(yy-Ymin)*Image1.height/(Ymax-Ymin))]:=clRed;

Rg:=trunc((xx[i]-xx[i-1])*(Image1.Width/(Xmax-Xmin)));

for PX:=0 to Rg do

Begin

x:=XXmin+PX*(Xmax-Xmin)/Image1.Width;

yy:=(S(x,i,a,y,xx,c,b,d,Xmax,Xmin,Ymax,Ymin));

PY:=trunc(Image1.Height-(yy-Ymin)*Image1.height/(Ymax-Ymin));

Image1.Canvas.Pen.Color:=clRed;

Lg:=trunc((x-Xmin)*(Image1.Width/(Xmax-Xmin)));

Image1.Canvas.LineTo(Lg,PY);

end;

end;

memo1.Visible:=TRUE;

end;

procedure TForm1.Button3Click(Sender: TObject);

begin

x:=StrToFloat(Edit2.text);

for i:=1 to n do

if (x>=xx[i-1]) and (x<=xx[i]) then

begin

yy:=(S(x,i,a,y,xx,c,b,d,Xmax,Xmin,Ymax,Ymin));

Form1.memo1.lines.add('Значение функции в точке '+FloatToStr(x)+'= '+FloatToStr(yy));

Exit;

end;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

{ image1.Canvas.MoveTo(0,Image1.height div 2);

image1.Canvas.LineTo(Image1.width,Image1.height div 2);

image1.Canvas.MoveTo(Image1.width div 2,0);

image1.Canvas.LineTo(Image1.width div 2,Image1.height); }

end;

end.