C++ 问题:";无法将参数1从';双倍';至';字符(*)(双)和#x27&引用;
我对以下作业有问题。问题是求函数的积分。它给我的错误是“无法将参数1从'double'转换为'char(*)(double)'”。我认为问题出在底部,在那里我定义了函数。我甚至不确定我是否应该用char来表示p 有人知道问题出在哪里吗C++ 问题:";无法将参数1从';双倍';至';字符(*)(双)和#x27&引用;,c++,arguments,integral,C++,Arguments,Integral,我对以下作业有问题。问题是求函数的积分。它给我的错误是“无法将参数1从'double'转换为'char(*)(double)'”。我认为问题出在底部,在那里我定义了函数。我甚至不确定我是否应该用char来表示p 有人知道问题出在哪里吗 /*43. Modify program chapter6_11 to estimate the integral of the function f (x) = 3x − 2x^2.*/ #include <iostream> //Required
/*43. Modify program chapter6_11 to estimate the integral of the function
f (x) = 3x − 2x^2.*/
#include <iostream> //Required for cin, cout
#include <fstream>
#include <cstdlib> //Required for srand(), rand().
#include <cmath> //Required for pow().
using namespace std;
/*-----------------------------------------------------------------*/
/* Program chapter6_11 */
/* */
/* This program finds the real roots of a cubic polynomial */
/* using the Newton-Raphson method. */
double integral(char(p)(double x), double a, double b, double n);
int main(){
// Declare objects.
int iterations(0);
double a1, a2, a3, x, p, dp, tol;
cout << "Enter coefficients a1, a2, a3 (here -2, 3 and 0)\n";
cin >> a1 >> a2 >> a3;
cout << "Enter initial guess for root\n";
cin >> x;
// Evaluate p at initial guess.
p = -2* x * x + 3 * x + 0;
// Determine tolerance.
tol = fabs(p);
while (tol > 0.001 && iterations < 100)
{
// Calculate the derivative.
dp = 2 * -2 * x + 3;
// Calculate next estimated root.
x = x - p / dp;
// Evaluate p at estimated root.
p = -2 * x * x + 3 * x + 0;
tol = fabs(p);
iterations++;
}
if (tol < 0.001)
{
cout << "Root is " << x << endl;
cout << iterations << " iterations\n";
cout << "Integral is" << integral(p, -100000, 100000, 1000);
}
else
cout << "Did not converge after 100 iterations\n";
return 0;
}
double integral(char(p)(double x), double a, double b, double n) {
double step = (b - a) / n; // width of each small rectangle
double area = 0.0; // signed area
for (int i = 0; i < n; i++) {
area += p(a + (i + 0.5) * step) * step; // sum up each small rectangle
}
return area;
}
/*43。修改程序第6章11以估计函数的积分
f(x)=3x− 2x^2*/
#包括//cin所需,cout
#包括
#包括//srand()所需,rand()。
#包括//pow()所需。
使用名称空间std;
/*-----------------------------------------------------------------*/
/*计划第6章11*/
/* */
/*这个程序找到一个三次多项式的实根*/
/*使用牛顿-拉斐逊方法*/
二重积分(char(p)(double x),double a,double b,double n);
int main(){
//声明对象。
整数次迭代(0);
双a1,a2,a3,x,p,dp,tol;
cout>a1>>a2>>a3;
cout>x;
//在初始猜测时计算p。
p=-2*x*x+3*x+0;
//确定公差。
tol=fabs(p);
而(tol>0.001&&iterations<100)
{
//计算导数。
dp=2*-2*x+3;
//计算下一个估计根。
x=x-p/dp;
//在估计根处计算p。
p=-2*x*x+3*x+0;
tol=fabs(p);
迭代++;
}
如果(tol<0.001)
{
你可以称之为积分
cout << "Integral is" << integral(p, -100000, 100000, 1000);
也许你想在函数中定义这样的函数?我想你应该问问自己我想集成什么函数?你遇到问题的原因是你没有编写或选择一个函数来集成。显然你不能集成一个数字,这就是错误告诉你的。顺便提一下,我想你想要做什么uble(p)(double x)
。对区域使用char
没有多大意义。第一个参数应该是函数指针,但您正在传递一个double
p
是否有某个值正确?您正在尝试将该值乘以(a+(i+0.5)*步)
,对吗?如果是这样,你必须使用*
操作符,就像这样:p*(a+(i+0.5)*步)
。现在在main
中,您将p
声明为double
。因此声明不能更改,应该是integral
函数中的double p
。我看到您试图在声明中混合p
和x
。然后执行som用它们进行运算(我不知道p(x)
意味着什么,再次相乘?)并将它们存储在p
中,就像这样:p=p*x
。我想知道用牛顿-拉斐逊法计算多项式的根与积分有什么关系。
char func(double x);