logo
Аппроксимация функции методом наименьших квадратов

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

Program ApprF;

Uses Crt;

const n=3;

type

mass=array[1..5]of real;

mass1=array[1..3,1..3] of real;

mass2=array[1..3] of real;

var

X,Y,E,y1,delta : mass;

A: mass1;

B,x1: mass2;

big,r,sum,temp,maxD,Q:real;

i,j,k,l,num : byte;

Procedure VVOD(var E: mass);

Begin

For i:=1 to 5 do

read(E[i]);

writeln;

End;

Function FI( i ,k : integer): real;

Begin

if i=1 then FI:=1;

if i=2 then FI:=Sin(x[k]);

if i=3 then FI:=Cos(x[k]);

End;

Procedure PEREST(i:integer;var a:mass1;var b:mass2);

begin

big:=0;

num:=0;

for l:= i to 3 do

if abs(a[l,i]) > big then

begin

big:=a[l,i]; writeln ( big:6:4);

num:=l;

end;

if big=0 then

writeln(Перестановка уравнений);

if num<>i then

for j:=i to 3 do

begin

temp:=a[i,j];

a[i,j]:=a[num,j];

a[num,j]:=temp;

end;

temp:=b[i];

b[i]:=b[num];

b[num]:=temp;

end;

Begin

writeln(__________________);

writeln(Введите значения Х);

VVOD(X) ;

writeln(__________________);

writeln(‚Введите значения Y);

VVOD(Y);

writeln(___________________);

For i:=1 to 3 do

For j:=1 to 3 do

begin

A[i,j]:=0;

For k:=1 to 5 do

begin A[i,j]:= A[i,j]+FI(i,k)*FI(j,k); write(a[i,j]:7:5); end;

end;

writeln(________________________);

writeln(Матрица Коэффициентов Ai,j);

writeln(__________________________);

For i:=1 to 3 do

begin

For j:=1 to 3 do

write (A[i,j]:5:2, );

writeln;

end;

For i:=1 to 3 do

begin

B[i]:=0;

For j:=1 to 5 do

B[i]:=B[i]+Y[j]*FI(i,j);

end;

writeln;

readkey;

writeln(__________________________);

writeln(`Матрица Коэффициентов Bi );

writeln(_________________________);

For i:=1 to 3 do

write(B[i]:5:2, );

writeln;

for i:=1 to 2 do

begin

PEREST(i,a,b);

for k:=i+1 to 3 do

begin

Q:=a[k,i]/a[i,i]; writeln(g=,Q);

a[k,i]:=0;

for j:=i+1 to 3 do

a[k,j]:=a[k,j]-Q*a[i,j]; writeln(a=,a[k,j]);

b[k]:=b[k]-Q*b[i]; writeln(b=,b[k]);

end;

end;

x1[n]:=b[n]/a[n,n];

write(x1[n]);

for i:=2 downto 1 do

begin

sum:=b[i];

for j:=i+1 to 3 do

sum:=sum-a[i,j]*x1[j];

x1[i]:=sum/a[i,i];

end;

writeln(__________________________);

writeln (Значение коэффициентов );

writeln(_________________________);

for i:=1 to 3 do

writeln( C,i,=,x1[i]);

k:=1;

for i:=1 to 5 do

begin

y1[i]:= x1[k]*FI(k,i) + x1[k+1]*FI(k+1,i) + x1[k+2]*FI(k+2,i);

delta[i]:=abs (y[i]-y1[i]);

writeln;

writeln (y1[i]);

end;

for i:=1 to 3 do

write (x1[i]:7:3);

writeln;

maxD:=delta[1];

for i:=1 to 5 do

if delta[i]>maxD then maxD:=delta[1];

writeln (max Delta= , maxD:5:3);

End.