arrays - 2D interpolation in C -
so program supposed ask user temperature in kelvins , starting ne. , interpolate find given value.
the temp
array temperatures , used find temperature between.
the n
array ne's , used find ne between.
whatever spots in each end being spot interpolating in bigger array, hpe.
the main function ask user temperature , ne , calls upon interpolation function.
the interpol function function interpolates. first finds given temperature in temp array. finds given ne in n array. uses spot (hpe[k][l]) 2d interpolation.
my problem when run , enter values should giving me answer, "the value determined nan
"
what doing wrong?
#include <stdio.h> #include <stdlib.h> #include <math.h> double interpol(double ti, double ne); int main() { int ni; double ti, result, ne; printf("\nenter temperature in kelvins , ne separated space.\n"); ni = scanf("%lf %lf", &ti, &ne); result = interpol(ti, ne); printf("\nthe value determined %lf\n", result); } double interpol(double ti, double ne) { int i, j, k, l; double y1, y2, y3, y4, t, u, p; double temp[15] = { 1000.0, 2000.0, 3000.0, 4000.0, 5000.0, 6000.0, 7000.0, 8000.0, 9000.0, 10000.0, 11000.0, 12000.0, 13000.0, 14000.0, 15000.0}; double n[8] = {10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0}; double hpe[15][8] = { {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, {0.0017, 0.0002, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, {0.2980, 0.0407, 0.0042, 0.0004, 0.0, 0.0, 0.0, 0.0}, {0.9582, 0.6961, 0.1864, 0.0224, 0.0023, 0.0002, 0.0, 0.0}, {0.9979, 0.9791, 0.8241, 0.3191, 0.0448, 0.0047, 0.0005, 0.0}, {0.9998, 0.9980, 0.9804, 0.8335, 0.3335, 0.0477, 0.0050, 0.0005}, {1.0, 0.9997, 0.9971, 0.9713, 0.7719, 0.2529, 0.0327, 0.0034}, {1.0, 0.9999, 0.9994, 0.9939, 0.9425, 0.6211, 0.1408, 0.0161}, {1.0, 1.0, 0.9998, 0.9984, 0.9841, 0.8606, 0.3817, 0.0581}, {1.0, 1.0, 0.9999, 0.9995, 0.9948, 0.9503, 0.6568, 0.1607}, {1.0, 1.0, 1.0, 0.9998, 0.9980, 0.9807, 0.8358, 0.3373}, {1.0, 1.0, 1.0, 0.9999, 0.9992, 0.9917, 0.9229, 0.5448} }; (i = 1 ; < 16 ; i++) { if (temp[i] <= ti && temp[i+1] >= ti) { k = i; break; } } (j = 1 ; j < 9 ; j++) { if (n[j] <= ne && n[j+1] >= ne) { l = j; break; } } y1 = hpe[k][l]; y2 = hpe[k+1][l]; y3 = hpe[k+1][l+1]; y4 = hpe[k][l+1]; t = (ti - (hpe[k][0])) / ((hpe[k+1][0]) - (hpe[k][0])); u = (ne - (hpe[0][l])) / ((hpe[0][l+1]) - (hpe[0][l])); p = ((1 - t) * (1 - u) * y1) + (t * (1 - u) * y2) + (t * u * y3) + ((1 - t) * u * y4); return (p); }
you have obvious mistake c programmer, arrays of size n
in c indexed 0
n - 1
. there languages array of size n
indexed 1
n
pascal example, in c it's 0
n - 1
.
Comments
Post a Comment