如何在c++;? 我使用C++中的辛普森1/3法则来求K*(x*x)的积分,其中k=2×m,m’从1到10,因此,我有10个积分。当我写下下面的代码时,我得到了答案,但它添加了前面的积分值!e、 g.对于m=1=>k=2,积分为0.6666
如何在c++;?如何在c++;? 我使用C++中的辛普森1/3法则来求K*(x*x)的积分,其中k=2×m,m’从1到10,因此,我有10个积分。当我写下下面的代码时,我得到了答案,但它添加了前面的积分值!e、 g.对于m=1=>k=2,积分为0.6666,c++,for-loop,iteration,integration,C++,For Loop,Iteration,Integration,如何在c++;? 我使用C++中的辛普森1/3法则来求K*(x*x)的积分,其中k=2×m,m’从1到10,因此,我有10个积分。当我写下下面的代码时,我得到了答案,但它添加了前面的积分值!e、 g.对于m=1=>k=2,积分为0.66667,对于m=2=>k=4,积分为2.00(0.66667+0.33333),而不是0.33333。如何防止它这样做 #include<iostream> #include<cmath> #include<ioman
我使用C++中的辛普森1/3法则来求K*(x*x)的积分,其中k=2×m,m’从1到10,因此,我有10个积分。当我写下下面的代码时,我得到了答案,但它添加了前面的积分值!e、 g.对于m=1=>k=2,积分为0.66667,对于m=2=>k=4,积分为2.00(0.66667+0.33333),而不是0.33333。如何防止它这样做
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int k;
double f(double x) {
return k * pow(x, 2);
}
int main() {
int n = 100000, m;
double h, a = 0, b = 1, sum = 0, x, y;
cout << "The number of sub-intervals is n = " << n << endl;
cout << fixed << showpoint << setprecision(5);
for (m = 1; m <= 10; m++) {
cout << "m = " << m << endl;
k = 2 * m;
cout << "k = " << k << endl;
h = (b - a) / n;
for (int i = 1; i <= n; i++) {
x = a + i * h;
if (i % 2 == 0) {
sum = sum + 2 * f(x);
} else {
sum = sum + 4 * f(x);
}
}
y = h / 3.0 * (f(a) + sum + f(b));
cout << "The integration is: " << y << endl;
}
}
#包括
#包括
#包括
使用名称空间std;
int k;
双f(双x){
返回k*pow(x,2);
}
int main(){
int n=100000,m;
双h,a=0,b=1,和=0,x,y;
难道因为你一直在加和而从不把它重置为零?