我对c ++编程比较陌生,我有一个代码来编写Newton Raphson方法,但是我有错误错误:
called object type 'double' is not a function or function pointer
当我尝试编译代码时出现此错误。我尝试了一些基本的更改来分配指针,但我可能以错误的方式做了,我的代码打印在下面,有人可以解释我怎样才能克服这个?
#include <iostream>
#include <math.h>
using namespace std;
double f(double x); //this is f(x)
double f(double x) {
double eq1 = exp(x) + pow(x,3) + 5;
return eq1;
}
double f1(double x); //this is the first derivative f'(x)
double f1(double x) {
double eq2 = exp(x) + 3*pow(x,2);
return eq2;
}
int main() {
double x, xn, f, f1, eps;
cout << "Select first root :" << '\n'; //Here we select our first guess
cin >> xn;
cout << "Select Epsilon accuracy :" << '\n';
cin >> epsi;
f = f(x);
f1 = f1(x);
cout << "x_n" << " " << "x_(n+1)" << " " << "|x_(n+1) - x_1|" << '\n';
do {
x = xn; //This is the first iteneration step where x takes the value of the last itenarated (known) root xn
f = f(x);
f1 = f1(x);
xn = x - (f/f1); //this the formula that sets the itenaration going
cout << x << " " << xn << " " << fabs(xn - x) << '\n';
}
while( fabs(xn - x) < epsi ); //If |x_(n+1) - x_n| is smaller than the desired accurcay than the itenaration continues
cout << "The root of the equation is " << xn << '\n';
return 0;
}
谢谢
答案 0 :(得分:2)
您有与函数同名的局部变量,因此
f = f(x);
f1 = f1(x);
无法正常工作。
重命名函数或变量。无论如何单字母变量/函数名称都不好。使用描述性名称。几个星期后你(或其他任何人)看一下代码将会感激不尽。
PS:你也不需要前瞻声明。而且函数可以写得更短:
//double f(double x); // this you dont need
double f(double x) {
return exp(x) + pow(x,3) + 5;
}
另外using namespace std;
is considered bad practice。在这种情况下,它几乎没有任何伤害,但你最好在它重要之前摆脱这种坏习惯。
最后但并非最不重要的是,您应该正确格式化代码。此
while( fabs(xn - x) < epsi );
看起来非常讨厌,因为它似乎是一个无限循环。我几乎从不使用do-while循环,但是,我建议你这样写:
do {
// ...
} while ();
因为通常只要你在同一行看到一段;
,就应该开始恐慌;)(虽然循环比do-while和;
引起的错误更常见while循环中的条件可能是a * to a debug中的痛苦
答案 1 :(得分:0)
您正在尝试使用名为f和f1的函数以及名为f和f1的双精度函数。如果您调用变量或函数,则可以解决错误。为这些变量提供更好的名称,告诉读者他们做了什么,并避免像这样的错误,这将是一个很好的编码实践。
答案 2 :(得分:0)
您的代码中存在多个错误。我把它编成可编辑:
#include <iostream>
#include <math.h>
using namespace std;
double func(double x); //this is f(x)
double func(double x) {
double eq1 = exp(x) + pow(x,3) + 5;
return eq1;
}
double func1(double x); //this is the first derivative f'(x)
double func1(double x) {
double eq2 = exp(x) + 3*pow(x,2);
return eq2;
}
int main() {
double x, xn, f, f1, eps;
cout << "Select first root :" << '\n'; //Here we select our first guess
cin >> xn;
cout << "Select Epsilon accuracy :" << '\n';
cin >> eps;
f = func(x);
f1 = func1(x);
cout << "x_n" << " " << "x_(n+1)" << " " << "|x_(n+1) - x_1|" << '\n';
do {
x = xn; //This is the first iteneration step where x takes the value of the last itenarated (known) root xn
f = func(x);
f1 = func1(x);
xn = x - (f/f1); //this the formula that sets the itenaration going
cout << x << " " << xn << " " << fabs(xn - x) << '\n';
}
while( fabs(xn - x) < eps ); //If |x_(n+1) - x_n| is smaller than the desired accurcay than the itenaration continues
cout << "The root of the equation is " << xn << '\n';
return 0;
}
主要问题是:
f
函数的相同名称定义了变量f(x)
(f'(x)
函数重复了相同的错误)和eps
变量代表程序中的 epsilon ,但您尝试通过调用epsi
多次访问它。