#include<iostream> #include<cmath> using namespace std; double psi(double x) { return (2.7 + x) / (5.7 - 0.9 * x + x * x); } double fi(double x) { return cos(x) * cos(x); } double func(double x, double t) { return psi(x) * fi(x + t / (1 + x * x)); } double funcGa(double t) { double a; double x = a; return psi(x) * fi(x + t / (1 + x * x)); } double funcGb(double t) { double b; double x = b; return psi(x) * fi(x + t / (1 + x * x)); } double integral(double a, double b, double e, double t) { double h, J1, J2; int i, n; n = 1; h = (b - a) / n; J2 = h / 3 * (2 * func(a + 2 * h, t) + 4 * func(a + h) + (funcGa(a) + funcGb(b)) / 2); do { J1 = J2; n = n * 2; h = (b - a) / n; J2 = 0; for (i = 1; i <= n / 2 - 1; i++) for (j = 1; j <= n / 2; j++) J2 = 2 * func(a + 2 * i * h, t) + 4 * func(a + (2 * j - 1) * h, t) + (funcGa(a) + funcGb(b)) / 2; J2 = J2 * h / 3; } while (fabs(J2 - J1) > e); return J2; } int main() { double a, b, e; int m, i; double* t; t = new double[m]; cin >> a >> b >> e >> m; for (i = 0; i < m; i++) { cin >> t[i]; cout << integral(a, b, e, t[i]) << endl; } } |
3 answers
You have one argument missing, variable j not declared. But this is nonsense, because -
double funcGa(double t) { double a; double x = a; return psi(x) * fi(x + t / (1 + x * x)); } Well, what does such a function count? There is some a , with an undefined value - there can be anything in it. And you start counting ...
Most of all it reminds Peretz's work on a broken Mercedes at the Strugatsky ...
- It turns out I needed to do something like that? double funcGa (double x, double t) {return psi (x) * fi (x + t / (1 + x * x)); } and when calling, write funcGa (a, t) - Dmitry Polyakov
- If you clearly state your task - then it will be possible to talk about something. You have no comment to the code and that is not ... - Harry
- I made a change in the question - Dmitry Polyakov
|
error at least here
J2=h/3*(2*func(a+2*h,t)+4*func(a+h)+(funcGa(a)+funcGb(b))/2); The second call to func contains one argument, and the signature of the function should be two. Either forgot one argument, or call the wrong function.
- I think I understood you and there you were, j forgot to announce - Dmitry Polyakov
|
Here is your decision. Frankly, the integrals did not consider separately, but it seems to work ...
double F(double x, double t) { double z = cos(x+t/(1+x*x)); return ((2.7*x)/((x-0.9)*x+5.7))*z*z; } double Simpson(double a, double b, double t, double eps, double (*f)(double,double)) { double N = 4.; double sum1 = 0., sum2 = 0.; double x, h; double index1, index2; unsigned int i; double func; for (;;) { h = (ba)/N; sum1 = sum2 = -(f(a,t)+f(b,t)); index1 = index2 = 4.; i = 0; for(x = a; x <= b+h/2.; x+=h) { sum1 += (index1 = 6.-index1)*(func=f(x,t)); if (i++%2==0) { sum2 += (index2 = 6.-index2)*func; }; }; if (fabs((sum1-2.*sum2)/sum1)<=eps) return (h*sum1)/3.+h*(2.*sum2-sum1)/45.; else N*=2.; }; }; int main(int argc, const char * argv[]) { double a, b, e; cout << "a = "; cin >> a; cout << "b = "; cin >> b; cout << "eps = "; cin >> e; int m; cout << "m = "; cin >> m; double* t = new double[m]; double* y = new double[m]; for (int i = 0; i < m; i++) { cout << "t["<<(i+1)<<"] = "; cin >> t[i]; y[i] = Simpson(a,b,t[i],e,F); cout << "y[" << (i+1) << "] = " << y[i] << endl; } } - double (* f) (double, double) what is the parameter? - Dmitry Polyakov
- A pointer to your function of two variables
F(x,t). So that it was possible, if necessary, to consider integrals for any other :) - Harry - What are the numbers with a dot for? - Dmitry Polyakov
- So that it was immediately obvious that this is a
double, of course ... - Harry - for (;;) for the first time I see such a cycle, can you please explain - Dmitry Polyakov
|

new double[m]must be called aftermbeen assigned a value. - αλεχολυτ