Aller au contenu

Mathc matrices/c22x

Un livre de Wikilivres.


SommaireUtilise la commande "Retour en Arrière" de ton navigateur.


Installer et compiler ces fichiers dans votre répertoire de travail.


c00g.c
/* ------------------------------------ */
/*  Save as :   c00g.c                   */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
/* ------------------------------------ */
#define   RA R5
#define   CA C2
#define   Cb C1 
/* ------------------------------------ */
/* ------------------------------------ */
void fun(void)
{
double ab[RA*(CA+Cb)]={
/* x**0   x**1     y    */
   1,     5.1,     0.19,
   1,     5.3,     0.32,
   1,     5.5,     1.04,
   1,     5.7,     2.47,
   1,     6.0,     3.74,
};

double **Ab = ca_A_mR(ab,i_Abr_Ac_bc_mR(RA,CA,Cb));
double **A  = c_Ab_A_mR(Ab,i_mR(RA,CA));
double **b  = c_Ab_b_mR(Ab,i_mR(RA,Cb));

double **Q    = i_mR(RA,CA);
double **R    = i_mR(CA,CA);

double **invR = i_mR(CA,CA);
double **Q_T  = i_mR(CA,RA);


double **invR_Q_T = i_mR(CA,RA);
double **x        = i_mR(CA,Cb); // x invR * Q_T * b

  clrscrn();
  printf(" Fitting a linear Curve to Data :\n\n");
  printf(" A :");
  p_mR(A,S5,P2,C7);
  printf(" b :");
  p_mR(b,S5,P2,C7);
  printf(" Ab :");
  p_mR(Ab,S5,P2,C7);
  stop();
    
  clrscrn();
  QR_mR(A,Q,R);    
  printf(" Q :");
  p_mR(Q,S3,P4,C6);  
  printf(" R :");
  p_mR(R,S10,P4,C6);
  stop();

  clrscrn();
  transpose_mR(Q,Q_T);   
  printf(" Q_T :");
  pE_mR(Q_T,S3,P4,C6); 
  inv_mR(R,invR); 
  printf(" invR :");
  pE_mR(invR,S10,P4,C6);
  stop();
  
  clrscrn();
  printf(" Solving this system yields a unique\n"
         " least squares solution, namely   \n\n");
  mul_mR(invR,Q_T,invR_Q_T);
  mul_mR(invR_Q_T,b,x);
  printf(" x = invR * Q_T * b :");
  p_mR(x,S10,P2,C6);
  printf(" The linear Curve to Data : \n\n"
         "  s = %+.2f %+.2f*t \n\n"
            ,x[R1][C1],x[R2][C1]);  
  
  stop();
  
  f_mR(A);
  f_mR(b);
  f_mR(Ab);
  f_mR(Q);
  f_mR(Q_T);
  f_mR(R);
  f_mR(invR);  
  f_mR(invR_Q_T); 
  f_mR(x); 
}
/* ------------------------------------ */
int main(void)
{
	
  fun();

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


Trouver la meilleur équation linéaire qui s'ajuste au mieux aux points donnés


Exemple de sortie écran :
 -----------------------------------
 Fitting a linear Curve to Data :

 A :
+1.00 +5.10 
+1.00 +5.30 
+1.00 +5.50 
+1.00 +5.70 
+1.00 +6.00 

 b :
+0.19 
+0.32 
+1.04 
+2.47 
+3.74 

 Ab :
+1.00 +5.10 +0.19 
+1.00 +5.30 +0.32 
+1.00 +5.50 +1.04 
+1.00 +5.70 +2.47 
+1.00 +6.00 +3.74 

 Press return to continue. 

 -----------------------------------
 Q :
+0.4472 -0.6012 
+0.4472 -0.3149 
+0.4472 -0.0286 
+0.4472 +0.2577 
+0.4472 +0.6871 

 R :
   +2.2361   +12.3431 
   +0.0000    +0.6986 

 Press return to continue. 


 -----------------------------------
 Q_T :
+4.4721e-01 +4.4721e-01 +4.4721e-01 +4.4721e-01 +4.4721e-01 
-6.0123e-01 -3.1493e-01 -2.8630e-02 +2.5767e-01 +6.8712e-01 

 invR :
+4.4721e-01 -7.9019e+00 
-0.0000e+00 +1.4315e+00 

 Press return to continue. 

 -----------------------------------

 Solving this system yields a unique
 least squares solution, namely   

 x = invR * Q_T * b :
    -21.85 
     +4.24 

 The linear Curve to Data : 

  s = -21.85 +4.24*t