C 语言的中心差分近似

发布于 2024-10-07 17:44:42 字数 3002 浏览 3 评论 0原文

我在期末学习时遇到了这个问题,但我似乎无法解决它。问题本身如下所示。任何有关如何解决此问题的帮助将不胜感激。

下面是我解决的类似问题的代码。我希望它可以作为解决这个问题的基础

    #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();

  }

这是实际的问题: 作业

I came across this question when studying for finals, and I can't seem to get it to work. The question itself is shown below. Any help regarding how to tackle this would be greatly appreciated.

Below is the code from a similar question I tackled. I hope it can be used as a basis on tackling this question

    #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();

  }

Here is the actual question:
The assignment

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

会傲 2024-10-14 17:44:42

嗯,首先,您说您是在 x = 1 处计算这些差异,但实际上是在 x = 0.5 处计算它们。

仅仅说“它不起作用”是不够的。你期待什么,你会得到什么?

Well, for one, you say that you are calculating these differences at x = 1 but you are actually calculating them at x = 0.5.

It's not enough to say "it doesn't work." What are you expecting, and what are you getting instead?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文