首先,我是物理系的学生,不是程序员,所以请原谅这个琐碎的问题。我正在尝试使用 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' 允许用户输入这些值,这将是三次方程的系数。接下来我调用该函数并将变量名称初始化在上面,我相信这意味着用户输入的值将被传递到函数中以在函数中使用。在此下面,我对其进行编程以打印函数的输出。
正如我所说,这段代码可能非常丑陋且效率低下,但它在某种程度上确实有效,我只是无法弄清楚为什么它不能以这种形式工作。
我希望你能帮助,
如果您还有其他问题,我会尽力澄清。