C ++,代码工作正常,但在提取函数时却没有

时间:2013-10-19 22:15:09

标签: c++ function newtons-method

首先,我是一名物理系学生,而不是程序员,所以请原谅这个琐碎的问题。我正在尝试使用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初始化为双精度,并使用导入'cin'允许用户输入这些值,这将是三次方程的系数。接下来我调用函数并将变量名称初始化在上面,我相信这意味着用户输入的值将被传递到函数中以便在函数中使用。 下面我编程它来打印函数的输出。在那下面是函数的定义,除了函数名和参数之外,我是如何在不同的cpp中编写的,它工作得很好,只有当我编写这段代码时,试图将原始代码放入函数形式,即发生了问题。

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

我希望你能提供帮助,

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

3 个答案:

答案 0 :(得分:3)

你的函数需要返回一个值,你最后错过了一个return语句:

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

答案 1 :(得分: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;

答案 2 :(得分:2)

这一行有一个问题

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

这不符合你的想法!这里的逗号实际上是“逗号运算符”,它评估它的操作数并获取最右边的值。它的优先级低于>>,因此您的行与

相同
((((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]