unit utestaproxcuadratica;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, xmatdefs, matreal, uaproxcuadratica;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  A: TMatR;
  B: TVectR;
  c: NReal;

  X, Y: TVectR;
  N: integer;
  k, j, h: integer;

  rq2: TAproxCuadratica;

  f: textfile;
  mval: NReal;
  M: integer;
begin
  N:= 18*2;
  M := (( N+1 ) * N ) div 2 + (N + 1);
  randomize;

  A:= TMatR.Create_Init( N, N );
  A.Ceros;
  for k:= 1 to N do a.pon_e(k, k, 1 );
  (*
  for k:= 1 to N do
    for j:= k to N do
      a.pon_e( k, j, random );

  for k:= 2 to N do
    for j:= 1 to k-1 do
      a.pon_e( k, j, a.e( j, k ));

*)
  B:= TVectR.Create_Init( N );
  for k:= 1 to N do
//    b.pon_e(k, random );
    b.pon_e(k, 0);

//  c:= random;
  c:= 0;

  X:= TVectR.Create_Init( N );
  Y:= TVectR.Create_Init( N );

  rq2:= TAproxCuadratica.Create( N );
  rq2.clear;

   x.Ceros;
   A.PorVector( X, Y );
   mval:= Y.PEV( X );
   mval:= mval + B.PEV( X ) + c;
   rq2.AgregarPunto( X, mval, 1 );

(*
   x.Ceros;
   x.pon_e( 3, 1 );
   A.PorVector( X, Y );
   mval:= Y.PEV( X );
   mval:= mval + B.PEV( X ) + c;
   rq2.AgregarPunto( X, mval, 1 );
   x.Ceros;
   x.pon_e( 2, 1 );
   A.PorVector( X, Y );
   mval:= Y.PEV( X );
   mval:= mval + B.PEV( X ) + c;
   rq2.AgregarPunto( X, mval, 1 );
   x.Ceros;
   x.pon_e( 2, 1 );
   x.pon_e( 3, 1 );
   A.PorVector( X, Y );
   mval:= Y.PEV( X );
   mval:= mval + B.PEV( X ) + c;
   rq2.AgregarPunto( X, mval, 1 );
   x.Ceros;
   x.pon_e( 1, 1 );
   A.PorVector( X, Y );
   mval:= Y.PEV( X );
   mval:= mval + B.PEV( X ) + c;
   rq2.AgregarPunto( X, mval, 1 );
   x.Ceros;
   x.pon_e( 1, 1 );
   x.pon_e( 3, 1 );

   A.PorVector( X, Y );
   mval:= Y.PEV( X );
   mval:= mval + B.PEV( X ) + c;
   rq2.AgregarPunto( X, mval, 1 );
   x.Ceros;
   x.pon_e( 1, 1 );
   x.pon_e( 2, 1 );

   A.PorVector( X, Y );
   mval:= Y.PEV( X );
   mval:= mval + B.PEV( X ) + c;
   rq2.AgregarPunto( X, mval, 1 );
   x.Ceros;
   x.pon_e( 1, 1 );
   x.pon_e( 2, 1 );
   x.pon_e( 3, 1 );

   A.PorVector( X, Y );
   mval:= Y.PEV( X );
   mval:= mval + B.PEV( X ) + c;
   rq2.AgregarPunto( X, mval, 1 );
   x.Ceros;
   x.pon_e( 1, -1 );

   A.PorVector( X, Y );
   mval:= Y.PEV( X );
   mval:= mval + B.PEV( X ) + c;
   rq2.AgregarPunto( X, mval, 1 );
   x.Ceros;
   x.pon_e( 2, -1 );

   A.PorVector( X, Y );
   mval:= Y.PEV( X );
   mval:= mval + B.PEV( X ) + c;
   rq2.AgregarPunto( X, mval, 1 );
*)


  for k:= 1 to M do
  begin
    x.random;
   A.PorVector( X, Y );
   mval:= Y.PEV( X );
   mval:= mval + B.PEV( X ) + c;
   rq2.AgregarPunto( X, mval, 1 );
  end;
  
  assignfile( f, 'rq2.xlt' );
  rewrite( f );
  writeln( f , 'datos' );
  A.WriteXLT( f );
  B.WriteXLT( f );
  writeln( f, c );

  writeln( f, 'XX =============' );
  rq2.XX.WriteXLT( f );
  writeln( f, 'XY ****************');
  rq2.XY.WriteXLT( f );

  if rq2.Resolver(f) then
  begin
    writeln(f, 'Resultados ' );
    writeln( 'Ok resuelto.' );
    rq2.A.WriteXLT( f );
    rq2.B.WriteXLT( f );
    writeln( f, rq2.C );
  end
  else
  begin
    writeln( 'No pude resolver el sistema ' );
  end;
  closefile( f );
  rq2.Free;
  A.Free;
  B.Free;
  Y.Free;
  X.Free;
end;

end.
