Linear Algebra and the C Language/a0bj
Install and compile this file in your working directory.
/* ------------------------------------ */
/* Save as : c00b.c */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
/* ------------------------------------ */
#define RA R3
#define CA C3
#define Cb C1
/* ------------------------------------ */
/* ------------------------------------ */
int main(void)
{
double xy[6] ={1, -9,
2, 8,
3, -8, };
double ab[RA*(CA+Cb)]={
/* x**2 x**1 x**0 y */
+1, +1, +1, -9,
+4, +2, +1, +8,
+9, +3, +1, -8,
};
double **XY = ca_A_mR(xy,i_mR(R3,C2));
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("\n");
printf(" Find the coefficients a, b, c of the curve \n\n");
printf(" y = ax**2 + bx + c (x**0 = 1) \n\n");
printf(" that passes through the points. \n\n");
printf(" x y");
p_mR(XY,S5,P0,C6);
printf(" Using the given points, we obtain this matrix.\n");
printf(" x**2 x**1 x**0 y\n");
p_mR(Ab,S7,P2,C6);
stop();
clrscrn();
QR_mR(A,Q,R);
printf(" Q :");
p_mR(Q,S10,P4,C6);
printf(" R :");
p_mR(R,S10,P4,C6);
transpose_mR(Q,Q_T);
printf(" Q_T :");
pE_mR(Q_T,S12,P4,C6);
inv_mR(R,invR);
printf(" invR :");
pE_mR(invR,S12,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("\n The coefficients a, b, c of the curve are : \n\n"
" y = %+.2fx**2 %+.2fx %+.2f\n\n"
,x[R1][C1],x[R2][C1],x[R3][C1]);
stop();
f_mR(XY);
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);
return 0;
}
/* ------------------------------------ */
/* ------------------------------------ */
Presentation :
Let's calculate the coefficients of a polynomial.
y = ax**2 + bx + c
Which passes through these three points.
x[1], y[1]
x[2], y[2]
x[3], y[3]
Using the points we obtain the matrix:
x**2 x**1 x**0 y
x[1]**2 x[1]**1 x[1]**0 y[1]
x[2]**2 x[2]**1 x[2]**0 y[2]
x[3]**2 x[3]**1 x[3]**0 y[3]
That we can write:
x**2 x 1 y
x[1]**2 x[1] 1 y[1]
x[2]**2 x[2] 1 y[2]
x[3]**2 x[3] 1 y[3]
Let's use the QR_mR(); function to solve
the system that will give us the coefficients a, b, c
Screen output example:
Find the coefficients a, b, c of the curve
y = ax**2 + bx + c (x**0 = 1)
that passes through the points.
x y
+1 -9
+2 +8
+3 -8
Using the given points, we obtain this matrix.
x**2 x**1 x**0 y
+1.00 +1.00 +1.00 -9.00
+4.00 +2.00 +1.00 +8.00
+9.00 +3.00 +1.00 -8.00
Press return to continue.
Q :
+0.1010 +0.7184 +0.6882
+0.4041 +0.6025 -0.6882
+0.9091 -0.3476 +0.2294
R :
+9.8995 +3.6365 +1.4142
-0.0000 +0.8806 +0.9733
+0.0000 +0.0000 +0.2294
Q_T :
+1.0102e-01 +4.0406e-01 +9.0914e-01
+7.1841e-01 +6.0254e-01 -3.4762e-01
+6.8825e-01 -6.8825e-01 +2.2942e-01
invR :
+1.0102e-01 -4.1714e-01 +1.1471e+00
+0.0000e+00 +1.1355e+00 -4.8177e+00
-0.0000e+00 -0.0000e+00 +4.3589e+00
Press return to continue.
Solving this system yields a unique
least squares solution, namely
x = invR * Q_T * b :
-16.50
+66.50
-59.00
The coefficients a, b, c of the curve are :
y = -16.50x**2 +66.50x -59.00
Press return to continue.
Copy and paste in Octave:
function y = f (x)
y = -16.50*x^2 +66.50*x -59.00;
endfunction
f (+1)
f (+2)
f (+3)