Hi,
I have to implement a code that uses two different approximations of the integration of a function and compares them by using the deviation between them. It then asks to make a table for the deviations for each value of i. I have already written the code about it, but I can't find how to make a proper table where for each value of i, I can give the respective deviations.
Here is my code:
#include<iostream>
#include<cmath>
usingnamespace std;
double R(double x)
{
double y=1.0/((25*x*x)+1);
return y;
}
double Int_R(double a,double b)
{
double y= (atan(5*b)-atan(5*a))/5;
return y;
}
int main()
{
int i;
double xi;
double xj;
double y;
double z;
double w;
for(i=0; i<=7; i++)
{
//These are the values of the endpoints for each value of i between 0 and 7.
xi= -1+i*0.25;
cout <<"xi=" << xi << endl;
xj= xi+0.25;
cout << "x(i+1)=" << xj << endl;
//Here I calculate and implement the Simposon's rule:
y=(xj-xi)*(R(xi)+4*R((xi+xj)/2)+R(xj));
//Here I again calculate the approximation of the integration of the Runge function using the mid-point rule in order to then calculate the deviation from its approximation using the Simpson's rule.
z=(xj-xi)*R((xj+xi)/2);
//Here I calculate the deviation from the two approximations:
w=y-z;
cout << "The value of the integrated Runge function is=" << Int_R(xi, xj) << endl;
cout << "The approximation of the Runge function using the mid-point rule is=" << z << endl;
cout << "The approximation of the Runge function using the Simpson's rule rule is=" << y << endl;
cout << "The deviation of using the two approximations menntioned above in each sub-interval are=" << w << endl;
}
return 0;
}
vector<double> table (estimatedsize); //above the loop
...
table.push_back(Int_R(xi, xj)); //before the cout statements
...
cout <<"blah blah" << table[i] << ... //change cout to use the table instead of recompute to print
or do you want a table of all the values ? you can make a struct to hold them and make a vector of that ... do you need to see this?
or, are you saying table and mean "formatted output" ?
You are missing a factor of (1/6) in Simpson's rule. (Think what would happen if R() was a constant ... your area would be 6 times too big.) y=(xj-xi)*(R(xi)+4*R((xi+xj)/2)+R(xj)); // <==== 6 times too big
@jonnin
What I want to do is make a table that shows the values that i takes, then the values of xi and xj for each of these i in the form of a subinterval [xi,xj] and then finally show the deviation between the two approximations in each of these subintervals. What I'm thinking is write a matrix of dimensions 8x3 (because i takes 8 values) with the first column showing the values of i, the second column containing the values of the [xi,xj] for each of the i, and the last column showing the respective deviations.
Can I do this as a matrix or do I have to do it with table.push_back(), which btw what does it exactly do?
Push back puts an item on the end of a vector (not a math vector, its an array-like container that is poorly named).
you can do it any of many ways.
you can do a 2-d array
double data[8][3]; //no push back, just direct access, data[row][col] = value
that is the simplest way.
if you did it with vectors, the 8 and 3 can grow to fit any problem size.
that looks like
vector < vector <double> > data (8, vector<double>(3));
and here, using the 8 and 3 up front, you don't have to use push back.
but if you wanted to be able to do more values than 8 you would need push back, it would grow it to 9, 10, 11, … each call would grow it by one slot.
use looks the same as array.. its still data[row][col] = value until you need the push back. Lets ignore the push-back part for now, though, and just allocate it as big as you want up front. arrays are part of the language syntax, vectors need <vector>