Linear Algebra and the C Language/a07k


Install and compile this file in your working directory.

/* ------------------------------------ */
/*  Save as :   c00d.c                  */
/* ------------------------------------ */
#include   "v_a.h"
#include "dpoly.h"
/* ------------------------------------ */
int main(void)
{
double   xy[8] ={  -5,      -8,
                   -2,       8,
                    2,      -8,
                    5,       8     };

double **XY =  ca_A_mR(xy,i_mR(R4,C2));
double **A  =             i_mR(R4,C4);
double **b =              i_mR(R4,C1);
double **Ab =   i_Abr_Ac_bc_mR(R4,C4,C1);

  clrscrn();
  printf("\n");
  printf(" Find the coefficients a, b, c  of the curve \n\n");
  printf("      y =  ax**3 + bx**2 + cx + d            \n\n");
  printf(" that passes through the points.             \n\n");

  printf("    x     y \n");
  p_mR(XY,S5,P0,C6);
  printf("\n Using the given points, we obtain this matrix\n\n");
  printf("   x**3    x**2    x**1    x**0     y\n");
  i_A_b_with_XY_mR(XY,A,b);
  c_A_b_Ab_mR(A,b,Ab);
  p_mR(Ab,S7,P2,C6);
  stop();

  clrscrn();
  printf(" The Gauss Jordan process will reduce this matrix to : \n");
  gj_TP_mR(Ab);
  p_mR(Ab,S7,P2,C6);
  printf("\n The coefficients a, b, c of the curve are :  \n\n");
  p_eq_poly_mR(Ab);
  stop();

  clrscrn();
  printf("    x     y \n");
  p_mR(XY,S5,P0,C6);
  printf("\n");

  printf(" Verify the result : \n\n");
  verify_X_mR(Ab,XY[R1][C1]);
  verify_X_mR(Ab,XY[R2][C1]);
  verify_X_mR(Ab,XY[R3][C1]);
  verify_X_mR(Ab,XY[R4][C1]);
  printf("\n\n\n");
  stop();

  f_mR(XY);
  f_mR(A);
  f_mR(b);
  f_mR(Ab);

  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 gj_TP_mR() function to solve
     the system that will give us the coefficients a, b, c, d


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

      y =  ax**3 + bx**2 + cx + d            

 that passes through the points.             

    x     y 

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


 Using the given points, we obtain this matrix

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

-125.00  +25.00   -5.00   +1.00   -8.00 
  -8.00   +4.00   -2.00   +1.00   +8.00 
  +8.00   +4.00   +2.00   +1.00   -8.00 
+125.00  +25.00   +5.00   +1.00   +8.00 

 Press return to continue. 


 The Gauss Jordan process will reduce this matrix to : 

  +1.00   +0.00   +0.00   +0.00   +0.27 
  +0.00   +1.00   +0.00   +0.00   +0.00 
  +0.00   +0.00   +1.00   +0.00   -5.07 
  +0.00   +0.00   +0.00   +1.00   +0.00 


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

  y =  +0.267x**3 -5.067x


 Press return to continue. 


    x     y 

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


 Verify the result : 

 With x =  -5.000,       y = -8.000 
 With x =  -2.000,       y = +8.000 
 With x =  +2.000,       y = -8.000 
 With x =  +5.000,       y = +8.000 



 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)