项目函数
一维插值
double inter_linear(double x0, double x1, double y0, double y1, double x)
{
double a0, a1, y;
a0 = (x - x1) / (x0 - x1);
a1 = (x - x0) / (x1 - x0);
y = a0*y0 + a1*y1;return y;
}
项目函数
二维插值
#include "AP_PBIB.H"
double interp2(double a, double b)
{//一维数组x、y。二维数组。m、n为向量大小。a、b为插值点位置
int i, j, tmpi, tmpj;
double w, w1, w2;int m=31, n=12;double z[31][12]={ //
{ 0.0, 0.1, 0.5, 1.0, 3.0, 5.0, 7.1, 10.1, 14.1, 20.1, 25.1, 30.0},
{ 42.0, 42.1, 42.5, 43.0, 44.9, 46.9, 48.8, 51.7, 55.6, 61.3, 66.1, 70.8},
{ 83.9, 84.0, 84.3, 84.8, 86.7, 88.6, 90.4, 93.2, 97.0, 102.5, 107.1, 111.7},
{ 167.4, 167.5, 167.9, 168.3, 170.1, 171.9, 173.6, 176.3, 179.8, 185.1, 189.4, 193.8},
{2611.3, 251.2, 251.2, 251.9, 253.6, 255.3, 256.9, 259.4, 262.8, 267.8, 272.0, 276.1},
{2649.3, 335.0, 335.3, 335.7, 337.3, 338.8, 340.4, 342.8, 346.0, 350.8, 354.8, 358.7},
{2687.3, 2676.5, 419.4, 419.7, 421.2, 422.7, 424.2, 426.5, 429.5, 434.0, 437.8, 441.6},
{2725.4, 2716.8, 503.9, 504.3, 505.7, 507.1, 508.5, 510.6, 513.5, 517.7, 521.3, 524.9},
{2763.6, 2756.6, 589.2, 589.5, 590.8, 592.1, 593.4, 595.4, 598.0, 602.0, 605.4, 603.1},
{2802.0, 2796.2, 2767.3, 675.7, 676.9, 678.0, 679.2, 681.0, 683.4, 687.1, 690.2, 693.3},
{2840.6, 2835.7, 2812.1, 2777.3, 764.1, 765.2, 766.2, 767.8, 769.9, 773.1, 775.9, 778.7},
{2879.3, 2875.2, 2855.5, 2827.5, 853.0, 853.8, 854.6, 855.9, 857.7, 860.4, 862.8, 856.2},
{2918.3, 2914.7, 2898.0, 2874.9, 943.9, 944.4, 945.0, 946.0, 947.2, 949.3, 951.2, 953.1},
{2957.4, 2954.3, 2939.9, 2920.5, 2823.0, 1037.8, 1038.0, 1038.4, 1039.1, 1040.3, 1041.5, 1024.8},
{2996.8, 2994.1, 2981.5, 2964.8, 2885.5, 1135.0, 1134.7, 1134.3, 1134.1, 1134.0, 1134.3, 1134.8},
{3036.5, 3034.0, 3022.9, 3008.3, 2941.8, 2857.0, 1236.7, 1235.2, 1233.5, 1231.6, 1230.5, 1229.9},
{3076.3, 3074.1, 3064.2, 3051.3, 2994.2, 2925.4, 2839.2, 1343.7, 1339.5, 1334.6, 1331.5, 1329.0},
{3177.0, 3175.3, 3167.6, 3157.7, 3115.7, 3069.2, 3017.0, 2924.2, 2753.5, 1648.4, 1626.4, 1611.3},
{3279.4, 3278.0, 3217.8, 3264.0, 3231.6, 3196.9, 3159.7, 3098.5, 3004.0, 2820.1, 2583.2, 2159.1},
{3321.0, 3319.7, 3313.8, 3306.6, 3276.9, 3245.4, 3211.0, 3156.0, 3072.7, 2917.0, 2730.8, 2424.7},
{3362.5, 3361.4, 3355.9, 3349.3, 3321.9, 3293.2, 3262.3, 3213.5, 3141.4, 3013.9, 2878.3, 2690.3},
{3383.3, 3382.2, 3377.1, 3370.7, 3344.4, 3316.8, 3288.0, 3242.2, 3175.8, 3062.4, 2952.1, 2823.1},
{3404.4, 3403.3, 3398.3, 3392.1, 3366.8, 3340.4, 3312.4, 3268.6, 3205.2, 3098.0, 2994.7, 2875.3},
{3446.7, 3445.6, 3440.9, 3435.1, 3411.6, 3387.2, 3361.3, 3321.3, 3264.1, 3169.1, 3079.8, 2979.6},
{3488.9, 3487.9, 3483.7, 3478.3, 3456.4, 3433.8, 3410.2, 3374.1, 3323.0, 3240.2, 3165.0, 3083.9},
{3531.8, 3530.9, 3526.9, 3521.9, 3501.3, 3480.1, 3458.6, 3425.1, 3378.4, 3303.7, 3237.0, 3166.1},
{3574.7, 3573.9, 3570.1, 3565.4, 3546.2, 3526.4, 3506.4, 3475.4, 3432.5, 3364.6, 3304.7, 3241.7},
{3593.2, 3595.4, 3591.7, 3587.2, 3568.6, 3549.6, 3530.2, 3500.4, 3459.2, 3394.3, 3337.3, 3277.7},
{3618.0, 3617.2, 3613.6, 3609.2, 3591.2, 3572.8, 3554.1, 3525.4, 3485.8, 3423.6, 3369.2, 3312.6},
{3661.6, 3660.9, 3657.5, 3653.3, 3636.3, 3619.1, 3601.6, 3574.9, 3538.2, 3480.9, 3431.2, 3379.8},
{3705.2, 3704.5, 3701.4, 3697.4, 3681.5, 3665.4, 3649.0, 3624.0, 3589.8, 3536.9, 3491.2, 3444.2}}; double x[31]={0,10,20,40,60,80,100,120,140,160,180,200,220,240,260,280,300,350,400,420,440,450,460,480,500,520,540,550,560,580,600};
double y[12]={0.01,0.1,0.5,1,3,5,7,10,14,20,25,30};tmpi = 0;
tmpj = 0;
w = 0.0;
for (i = 0; i<m; i++) //确定a在x轴的位置
{
if ((a <= x[i]) && (a>x[i + 1])) //
{
tmpi = i;
break;
}
if( (a>=x[i])&&(a<x[i+1]) ) //
{
tmpi = i;
break;
}
}
for (j = 0; j<n; j++) //确定b在y轴的位置
{
if ((b >= y[j]) && (b<y[j + 1]))
{
tmpj = j;
break;
}
}
/********x方向进行插值*****************/
if (x[tmpi] == a)
{
//
w1 = z[tmpi][tmpj];
w2 = z[tmpi][tmpj + 1];
/**********/
if (y[tmpj] == b)
{
//
w = w1;
}
else
{
//
w = inter_linear(y[tmpj], y[tmpj + 1], w1, w2, b);
}
}
else
{
//
w1 = inter_linear(x[tmpi], x[tmpi + 1], z[tmpi][tmpj], z[tmpi + 1][tmpj], a);
w2 = inter_linear(x[tmpi], x[tmpi + 1], z[tmpi][tmpj + 1], z[tmpi + 1][tmpj + 1], a);
if (y[tmpj] == b)
{
//
w = w1;
}
else
{
//
w = inter_linear(y[tmpj], y[tmpj + 1], w1, w2, b);
}
}
return (w);
}
动作函数
#include "apdefap.h"int gscAction( void )
{double out = interp2(600, 25);
printf("interp2:%f\r\n", out);return 0;
}