|
| 1 | +#ifndef interpolate_h |
| 2 | +#define interpolate_h |
| 3 | + |
| 4 | +#include <math.h> |
| 5 | + |
| 6 | +// This functions finds the determinant of Matrix |
| 7 | +double determinantOfMatrix(double mat[3][3]) { |
| 8 | + double ans; |
| 9 | + ans = mat[0][0] * (mat[1][1] * mat[2][2] - mat[2][1] * mat[1][2]) |
| 10 | + - mat[0][1] * (mat[1][0] * mat[2][2] - mat[1][2] * mat[2][0]) |
| 11 | + + mat[0][2] * (mat[1][0] * mat[2][1] - mat[1][1] * mat[2][0]); |
| 12 | + return ans; |
| 13 | +} |
| 14 | + |
| 15 | +// This function finds the solution of system of |
| 16 | +// linear equations using cramer's rule |
| 17 | +void findSolution(double coeff[3][4], double solution[3]) { |
| 18 | + // Matrix d using coeff as given in cramer's rule |
| 19 | + double d[3][3] = { |
| 20 | + { coeff[0][0], coeff[0][1], coeff[0][2] }, |
| 21 | + { coeff[1][0], coeff[1][1], coeff[1][2] }, |
| 22 | + { coeff[2][0], coeff[2][1], coeff[2][2] }, |
| 23 | + }; |
| 24 | + // Matrix d1 using coeff as given in cramer's rule |
| 25 | + double d1[3][3] = { |
| 26 | + { coeff[0][3], coeff[0][1], coeff[0][2] }, |
| 27 | + { coeff[1][3], coeff[1][1], coeff[1][2] }, |
| 28 | + { coeff[2][3], coeff[2][1], coeff[2][2] }, |
| 29 | + }; |
| 30 | + // Matrix d2 using coeff as given in cramer's rule |
| 31 | + double d2[3][3] = { |
| 32 | + { coeff[0][0], coeff[0][3], coeff[0][2] }, |
| 33 | + { coeff[1][0], coeff[1][3], coeff[1][2] }, |
| 34 | + { coeff[2][0], coeff[2][3], coeff[2][2] }, |
| 35 | + }; |
| 36 | + // Matrix d3 using coeff as given in cramer's rule |
| 37 | + double d3[3][3] = { |
| 38 | + { coeff[0][0], coeff[0][1], coeff[0][3] }, |
| 39 | + { coeff[1][0], coeff[1][1], coeff[1][3] }, |
| 40 | + { coeff[2][0], coeff[2][1], coeff[2][3] }, |
| 41 | + }; |
| 42 | + |
| 43 | + // Calculating Determinant of Matrices d, d1, d2, d3 |
| 44 | + double D = determinantOfMatrix(d); |
| 45 | + double D1 = determinantOfMatrix(d1); |
| 46 | + double D2 = determinantOfMatrix(d2); |
| 47 | + double D3 = determinantOfMatrix(d3); |
| 48 | + |
| 49 | + // Case 1 |
| 50 | + if (D != 0) { |
| 51 | + // Coeff have a unique solution. Apply Cramer's Rule |
| 52 | + double x = D1 / D; |
| 53 | + double y = D2 / D; |
| 54 | + double z = D3 / D; |
| 55 | + solution[0] = x; |
| 56 | + solution[1] = y; |
| 57 | + solution[2] = z; |
| 58 | + } |
| 59 | + // Case 2 |
| 60 | + else { |
| 61 | + if (D1 == 0 && D2 == 0 && D3 == 0) |
| 62 | + printf("Infinite solutions\n"); |
| 63 | + else if (D1 != 0 || D2 != 0 || D3 != 0) |
| 64 | + printf("No solutions\n"); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +void interpolate_min(float off1, float val1, float off2, float val2, float off3, float val3, float *min_out) { |
| 69 | + double coeff[3][4] = { |
| 70 | + { powf(off1, 2), off1, 1, val1 }, |
| 71 | + { powf(off2, 2), off2, 1, val2 }, |
| 72 | + { powf(off3, 2), off3, 1, val3 }, |
| 73 | + }; |
| 74 | + |
| 75 | + double solution[3] = { 0, 0, 0 }; |
| 76 | + |
| 77 | + findSolution(coeff, solution); |
| 78 | + |
| 79 | + *min_out = (-1 * solution[1]) / (2 * solution[0]); // minimum of quadratic function (-b / 2a) |
| 80 | +} |
| 81 | + |
| 82 | +#endif /* interpolate_h */ |
0 commit comments