I have the function x^2 and want to find the area under the curve. I want to make it so that if I type in fx n=4 intervals I get the area for n=1 and then n=2 and then n=3... etc. I believe I am supposed to make a nested loop but I cant make it work. this is what I have
#include <iostream>
double f( double );
using std::cin;
using std::cout;
int main(){
double sum, x, dx, a, b;
int n,j,k;
cout<<"Input x1:";
cin >> a;
cout<<"Input x2:";
cin >> b;
cout<<"Input no. of divisions:";
cin>>n;
dx = ( b - a ) / n;
x = a + dx/2;
sum = 0;
do
{
sum += dx * f( x);
x += dx;
} while( x <= b );
std::cout << "Area is: " << sum;
return 0;
}
double f(double x)
{
double result;
result = x * x;
return result;
}
But I still get it only for the number of intervals I type in. How do I get it so that it shows fx if i type n=4 I get in one output the area for n=1, the area for n=2 and n=3. This is what I can't figure out.
Without sounding like a freeloader I really need to get this point. Does that mean that the do while loop needs to be inside the brackets of the for loop. I did the following and get
#include <iostream>
double f( double );
using std::cin;
using std::cout;
int main(){
double sum, x, dx, a, b;
int n,j,k;
cout<<"Input x1:";
cin >> a;
cout<<"Input x2:";
cin >> b;
cout<<"Input no. of divisions:";
cin>>n;
dx = ( b - a ) / n;
x = a + dx/2;
sum = 0;
for( k = 0; k < n; k++){
do
{
sum += dx * f( x);
x += dx;
} while( x <= b );
std::cout << "\n Area is: " << sum;
}
return 0;
}
double f(double x)
{
double result;
result = x * x;
return result;
}
I cant see why I get 21 and 41.25. By the way this is not my actual assignment but hopefully I can get this method straight I can build on it and complete my assignment
#include <iostream>
double f( double );
using std::cin;
using std::cout;
int main(){
double sum, x, dx, a, b;
int n,j,k;
cout<<"Input x1:";
cin >> a;
cout<<"Input x2:";
cin >> b;
cout<<"Input no. of divisions:";
cin>>n;
dx = ( b - a ) / n;
x = a + dx/2;
sum = 0;
for (k = 0; k < n; k++){
do
{
sum += dx * f( x);
x += dx;
} while( x <= b );
std::cout << "\n Area is: " << sum;
}
return 0;
}
double f(double x)
{
double result;
result = x * x;
return result;
}
Area is: 5205.62
Area is: 5302.04
Area is: 5399.65
Area is: 5498.44
Area is: 5598.44
Area is: 5699.63
Area is: 5802.04
Area is: 5905.67
Area is: 6010.52
Area is: 6116.6
Area is: 6223.93
Area is: 6332.51
Area is: 6442.33
Area is: 6553.43
Area is: 6665.79
Area is: 6779.42
Area is: 6894.34
Area is: 7010.55
Area is: 7128.06
Area is: 7246.88
Area is: 7367.01
Area is: 7488.45
Area is: 7611.23
Area is: 7735.34
Area is: 7860.79
Area is: 7987.58
Area is: 8115.74
Area is: 8245.26
Area is: 8376.14
Area is: 8508.41
Area is: 8642.06
Area is: 8777.1
Area is: 8913.54
Area is: 9051.39
Area is: 9190.65
Area is: 9331.33
Area is: 9473.44
Area is: 9616.98
Area is: 9761.97
Area is: 9908.4
Area is: 10056.3
Area is: 10205.7
Area is: 10356.5
Area is: 10508.8
Area is: 10662.6
Area is: 10817.9
Area is: 10974.6
Area is: 11132.9
Area is: 11292.7
Area is: 11454.1
Area is: 11616.9
Area is: 11781.3
Area is: 11947.3
Area is: 12114.7
Area is: 12283.8
Area is: 12454.4
Area is: 12626.6
Area is: 12800.3
Area is: 12975.7
Area is: 13152.6
Area is: 13331.2
Area is: 13511.3
Area is: 13693.1
Area is: 13876.5
Area is: 14061.5
Area is: 14248.1
Area is: 14436.4
Area is: 14626.4
Area is: 14818
Area is: 15011.3
Area is: 15206.2
Area is: 15402.8
Area is: 15601.2
Area is: 15801.2
Area is: 16002.9
Area is: 16206.3
Area is: 16411.4
Area is: 16618.3
Area is: 16826.9
Area is: 17037.2
Area is: 17249.3
Area is: 17463.1
Area is: 17678.7
Area is: 17896.1
Area is: 18115.2
Area is: 18336.1
Area is: 18558.8
Area is: 18783.3
Area is: 19009.6
Area is: 19237.7
Area is: 19467.6
Area is: 19699.4
Area is: 19932.9
Area is: 20168.4
Area is: 20405.6
Area is: 20644.7
Area is: 20885.7
Area is: 21128.6
Area is: 21373.3
Area is: 21619.9
Area is: 21868.4
Area is: 22118.8
Area is: 22371.1
Area is: 22625.3
Area is: 22881.4
Area is: 23139.4
Area is: 23399.4
Area is: 23661.3
Area is: 23925.2
Area is: 24191
Area is: 24458.8
Area is: 24728.6
Area is: 25000.3
Area is: 25274
Area is: 25549.7
Area is: 25827.4
Area is: 26107.1
Area is: 26388.8
Area is: 26672.5
Area is: 26958.3
Area is: 27246.1
Area is: 27535.9
Area is: 27827.8
Area is: 28121.7
Area is: 28417.7
Area is: 28715.8
Area is: 29016
Area is: 29318.2
Area is: 29622.5
Area is: 29928.9
Area is: 30237.4
Area is: 30548.1
Area is: 30860.8
Area is: 31175.7
Area is: 31492.8
Area is: 31811.9
Area is: 32133.2
Area is: 32456.7
Area is: 32782.3
Area is: 33110.1
Area is: 33440.1
Area is: 33772.3
Area is: 34106.6
Area is: 34443.2
Area is: 34781.9
Area is: 35122.9
Area is: 35466.1
Area is: 35811.5
Area is: 36159.2
Area is: 36509.1
I just want to show in 1 output that the more intervals under the graph for the function the closer the approximation will be. Besides isn't there a way to make it work for whatever input you write?
#include <iostream>
double f( double );
using std::cin;
using std::cout;
int main(){
double sum, x, dx, a, b, n;
cout<<"Input x1: ";
cin >> a;
cout<<"Input x2: ";
cin >> b;
cout<<"Input no. of divisions: ";
cin>>n;
for (int k = 1; k <=n; k++ )
{
dx = ( b - a ) / k;
x = a + dx/2;
sum = 0;
do
{
sum += dx * f( x);
x += dx;
} while( x <= b );
std::cout << "k: " << k << '\t' << "Area is: " << sum << std::endl;
}
return 0;
}
double f(double x)
{
return x*x;
}
Input x1: 2
Input x2: 5
Input no. of divisions: 25
k: 1 Area is: 36.75
k: 2 Area is: 38.4375
k: 3 Area is: 38.75
k: 4 Area is: 38.8594
k: 5 Area is: 38.91
k: 6 Area is: 38.9375
k: 7 Area is: 38.9541
k: 8 Area is: 38.9648
k: 9 Area is: 38.9722
k: 10 Area is: 38.9775
k: 11 Area is: 38.9814
k: 12 Area is: 38.9844
k: 13 Area is: 38.9867
k: 14 Area is: 38.9885
k: 15 Area is: 38.99
k: 16 Area is: 38.9912
k: 17 Area is: 38.9922
k: 18 Area is: 38.9931
k: 19 Area is: 38.9938
k: 20 Area is: 38.9944
k: 21 Area is: 38.9949
k: 22 Area is: 38.9954
k: 23 Area is: 38.9957
k: 24 Area is: 38.9961
k: 25 Area is: 38.9964
Exit code: 0 (normal program termination)