2

私はコーディングにかなり慣れておらず、3次スプラインを補間するコードを作成していますが、最後の方程式で次のエラーが発生します。 「」

問題は、最初の2行に「population=」が付いたコードの最後のビットにあります。誰かが私を正しい方向に向けてくれたら本当にありがたいです。

#include <stdio.h>


main () {

    int x;
    int y;
    double stats[10][2];
    double gpp[8][9] = {0};
    double gppr[10] = {0};
    double year;
    double population;
    int xi = 0;
    year=1950;
    population=0;
    x=0;

    stats[0][0] = 1930; stats[1][0] = 1940; stats[2][0] = 1949;
    stats[3][0] = 1955; stats[4][0] = 1960; stats[5][0] = 1970;
    stats[6][0] = 1980; stats[7][0] = 1990; stats[8][0] = 2000;
    stats[9][0] = 2005;

    stats[0][1] = 21.058; stats[1][1] = 23.547; stats[2][1] = 20.167;
    stats[3][1] = 21.502; stats[4][1] = 24.989; stats[5][1] = 30.852; 
    stats[6][1] = 37.407; stats[7][1] = 43.390; stats[8][1] = 45.985;
    stats[9][1] = 47.041;

    //Initiate  g'' system of equation
    for (x=0;x<8;x++) {
        gpp[x][x] = ((stats[x+1][0]-stats[x][0])+(stats[x+2][0]-stats[x+1][0]))/3;
        if (x<7) {
            gpp[x][x+1] = (stats[x+2][0]-stats[x+1][0])/6;
        }
        if (x>0) {
            gpp[x][x-1] = (stats[x+2][0]-stats[x+1][0])/6;
        }
        gpp[x][8] = ((stats[x+2][1]-stats[x+1][1])/(stats[x+2][0]-stats[x+1][0]))-((stats[x+1][1]-stats[x][1])/(stats[x+1][0]-stats[x][0]));
    }

    //Forward sweep
    for (x=0;x<7;x++) {
        gpp[x+1][x] = 0;
        gpp[x+1][x+1] = gpp[x+1][x+1] - (gpp[x][x+1]/gpp[x][x])*gpp[x+1][x];
        gpp[x+1][8] = gpp[x+1][8] - (gpp[x][x+1]/gpp[x][x])*gpp[x][8];
    }
    //Backward sweep
    gppr[9] = 0;gppr[0] = 0;
    gppr[8] = gpp[7][8]/gpp[7][7];
    for (x=7;x > 0;x=x-1) {
        gppr[x] = (gpp[x][8]-(gppr[x+1]*gpp[x][x+1]))/gpp[x][x];
    }

    //check where is xi
    for (x=0;x<10;x++) {
        if (stats[x][0] > year) {
            xi = x;
            break;
        }
    }

//Calculate population at x
population = (gppr[xi]/6)*((((stats[xi+1][0]-year)^3.0)/(stats[xi+1][0]-stats[xi][0]))-(stats[xi+1][0]-stats[xi][0])*((stats[xi+1][0]-year)))
       + (gppr[xi+1]/6)*((((year-stats[xi][0])^3.0)/(stats[xi+1][0]-stats[xi][0]))-(stats[xi+1][0]-stats[xi][0])*((year-stats[xi+1][0])))
       + (stats[xi][1])*((stats[xi+1][0]-year)/(stats[xi+1][0]-stats[xi][0]))
       + (stats[xi+1][1])*((year-stats[xi][0])/(stats[xi+1][0]-stats[xi][0]));

}

Cについてもう少し学ぶのを楽しみにしています!

ヒューゴ

4

3 に答える 3

6

^はCの排他的論理和演算子であり、doubleには適していません。これはビット演算子であり、ここで(および他のビット演算子)の詳細を見つけることができます。

ある数値を別の数値の累乗にしたい場合は、pow()関数が必要です。

だから次のようなもの:

((stats[xi+1][0]-year)^3.0)

実際に書く必要があります:

pow (stats[xi+1][0] - year, 3.0)

7.12.7.4 The pow functions最新の(C11)標準のセクションは次のように述べています。

あらすじ:
#include <math.h>
double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);

説明:
pow関数は、xをyの累乗で計算します。xが有限で負であり、yが有限で整数値でない場合、ドメインエラーが発生します。レンジエラーが発生する場合があります。xがゼロでyがゼロの場合、ドメインエラーが発生する可能性があります。xがゼロで、yがゼロ未満の場合、ドメインエラーまたは極エラーが発生する可能性があります。

pow関数はxyを返します。

于 2012-10-03T05:28:20.287 に答える
3

Cには指数演算子がありません。^はXOR演算子であり、非整数では機能しないため、エラーが発生します。pow代わりに関数を使用してください。

于 2012-10-03T05:27:59.017 に答える
0

最後の方程式では、数学のべき関数を見つけようとしていると思います。しかし、c "^"では、この演算子は排他的論理和を意味します。#include>math.h>を試してみてください

于 2012-10-03T05:32:36.940 に答える