Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 其中是使用Horner'的多项式导数函数中的误差;什么方法?_C_Numerical Methods_Dev C++ - Fatal编程技术网

C 其中是使用Horner'的多项式导数函数中的误差;什么方法?

C 其中是使用Horner'的多项式导数函数中的误差;什么方法?,c,numerical-methods,dev-c++,C,Numerical Methods,Dev C++,我使用霍纳斯方法来创建代码,可以计算多项式,以及它对给定的任何一组系数的导数。当我对值进行硬编码时,Horner的方法工作得很好,但当我更改代码以从命令行提示符获取任何输入时,Horner的导数开始打印出带有几十个零的疯狂数字。但霍纳的多项式方法仍然在正确计算中。我不知道这段代码中的错误在哪里 #include<stdio.h> #include<math.h> #include<stdlib.h> double horner(d

我使用霍纳斯方法来创建代码,可以计算多项式,以及它对给定的任何一组系数的导数。当我对值进行硬编码时,Horner的方法工作得很好,但当我更改代码以从命令行提示符获取任何输入时,Horner的导数开始打印出带有几十个零的疯狂数字。但霍纳的多项式方法仍然在正确计算中。我不知道这段代码中的错误在哪里

    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>

  double horner(double *coeffs, int s, double x)
{
  int i;
  double res = 0.0;

  for(i=s-1; i >= 0; i--)
  {
    res = res * x + coeffs[i];
  }
  return res;
}

double hornerDerivative(double *coeffs, int s_1, double x_1)
{
  int i_1;
  double res_1 = 0.0;

  for(i_1 = s_1; i_1 >= 1; i_1--)
  {
    res_1 = res_1 * x_1 + i_1*coeffs[i_1];
  }
  return res_1;
}

double newton(double *coeffs, double a, double b, double eps)
{
       int N = 0;
       double c = ((a+b)/2);

       while(N < 10)
       {
               double y = horner(coeffs, sizeof(coeffs), c);
               double y_derivative = hornerDerivative(coeffs, sizeof(coeffs), c);
               double c_1 = c - (y/y_derivative);
               printf("c: %f\t",c);
               printf("y: %f\t",y);
               printf("y_der: %f\t",y_derivative);
               printf("c_1: %f\n",c_1);
               if(fabs(c_1-c)<eps)
               {
                    return c_1;                   
               }
               c = c_1;
               N = N + 1;
       }
}

int main(int argc, char **argv)
{
  printf("# of arguments%d\n\n\n\n", argc);
  double coeffs[argc-3];

  double a = atof(argv[argc-2]);
  double b = atof(argv[argc-1]);
  double eps = .001;

  int i;
  for(i=1; i < argc-2; i++)
  {
           coeffs[i-1] = atof(argv[argc-2-i]);
  }

  printf("The root of the equation is: %f\n", newton(coeffs, a, b, eps));
  system("pause");
  return 0;
}

正如你所看到的,霍纳的方法计算多项式很好,但它没有正确地计算导数。无论我使用什么样的例子来计算教授给我的列表中的系数,这都是相同的错误

如前所述,代码中存在一些问题:

  • 使用
    sizeof

    • sizeof
      将以字节为单位返回的大小
    • sizeof
      将返回系统上指针的大小,通常为4(32位拱形)或8(64位拱形)
    请看以下代码:

    void foo(int * p)
    {
        printf("sizeof pointer is %z\n", sizeof p);
    }   
    void bar()
    {
        int a[256];
        printf("sizeof array is %z\n", sizeof a);
        foo(a);
    }
    
    它将打印:

    sizeof array is 1024
    sizeof pointer is 8
    
    所以不能使用
    sizeof(coefs)

  • 当牛顿的方法收敛超过10步时,你如何处理?如果条件
    fabs(c_1-c)=0,则应该返回一些值;我——)
    {
    res=res*x+系数[i];
    }
    返回res;
    }
    双角导数(双*系数,整数s_1,双x_1)
    {
    int i_1;
    双精度1=0.0;
    对于(i_1=s_1;i_1>=1;i_1--)
    {
    res_1=res_1*x_1+i_1*系数[i_1];
    }
    返回resu1;
    }
    /*此处的新参数:cnt系数中的元素数*/
    双牛顿(双*系数、尺寸、双a、双b、双eps)
    {
    int N=0;
    双c=((a+b)/2);
    而(N<10)
    {
    /*用cnt替换…的大小*/
    双y=霍纳(系数,cnt,c);
    /*用cnt替换…的大小*/
    双y_导数=霍纳导数(系数,cnt,c);
    双c_1=c-(y/y_导数);
    printf(“c:%f\t”,c);
    printf(“y:%f\t”,y);
    printf(“y\u顺序:%f\t”,y\u导数);
    printf(“c_1:%f\n”,c_1);
    
    if(晶圆厂(c_1-c)您不应将图像用作代码。请使用文本。欢迎使用堆栈溢出。请直接将您的代码包含在问题中-不要使用代码的图像,因为它不可读、不可复制、不可修改,因此不可修改。正如@rootTraveler所建议的,您不应发布代码的图像。除此之外,不要只是复制粘贴您试图运行的整个代码。创建一个适当的示例来演示问题。您应该在阅读后尝试打印
    coeff
    s以确定确切的问题所在。
    horner(coefs,sizeof(coefs),c);
    不会做您认为它会做的事。
    coefs
    是一个指针,所以
    sizeof(coefs)
    是指针的大小,而不是数组中的元素数。
    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    
    double horner(double *coeffs, int s, double x)
    {
        int i;
        double res = 0.0;
    
        for(i=s-1; i >= 0; i--)
        {
            res = res * x + coeffs[i];
        }
        return res;
    }
    
    double hornerDerivative(double *coeffs, int s_1, double x_1)
    {
        int i_1;
        double res_1 = 0.0;
    
        for(i_1 = s_1; i_1 >= 1; i_1--)
        {
            res_1 = res_1 * x_1 + i_1*coeffs[i_1];
        }
        return res_1;
    }
    
    /* New parameter here: cnt the number of elements in coeffs */
    double newton(double *coeffs, size_t cnt, double a, double b, double eps)
    {
       int N = 0;
       double c = ((a+b)/2);
    
       while(N < 10)
       {
            /* replacing sizeof... by cnt */
            double y = horner(coeffs, cnt, c);
            /* replacing sizeof... by cnt */
            double y_derivative = hornerDerivative(coeffs, cnt, c);
            double c_1 = c - (y/y_derivative);
            printf("c: %f\t",c);
            printf("y: %f\t",y);
            printf("y_der: %f\t",y_derivative);
            printf("c_1: %f\n",c_1);
            if(fabs(c_1-c)<eps)
            {
                return c_1;                   
            }
            c = c_1;
            N = N + 1;
       }
    
       /* always return some value */
       fprintf(stderr, "Warning newton do not converge in 10 steps...\n")
       return c;
    }
    
    int main(int argc, char **argv)
    {
        printf("# of arguments%d\n\n\n\n", argc);
        double coeffs[argc-3];
    
        double a = atof(argv[argc-2]);
        double b = atof(argv[argc-1]);
        double eps = .001;
    
        int i;
        for(i=1; i < argc-2; i++)
        {
            coeffs[i-1] = atof(argv[argc-2-i]);
        }
    
        /* pass the number of elements in coeffs array */
        printf("The root of the equation is: %f\n", newton(coeffs, argc-3, a, b, eps));    
        return 0;
    }