Центрально-разностные аппроксимации в C - PullRequest
0 голосов
/ 13 декабря 2010

Я сталкивался с этим вопросом, когда готовился к выпускным экзаменам, и я не могу заставить его работать.Сам вопрос показан ниже.Буду очень признателен за любую помощь в решении этой проблемы.

Ниже приведен код аналогичного вопроса, который я задал.Я надеюсь, что это может быть использовано в качестве основы для решения этого вопроса

    #include <stdio.h>
    #include <stdlib.h>
    #include <cmath>
    using namespace std;

        double f(double x)
      {
           return (cos(x));
      }


    /*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
     Numerical Differentiation Formulae (n-th derivative)
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
    void dnf_dxn (int n, double x, double h, double& fd, double& cd)
  {

    if(n==1)
         // Approximation to the 1st Derivative of f at x
      {
         //  1st order forward differencing
         fd = ( f(x+h) - f(x) ) / h;

         //  2nd order centered differencing
         cd = ( f(x+h) - f(x-h) ) / (2*h);

      }

    else if(n==2)
         // Approximation to the 2nd Derivative of f at x
      {
         //  1st order forward differencing
         fd = ( f(x+2*h) - 2*f(x+h) + f(x) ) / (h*h);

         //  2nd order centered differencing
         cd = ( f(x+h) - 2*f(x) + f(x-h) ) / (h*h);

      }

    else if(n==3)
         // Approximation to the 3rd Derivative of f at x
      {
         //  1st order forward differencing
         fd = ( f(x+3*h) - 3*f(x+2*h) + 3*f(x+h) - f(x) ) / (h*h*h);

         //  2nd order centered differencing
         cd = ( f(x+2*h) - 2*f(x+h) + 2*f(x-h) - f(x-2*h) ) / (2*h*h*h);

      }

    else
      {
        printf("Only derivatives of orders 1, 2 and 3 are implemented. \n");
        getchar();
        exit(1);
      }

  }



    /*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
         NUM_DIFF       M A I N      P R O G R A M
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
    int main()
  {

    printf("\n Numerical Differentiation of f(x)=cos(x) at x=1  \n \n");
    printf(" Derivative of order 1, 2 and 3 using forward       \n");
    printf(" and centered difference approximations (h=0.01):   \n \n");


    double x =   0.5;
    double h =   0.01;
    int n;
    double fd, cd, exact, cd_error, fd_error;
    double true_fx   = - sin(x);
    double true_fxx  = - cos(x);
    double true_fxxx =   sin(x);


    printf("Derivative  Stepsize Differencing      Result   Abs Error \n");

    for(n=1; n<4; n++)
      {

        dnf_dxn (n, x, h, fd, cd);

        if(n==1)
          { exact = true_fx; }
        else if(n==2)
          { exact = true_fxx; }
        else
          { exact = true_fxxx; }

        fd_error = abs(exact - fd);
        cd_error = abs(exact - cd);
        printf("     %i        %4.2f     Forward     %10.7f  %10.3e \n",
                  n, h, fd, fd_error);
        printf("                       Centered    %10.7f  %10.3e \n",
                        cd, cd_error);

      }

    printf("\n \n <Press the RETURN key to exit num_diff.cpp> \n \n");
    getchar();

  }

Вот актуальный вопрос: The assignment

1 Ответ

1 голос
/ 13 декабря 2010

Ну, например, вы говорите, что вы рассчитываете эти различия на x = 1, но вы фактически рассчитываете их на x = 0.5.

Недостаточно сказать «это не работает».Чего вы ожидаете и что получаете вместо этого?

...