Linear Algebra and the C Language/a0jx


Install and compile this file in your working directory.

/* ------------------------------------ */
/*  Save as :   c00d.c                  */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
/* ------------------------------------ */
#define   RA  R4
#define   CA  C4
#define   CXY C2
/* ------------------------------------ */
int main(void)
{
double tA[RA*CA]={
/* x**3    x**2     x**1   x**0  */
 -125,    +25,     -5,     +1,        
   -8,     +4,     -2,     +1,        
   +8,     +4,     +2,     +1,        
 +125,    +25,     +5,     +1,          
};

double tb[RA*C1]={
/*     y  */
      -8,   
      +8,   
      -8,   
      +8,  
};

double xy[RA*CXY] ={  -5,      -8,
                      -2,       8,
                       2,      -8,
                       5,       8     };
                    
double **XY   = ca_A_mR(xy,i_mR(RA,CXY));
double **A    = ca_A_mR(tA,i_mR(RA,CA));
double **b    = ca_A_mR(tb,i_mR(RA,C1));
double **Inv  =            i_mR(CA,RA);          
double **Invb =            i_mR(CA,C1);       

  clrscrn();
  printf(" Fitting a linear Curve to Data :\n\n");
  printf("    x     y \n");
  p_mR(XY,S5,P0,C6);
  printf(" A :\n x**3     x**2     x**1    x**0");
  p_mR(A,S7,P2,C7);
  printf(" b :\n   y ");
  p_mR(b,S7,P2,C7);
  stop();
  
  clrscrn();    
  printf(" Inv : ");
  invgj_mR(A,Inv); 
  pE_mR(Inv,S12,P4,C10);    
  
  printf(" x = Inv * b ");   
  mul_mR(Inv,b,Invb); 
  p_mR(Invb,S10,P2,C10);
  printf("\n The coefficients a, b, c of the curve are :  \n\n" 
         " y = %+.2fx**3  %+.2fx \n\n"
            ,Invb[R1][C1],Invb[R3][C1]);         
  stop();  

  f_mR(XY);
  f_mR(b);     
  f_mR(A); 
  f_mR(Inv);
  f_mR(Invb); 

  return 0;
}
/* ------------------------------------ */
/* ------------------------------------ */
Presentation :
         Let's calculate the coefficients of a polynomial.
 
              y =  ax**3 + bx**2 + cx + d        
  
        Which passes through these four points.     
          
       x[1],  y[1] 
       x[2],  y[2] 
       x[3],  y[3] 
       x[4],  y[4] 

   Using the points we obtain the matrix:

     x**3        x**2      x**1      x**0      y

     x[1]**3     x[1]**2   x[1]**1   x[1]**0   y[1]
     x[2]**3     x[2]**2   x[2]**1   x[2]**0   y[2]
     x[3]**3     x[3]**2   x[3]**1   x[3]**0   y[3]
     x[4]**3     x[4]**2   x[4]**1   x[4]**0   y[4]

  That we can write:

      x**3       x**2      x      1   y
 
      x[1]**3    x[1]**2   x[1]   1   y[1]
      x[2]**3    x[2]**2   x[2]   1   y[2]
      x[3]**3    x[3]**2   x[3]   1   y[3]
      x[4]**3    x[4]**2   x[4]   1   y[4]
   
     Let's use the invgj_mR() function to solve
     the system that will give us the coefficients a, b, c, d
Screen output example:
 Fitting a linear Curve to Data :

    x     y 

   -5    -8 
   -2    +8 
   +2    -8 
   +5    +8 

 A :
 x**3     x**2     x**1    x**0
-125.00  +25.00   -5.00   +1.00 
  -8.00   +4.00   -2.00   +1.00 
  +8.00   +4.00   +2.00   +1.00 
+125.00  +25.00   +5.00   +1.00 

 b :
   y 
  -8.00 
  +8.00 
  -8.00 
  +8.00 

 Press return to continue. 


 Inv : 
 -4.7619e-03  +1.1905e-02  -1.1905e-02  +4.7619e-03 
 +2.3810e-02  -2.3810e-02  -2.3810e-02  +2.3810e-02 
 +1.9048e-02  -2.9762e-01  +2.9762e-01  -1.9048e-02 
 -9.5238e-02  +5.9524e-01  +5.9524e-01  -9.5238e-02 

 x = Inv * b 
     +0.27 
     +0.00 
     -5.07 
     +0.00 


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

 y = +0.27x**3  -5.07x 

 Press return to continue.
Copy and paste in Octave:
function y = f (x)
  y = +0.266666667*x^3  -5.066666667*x;
endfunction

f (-5) 
f (-2)
f (+2) 
f (+5)