numerical integration lower limit,upper limit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#include <fstream.h>
#include <iomanip.h>
#include <math.h>
#include <iostream.h>
#include <stdlib.h>
using namespace std;
double f(double);

int main()
{double a,b,delx,n,area,i,x;
ofstream output ("e:result17.dat'");
cout<<"..."<<endl;
output<<"..."<<endl;
a=2.0;
b=5.0;
cout<<" Enter a value for n : "<<endl;
output<<" Enter a value for n : "<<endl;
cin>>n;
cout<<" n= "<<n<<endl;
output<<" n= "<<n<<endl;
area=0;
delx=(b-a)/n;
for(i=0;i<n;++i)
{
area=area+f(a+i*delx)*delx;
}
cout<<" The area of lower limit is = "<<area<<endl;
output<<" The area of lower limit is = "<<area<<endl;
for(i=1;i<=n;++i)
area=area+f(a+i*delx)*delx;
cout<<" The area of upper limit is = "<<area<<endl;
  system("PAUSE");
      return 0.6*pow(x,2)+2;
}

[/cpp]
i am getting a warning return to`int' from `double'
any help??
Last edited on
This is because you are returning a non integer number in a function that is defined to return an integer. The return statement at the end:

return 0.6*pow(x,2)+2;

will not return an integer. However you have defined main() as returning an integer. Usually the return value of main should indicate whether the program ran successfully or not. So your return statement should be return 0 if everything worked correctly.
the first thing is that your code is very unreadable....
you should stick with some formatting... (i think its called Best Practices...) just look at the codes here in this site and try to figure out a formatting style for you, so you will be able to read your code more easily...

i cannot see the function "f" defined in here...
what you have done is just write the codes of your function in the "main" function, which always returns type "int"... normally we set the return value to "0"(zero) so that the system knows that the program worked successfully.. ( see EXIT_SUCCESS in the library stdlib.h , just search for it in google)...

i guess,
what your were thinking is that this is your function "f", and of course the function "f" returns "double".... but you have written it not inside funcion "f" , but inside function "main"...

this must be what you were trying to do,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <fstream.h>
#include <iomanip.h>
#include <math.h>
#include <iostream.h>
#include <stdlib.h>
using namespace std;
double f(double);

int main()
{
  double returnValueOfF;
  returnValueOfF = f();

  return 0;
}

double f(double parameter)
{
  double a,b,delx,n,area,i,x;
  ofstream output ("e:result17.dat'");

  cout<<"..."<<endl;
  output<<"..."<<endl;

  a=2.0;
  b=5.0;

  cout<<" Enter a value for n : "<<endl;
  output<<" Enter a value for n : "<<endl;
  cin>>n;
  cout<<" n= "<<n<<endl;
  output<<" n= "<<n<<endl;

  area=0;
  delx=(b-a)/n;

  for(i=0;i<n;++i)
  {
      area=area+f(a+i*delx)*delx;
  }

  cout<<" The area of lower limit is = "<<area<<endl;
  output<<" The area of lower limit is = "<<area<<endl;

  for(i=1;i<=n;++i)
      area=area+f(a+i*delx)*delx;

  cout<<" The area of upper limit is = "<<area<<endl;

  system("PAUSE");

  return 0.6*pow(x,2)+2;
}


ps. see how nice the code looks with some formatting... see how nicely it is readable... it only lacks commenting....
please, never forget to add comments and white-spaces (newlines, tabs, spaces ) to your code.... it will make your life easier...
Last edited on
so where would i put that equation 0.6*pow(x,2)+2; ????
I presume that the function f should be a representation of the function that you are trying to integrate. I'm going to go out on a limb and assume it's 0.6x^2 + 2.

In this case you need to write a function that takes a number (x) as a parameter and returns that number squared and multiplied by 0.6, and then add 2 to it.

So you need a function like this:

1
2
3
4
double f(double x)     //this functions returns a double and takes one double (which we will call x) as a parameter
{
    return 0.6*pow(x,2) + 2; 
}


So now when we add a function call like f(x) it will give us back the function applied to that number.



well when i run the program when n=5,n=50,n=100 the exact answer should be 29.4
for the lower limit we get a close value ... not really sure about the other 3....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

#include <fstream.h>
#include <iomanip.h>
#include <math.h>
#include <iostream.h>
#include <stdlib.h>
using namespace std;
double f(double x)
{
return 0.6*pow(x,2)+2;
}
int main()


{double a,b,delx,n,area,i;
ofstream output ("e:result17.dat'");
cout<<"...."<<endl;
output<<"......"<<endl;
a=2.0;
b=5.0;
cout<<" Enter a value for n : "<<endl;
output<<" Enter a value for n : "<<endl;
cin>>n;
cout<<" n= "<<n<<endl;
output<<" n= "<<n<<endl;
area=0;
delx=(b-a)/n;
for(i=0;i<n;++i)
{
area=area+f(a+i*delx)*delx;
}
cout<<" The area using lower limit is ="<<area<<endl;
output<<" The area using lower limit is ="<<area<<endl;
for(i=1;i<=n;++i)
{
area=area+f(a+i*delx)*delx;
}
cout<<" The area using upper limit is = "<<area<<endl;
output<<" The area using upper limit is = "<<area<<endl;
for (i=0;i<n;++i)
{
area=area+f(a+0.5*delx+i*delx)*delx;
}
cout<<" The area using mid point is = "<<area<<endl;
output<<" The area using mid point is = "<<area<<endl;
for (i=0;i<n;++i)
{
area=area+f(a+i*delx)+f(a+(i+1)*delx)*delx/2;
}
cout<<" The area using trapezoid is = "<<area<<endl;
output<<" The area using trapezoid is = "<<area<<endl;

  system("PAUSE");

      return 0;
}
You have to reset your variables. For the upper limit the area is starting at what you got from the lower limit.
how do i do that ??
Just set area back to zero before line 34.

Edit: Make sure you set the area to zero before the other integration methods as well.
Last edited on
for the upper limit area should be set area = 1??
line 34 my i=1 it starts at 1? maybe i am wrong ... what do u say?
if you set area = 0 before each new calculation your code is correct except for the trapezoid part. At the moment your code is calculating f(x) + f(x + dx) * dx/2 when it should be calculating
(f(x) + f(x + dx)) * dx/2. The brackets are important, your code is missing a pair.
Topic archived. No new replies allowed.