Linear Algebra and the C Language/a04a


Install and compile this file in your working directory.

/* ------------------------------------ */
/*  Save as:   c00d.c                   */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
#define   RA R5
#define   CA C5
/* ------------------------------------ */
#define FACTOR_E        +1.E-2         
/* ------------------------------------ */
int main(void)
{
double txy[6] ={
   10,     10,
   -5,      1,
    7,    -10    };
   
double tA[RA*CA]={
/* x**2     y**2     x        y        e     */
  +1,      +0,      +0,      +0,      +0,     
  +0,      +1,      +0,      +0,      +0,      
+100,    +100,     +10,     +10,      +1,        
 +25,      +1,      -5,      +1,      +1,        
 +49,    +100,      +7,     -10,      +1,       
};

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

double **xy      =     ca_A_mR(txy,    i_mR(R3,C2));
double **A       =     ca_A_mR(tA,     i_mR(RA,CA));
double **b       =     ca_A_mR(tb,     i_mR(RA,C1));
double **Pinv    =  Pinv_Rn_mR(A,      i_mR(CA,RA),FACTOR_E);          
double **Pinvb   =      mul_mR(Pinv,b, i_mR(CA,C1));          

  clrscrn();
  printf("\n");
  printf(" Find the coefficients a, b, c, d,  of a circle  \n\n");
  printf("     ax**2 + ay**2 + bx + cy + d  = 0            \n\n");
  printf(" that passes through these three xy.         \n\n");
  printf("    x     y");
  p_mR(xy, S5,P0,C6);
  stop();
  
  clrscrn(); 
  printf(" Using the given xy, 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 ");
  pE_mR(Pinv, S12,P4,C10); 
  stop();
  
  clrscrn(); 
  printf(" Pinv = V invS_T U_T "); 
  p_mR(Pinv, S10,P4,C10);  
  printf(" Pinv b ");   
  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(A);
  f_mR(b); 
  f_mR(Pinv);
  f_mR(Pinvb); 

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


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

     ax**2 + ay**2 + bx + cy + d  = 0            

 that passes through these three XY.         

    x     y
  +10   +10 
   -5    +1 
   +7   -10 

 Press return to continue. 


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

 A :
     +1.00      +0.00      +0.00      +0.00      +0.00 
     +0.00      +1.00      +0.00      +0.00      +0.00 
   +100.00    +100.00     +10.00     +10.00      +1.00 
    +25.00      +1.00      -5.00      +1.00      +1.00 
    +49.00    +100.00      +7.00     -10.00      +1.00 

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

 Pinv = V * invS_T * U_T 
 +1.0000e+00  -2.0747e-10  +8.7796e-13  +3.3409e-12  +1.1642e-12 
 +2.4431e-10  +1.0000e+00  -9.3712e-13  -3.4447e-12  -1.3169e-12 
 -3.8132e+00  -7.2527e+00  +4.0293e-02  -7.3260e-02  +3.2967e-02 
 -1.9780e+00  +1.0879e+00  +4.3956e-02  +1.0989e-02  -5.4945e-02 
 -4.2088e+01  -3.8352e+01  +1.5751e-01  +6.2271e-01  +2.1978e-01 

 Press return to continue. 


 Pinv = V * invS_T * U_T 
   +1.0000    -0.0000    +0.0000    +0.0000    +0.0000 
   +0.0000    +1.0000    -0.0000    -0.0000    -0.0000 
   -3.8132    -7.2527    +0.0403    -0.0733    +0.0330 
   -1.9780    +1.0879    +0.0440    +0.0110    -0.0549 
  -42.0879   -38.3516    +0.1575    +0.6227    +0.2198 

 Pinv * b 
   +1.0000 
   +1.0000 
  -11.0659 
   -0.8901 
  -80.4396 

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

  +1.000000000*x^2 +1.000000000*y^2 -11.065934068*x -0.890109889*y -80.439560439 = 0

 Press return to continue.


Copy and paste in Octave:
function xy = f (x,y)
  xy = +1.000000000*x^2 +1.000000000*y^2 -11.065934068*x -0.890109889*y -80.439560439;
endfunction

f (+10,+10)
f (-5,+1)
f (+7,-10)