Linear Algebra and the C Language/a043


Install and compile this file in your working directory.

/* ------------------------------------ */
/*  Save as :   c00b.c                  */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
/* ------------------------------------ */
#define   RA R5
#define   CA C5
/* ------------------------------------ */
#define FACTOR_E        +1.E-0         
/* ------------------------------------ */
int main(void)
{
double   xy[8] ={
   1,     -8,
   2,      2,
   3,      1,
   4,      2  };
   
double tA[RA*CA]={
/* x**2    y**2    x       y       e     */
  +1,     +0,     +0,     +0,     +0, 
  +1,    +64,     +1,     -8,     +1,     
  +4,     +4,     +2,     +2,     +1,      
  +9,     +1,     +3,     +1,     +1,    
 +16,     +4,     +4,     +2,     +1,         
};

double tb[RA*C1]={
/*    = 0   */
       +1,   
       +0,   
       +0,   
       +0,   
       +0 
};

double **XY      = ca_A_mR(xy,i_mR(R4,C2));
double **A       = ca_A_mR(tA,i_mR(RA,CA));
double **b       = ca_A_mR(tb,i_mR(RA,C1));
double **Pinv    = i_mR(CA,RA);         
double **Pinvb   = i_mR(CA,C1);         

  clrscrn();
  printf("\n");
  printf(" Find the coefficients a, b, c, d, e, of the curve \n\n");
  printf("     ax**2 + by**2 + cx + dy + e  = 0 \n\n");
  printf(" that passes through these four points.\n\n");
  printf("         x          y");
  p_mR(XY,S10,P0,C6);
  stop();
  
  clrscrn();
  printf(" Using the given points, we obtain this matrix.\n");
  printf("  (a = 1. This is my choice)\n\n");  
  printf(" A :");
  p_mR(A,S10,P2,C7);
  printf(" b :");
  p_mR(b,S10,P2,C7);
   
  printf(" Pinv = V * invS_T * U_T ");
  Pinv_Rn_mR(A,Pinv,FACTOR_E); 
  pE_mR(Pinv,S12,P4,C10); 
  stop();
  
  clrscrn();
  printf(" Solving this system yields a unique\n"
         " least squares solution, namely   \n\n");  
  printf(" Pinv * b ");   
  mul_mR(Pinv,b,Pinvb); 
  p_mR(Pinvb,S10,P4,C10);
  printf(" The coefficients a, b, c, d, e, of the curve are : \n\n"
         "  %+.9f*x^2 %+.9f*y^2 %+.9f*x %+.9f*y %+.9f = 0\n\n"
            ,Pinvb[R1][C1],Pinvb[R2][C1],Pinvb[R3][C1],
             Pinvb[R4][C1],Pinvb[R5][C1]);      
  stop();  

  f_mR(XY);
  f_mR(b);
  f_mR(A);
  f_mR(Pinv);
  f_mR(Pinvb); 

  return 0;
}
/* ------------------------------------ */
/* ------------------------------------ */


Screen output example:
 Find the coefficients a, b, c, d, e, of the curve 

     ax**2 + by**2 + cx + dy + e  = 0 

 that passes through these four points.

         x          y
        +1         -8 
        +2         +2 
        +3         +1 
        +4         +2 

 Press return to continue. 


 Using the given points, we obtain this matrix.
  (a = 1. This is my choice)

 A :
     +1.00      +0.00      +0.00      +0.00      +0.00 
     +1.00     +64.00      +1.00      -8.00      +1.00 
     +4.00      +4.00      +2.00      +2.00      +1.00 
     +9.00      +1.00      +3.00      +1.00      +1.00 
    +16.00      +4.00      +4.00      +2.00      +1.00 

 b :
     +1.00 
     +0.00 
     +0.00 
     +0.00 
     +0.00 


 Pinv = V * invS_T * U_T 
 +1.0000e+00  +1.1983e-15  -2.5951e-15  +4.9571e-14  -2.9560e-14 
 -1.4444e-01  +1.1111e-02  +3.8889e-02  -1.1111e-01  +6.1111e-02 
 -6.0000e+00  -1.0143e-14  -5.0000e-01  -4.2810e-13  +5.0000e-01 
 -5.6667e-01  -3.3333e-02  +3.8333e-01  -6.6667e-01  +3.1667e-01 
 +9.7111e+00  +2.2222e-02  +1.0778e+00  +1.7778e+00  -1.8778e+00 

 Press return to continue. 


 Solving this system yields a unique
 least squares solution, namely   

 Pinv * b 
   +1.0000 
   -0.1444 
   -6.0000 
   -0.5667 
   +9.7111 

 The coefficients a, b, c, d, e, of the curve are : 

  +1.000000000*x^2 -0.144444444*y^2 -6.000000000*x -0.566666667*y +9.711111111 = 0

 Press return to continue.


Copy and paste in Octave:
function xy = f (x,y)
  xy = +1.000000000*x^2 -0.144444444*y^2 -6.000000000*x -0.566666667*y +9.711111111;
endfunction

f (1, -8)
f (2,  2) 
f (3,  1) 
f (4,  2)