Linear Algebra and the C Language/a0bn
Install and compile this file in your working directory.
/* ------------------------------------ */
/* Save as : c00f.c */
/* ------------------------------------ */
#include "v_a.h"
/* ------------------------------------ */
/* ------------------------------------ */
#define RA R5
#define CA C5
#define Cb C1
/* ------------------------------------ */
/* ------------------------------------ */
int main(void)
{
double xy[10] ={
1, -2,
2, -2,
3, 3,
4, -9,
5, 4, };
double ab[RA*(CA+Cb)]={
/* x**4 x**3 x**2 x**1 x**0 y */
+1, +1, +1, +1, +1, -2,
+16, +8, +4, +2, +1, -2,
+81, +27, +9, +3, +1, +3,
+256, +64, +16, +4, +1, -9,
+625, +125, +25, +5, +1, +4,
};
double **XY = ca_A_mR(xy,i_mR(R5,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**4 + bx**3 + cx**2 + dx + e \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**4 x**3 x**2 x**1 x**0 y");
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);
stop();
clrscrn();
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**4 %+.2fx**3 %+.2fx**2 %+.2fx %+.2f\n\n"
,x[R1][C1],x[R2][C1],x[R3][C1],x[R4][C1],x[R5][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**4 + bx**3 + cx**2 + dx + e
Which passes through these five points.
x[1], y[1]
x[2], y[2]
x[3], y[3]
x[4], y[4]
x[5], y[5]
Using the points we obtain the matrix:
x**4 x**3 x**2 x**1 x**0 y
x[1]**4 x[1]**3 x[1]**2 x[1]**1 x[1]**0 y[1]
x[2]**4 x[2]**3 x[2]**2 x[2]**1 x[2]**0 y[2]
x[3]**4 x[3]**3 x[3]**2 x[3]**1 x[3]**0 y[3]
x[4]**4 x[4]**3 x[4]**2 x[4]**1 x[4]**0 y[4]
x[5]**4 x[5]**3 x[5]**2 x[5]**1 x[5]**0 y[5]
That we can write:
x**4 x**3 x**2 x 1 y
x[1]**4 x[1]**3 x[1]**2 x[1] 1 y[1]
x[2]**4 x[2]**3 x[2]**2 x[2] 1 y[2]
x[3]**4 x[3]**3 x[3]**2 x[3] 1 y[3]
x[4]**4 x[4]**3 x[4]**2 x[4] 1 y[4]
x[5]**4 x[5]**3 x[5]**2 x[5] 1 y[5]
Let's use the QR_mR() function to solve
the system that will give us the coefficients a, b, c, d, e
Screen output example:
Find the coefficients a, b, c of the curve
y = ax**4 + bx**3 + cx**2 + dx + e
that passes through the points.
x y
+1 -2
+2 -2
+3 +3
+4 -9
+5 +4
Using the given points, we obtain this matrix.
x**4 x**3 x**2 x**1 x**0 y
+1.00 +1.00 +1.00 +1.00 +1.00 -2.00
+16.00 +8.00 +4.00 +2.00 +1.00 -2.00
+81.00 +27.00 +9.00 +3.00 +1.00 +3.00
+256.00 +64.00 +16.00 +4.00 +1.00 -9.00
+625.00 +125.00 +25.00 +5.00 +1.00 +4.00
Press return to continue.
Q :
+0.0015 +0.0485 +0.4216 +0.8487 +0.3156
+0.0235 +0.2856 +0.7083 -0.1335 -0.6312
+0.1190 +0.6174 +0.2365 -0.3877 +0.6312
+0.3762 +0.6420 -0.4915 +0.3242 -0.3156
+0.9185 -0.3504 +0.1519 -0.0805 +0.0631
R :
+680.4256 +142.3006 +30.1502 +6.5033 +1.4388
+0.0000 +16.2950 +8.2602 +3.2880 +1.2431
-0.0000 -0.0000 +1.3158 +1.3410 +1.0268
+0.0000 +0.0000 +0.0000 +0.3128 +0.5711
-0.0000 -0.0000 -0.0000 -0.0000 +0.0631
Press return to continue.
Q_T :
+1.4697e-03 +2.3515e-02 +1.1904e-01 +3.7624e-01 +9.1854e-01
+4.8534e-02 +2.8560e-01 +6.1737e-01 +6.4201e-01 -3.5037e-01
+4.2165e-01 +7.0827e-01 +2.3651e-01 -4.9150e-01 +1.5186e-01
+8.4868e-01 -1.3354e-01 -3.8774e-01 +3.2419e-01 -8.0475e-02
+3.1560e-01 -6.3119e-01 +6.3119e-01 -3.1560e-01 +6.3119e-02
invR :
+1.4697e-03 -1.2834e-02 +4.6895e-02 -9.6700e-02 +3.3138e-01
+0.0000e+00 +6.1368e-02 -3.8527e-01 +1.0067e+00 -4.0502e+00
-0.0000e+00 -0.0000e+00 +7.6002e-01 -3.2586e+00 +1.7121e+01
+0.0000e+00 +0.0000e+00 +0.0000e+00 +3.1973e+00 -2.8930e+01
-0.0000e+00 -0.0000e+00 -0.0000e+00 +0.0000e+00 +1.5843e+01
Press return to continue.
Solving this system yields a unique
least squares solution, namely
x = invR * Q_T * b :
+2.67
-30.33
+117.83
-181.17
+89.00
The coefficients a, b, c of the curve are :
y = +2.67x**4 -30.33x**3 +117.83x**2 -181.17x +89.00
Press return to continue.
Copy and paste in Octave:
function y = f (x)
y = +2.666665810*x^4 -30.333323719*x^3 +117.833298608*x^2 -181.166625268*x +88.999995106;
endfunction
f (+1)
f (+2)
f (+3)
f (+4)
f (+5)