-1

首先,我是物理系的学生,不是程序员,所以请原谅这个琐碎的问题。我正在尝试使用 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' 允许用户输入这些值,这将是三次方程的系数。接下来我调用该函数并将变量名称初始化在上面,我相信这意味着用户输入的值将被传递到函数中以在函数中使用。在此下面,我对其进行编程以打印函数的输出。

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

我希望你能帮助,

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

4

3 回答 3

3

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

return value; // instead of "value", pick the variable you want to return
于 2013-10-19T22:18:23.673 回答
2

一个问题是这条线

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

这不是你想的那样!这里的逗号实际上是“逗号运算符”,它计算它的两个操作数并取最右边的值。它的优先级低于>>所以你的行意味着相同

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

它从cinintox3然后继续评估 variables x2x并且con- 这没有做任何事情,因为评估变量没有副作用。要读取所有 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]
于 2013-10-19T22:48:41.387 回答
2

以下是错误的:

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

这会调用逗号运算符,它不会执行您认为的操作。

你应该做这个:

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;
于 2013-10-19T22:46:47.703 回答