【问题标题】:C++, code works fine but not when extracting a functionC ++,代码工作正常,但在提取函数时不能
【发布时间】:2013-10-28 14:26:24
【问题描述】:

首先,我是物理系的学生,不是程序员,所以请原谅这个小问题。我正在尝试使用 Newton Raphson 方法创建一个函数来查找三次方程的根。我已经创建了几乎可以正常工作的代码,但练习的重点是让这些代码采用“函数”形式,即返回类型、参数然后是代码块。当我尝试将它放入这种形式时,我的代码将编译,但是当我输入它时,结果(它返回的根)是乱码。这是我的代码。

#include<iostream>
#include<cmath>

double roots(double,double,double,double);

int main()
{
    double x3,x2,x,con;
    std::cin>>x3,x2,x,con;

    double result=roots(x3,x2,x,con);
    std::cout<<result<<std::endl;
    system("pause");
}

double roots(double xcubecoeff, double xsquarecoeff, double xcoeff, double constant)
{
    int j=0;
    int k=0;
    int l=0;
    int m=0;
    double seedvalue1=-10;
    double seedvalue2=10;
    double seedvalue3=0.3;
    double seedvalue4=-0.3;
    double xnplus1;
    double xnplus2;
    double xnplus3;

    while(j <= 100)
    {
        //func is just the structure of the cubic equation in terms of the 
        // parameters of the function
        //derfunc is just the structure of the gneral derivitive of a cuic
        // function, again in terms of the parameters
        //seedvalues are just the initial values for x in the general cubic
        // equation. 
        //Seedvalue one goes into the loop to find the negative x root, 
        // seedvalue 2 finds the positive one, seedvalue three attempts to
        // find the one in the middle of those, however if it just finds
        // either of those again then an IF statement and seedvalue 4 are
        //used to find it.

        double func = xcubecoeff * (seedvalue1 * seedvalue1 * seedvalue1) +
                      xsquarecoeff * (seedvalue1 * seedvalue1) + 
                      xcoeff * seedvalue1 + 
                      constant;

        double derfunc = 3 * xcubecoeff * (seedvalue1 * seedvalue1) +
                         2 * xsquarecoeff * seedvalue1 + 
                         xcoeff;


        double xnplus1 = seedvalue1 - (func / derfunc);

        seedvalue1=xnplus1;
        j++;
    }


    while(k <= 100)
    {
        double func = xcubecoeff * (seedvalue2 * seedvalue2 * seedvalue2) + 
                      xsquarecoeff * (seedvalue2 * seedvalue2) + 
                      xcoeff * seedvalue2 +
                      constant;

        double derfunc = 3 * xcubecoeff * (seedvalue2 * seedvalue2) + 
                         2 * xsquarecoeff * seedvalue2 +
                         xcoeff;

        double xnplus2 = seedvalue2 - (func / derfunc);

        seedvalue2 = xnplus2;
        k++;
    }

    while(l<=100)
    {
        double func = xcubecoeff * (seedvalue3 * seedvalue3 * seedvalue3) + 
                      xsquarecoeff * (seedvalue3 * seedvalue3) +
                      xcoeff * seedvalue3 +
                      constant;

        double derfunc = 3 * xcubecoeff * (seedvalue3 * seedvalue3) + 
                         2 * xsquarecoeff * seedvalue3 + 
                         xcoeff;

        double xnplus3 = seedvalue3 - (func / derfunc);

        seedvalue3=xnplus3;
        l++;
    }

    if(seedvalue3 == seedvalue1 || seedvalue3 == seedvalue2)
    {
        while(m<=100)
        {
            double func = xcubecoeff * (seedvalue4 * seedvalue4 * seedvalue4) +
                          xsquarecoeff * (seedvalue4 * seedvalue4) + 
                          xcoeff * seedvalue4 +
                          constant;

            double derfunc = 3 * xcubecoeff * (seedvalue4 * seedvalue4) +
                             2 * xsquarecoeff * seedvalue4 + xcoeff;

            double xnplus4 = seedvalue4 - (func / derfunc);

            seedvalue4=xnplus4;
            m++;
        }

        std::cout<<seedvalue1<<std::endl;
        std::cout<<seedvalue2<<std::endl;
        std::cout<<seedvalue4<<std::endl;
    }
    else
    {

        std::cout<<seedvalue1<<std::endl;
        std::cout<<seedvalue2<<std::endl;
        std::cout<<seedvalue3<<std::endl;
    }
}

这可能是一个非常笨重和繁琐的代码,我确信有更好的方法来执行 Newton Raphson 方法。 为了清楚起见,我首先包括标准 iostream 和数学。然后我声明我的函数,名称后跟可以传递给它的参数类型。接下来我开始我的代码。我将变量 x3、x2、x 和 con 初始化为双精度值,并使用 import 'cin' 允许用户输入这些值,这将是三次方程的系数。接下来我调用该函数并将变量名称初始化在上面,我相信这意味着用户输入的值将被传递到函数中以在函数中使用。 下面我对其进行编程以打印函数的输出。下面是函数的定义,除了函数名和参数之外,我是如何在不同的 cpp 中编写它的,它工作得很好,只有当我编写这段代码时,试图将原始代码转换为函数形式出现问题。

正如我所说,这段代码可能非常丑陋且效率低下,但它在某种程度上确实有效,我只是想不通为什么它不能以这种形式工作。

希望能帮到你

如果您还有其他问题,我会尽力澄清。

【问题讨论】:

  • 牛顿也讨厌这种格式和缩进:P
  • 如果您向我们展示有效的代码、正确的结果以及错误代码给出的结果,这可能会有所帮助。
  • 代码块是什么意思 - 我会让函数相互调用,但删除所有 NR 代码,以便我们可以看到您认为调用应该是什么以及它们如何协同工作跨度>
  • TL;DR;归结为要领
  • 立方有直接解析解。

标签: c++ function newtons-method


【解决方案1】:

你的函数需要返回一个值,你在最后缺少一个 return 语句:

return value; // instead of "value", pick the variable you want to return

【讨论】:

    【解决方案2】:

    以下是错误的:

    double x3,x2,x,con;
    std::cin>>x3,x2,x,con;
    

    这会调用comma operator,它不会做你认为它做的事情。

    你应该这样做:

    std::cout << "Enter x3: ";
    std::cin >> x3;
    std::cout << "Enter x2: ";
    std::cin >> x2;
    std::cout << "Enter x: ";
    std::cin >> x;
    std::cout << "Enter c: ";
    std::cin >> con;
    

    如果你想将它们组合成一行:

    std::cin >> x3 >> x2 >> x >> con;
    

    【讨论】:

      【解决方案3】:

      这一行有一个问题

      std::cin>>x3,x2,x,con;
      

      这不是你想的那样!这里的逗号实际上是“逗号运算符”,它计算它的两个操作数并取最右边的值。它的优先级低于&gt;&gt;,因此您的行的含义与

      ((((std::cin>>x3), x2), x), con);
      

      cin 读取到x3,然后继续评估变量x2xcon - 这没有任何作用,因为评估变量没有副作用。要读取所有 4 个变量,您可以使用:

      std::cin >> x3 >> x2 >> x >> con;
      

      打开尽可能多的编译器警告是个好主意,因为它经常会遇到这样的事情。例如,如果我使用 gcc -Wall -Wextra 编译您的代码行,它会给出以下警告:

      test.cpp: In function 'int main()':
      test.cpp:5:17: warning: right operand of comma operator has no effect [-Wunused-value]
      test.cpp:5:19: warning: right operand of comma operator has no effect [-Wunused-value]
      test.cpp:5:22: warning: right operand of comma operator has no effect [-Wunused-value]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多