x Y(x) S(x) #iter
-1.0000 0.0558 -0.0559 54
-0.9000 0.0079 -0.0079 74
-0.8000 -0.0389 0.0388 52
-0.7000 -0.0833 0.0833 73
-0.6000 -0.1243 0.1243 54
-0.5000 -0.1607 0.1610 21
-0.4000 -0.1919 0.1919 50
-0.3000 -0.2168 0.2168 67
-0.2000 -0.2351 0.2351 54
-0.1000 -0.2463 0.2462 46
0.0000 -0.2500 0.2500 316
0.1000 -0.2463 0.2462 46
0.2000 -0.2351 0.2351 54
0.3000 -0.2168 0.2168 67
0.4000 -0.1919 0.1919 50
0.5000 -0.1607 0.1610 21
0.6000 -0.1243 0.1243 54
0.7000 -0.0833 0.0833 73
0.8000 -0.0389 0.0388 52
0.9000 0.0079 -0.0079 74
1.0000 0.0558 -0.0559 54
1.1000 0.1036 -0.1034 49
1.2000 0.1498 -0.1497 71
1.3000 0.1932 -0.1932 80 |
You mean you want output like this? |
BTW, are you sure that your functions Y(x) and S(x) have the correct sign? |
| You also need to explain what is meant by "accuracy Sigma" - here it is taken as the absolute value of a term at which the series is truncated, but it is still mildly inaccurate as you can see from the table below. Smaller Sigma would remove the truncation error. |
| BTW, are you sure that your functions Y(x) and S(x) have the correct sign? It should be, it is a task from a textbook |
| Display a table of values of the function Y (x) and its expansion in a series S (x) with Sigma precision. |
| Probably it is how it should be more correct, I think. It means that I just translated it wrong before. |
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double S( double x, double Sigma, int &iter ) // Code for the series S(x)
{
double sum = 0.0, term = 1.0;
// YOUR code goes here
// Loop round, setting each new term and adding to sum until abs( term ) < Sigma
for ( .... )
{
...
}
// Don't forget to set the number of iterations
return sum;
}
double Y( double x )
{
return .... // YOUR code for the function Y(x) - just a one-liner
}
int main()
{
int iter;
const double sigma = 1.0e-5;
// YOUR code to write headers
// YOUR code to loop through values of x and output x, Y(x), S(x) and number of iterations
for ( ... )
{
...
}
} |
#include <iostream.h>
#include <math.h>
#include <iomanip.h>
typedef double (*uf)(double, double, int &);
void tabl(double, double, double, double, uf);
double y(double, double, int &);
double s(double, double, int &);
int main()
{
cout << setw(8) <<"x"<< setw(15) <<"y(x)"<< setw(10) << "k" << endl;
tabl(-0.9,0.9,0.1,0.0001,y);
cout << endl;
cout << setw(8) <<"x"<< setw(15) <<"s(x)"<< setw(10) << "k" <<endl ;
tabl(-0.9,0.9,0.1,0.0001,s);
return 0;
}
void tabl(double a, double b, double h, double eps, uf fun)
{
int k=0;
double sum;
for (double x=a; x<b+h/2; x+=h)
{
sum=fun(x,eps,k);
cout << setw(8) << x << setw(15) << sum << setw(10) << k << endl;
}
}
double y(double x, double eps, int &k)
{
return (1./2.)*log((1+x)/(1-x));
}
double s(double x, double eps, int &k)
{
double a,c,sum;
sum=a=c=x;
k=1;
while (fabs(c)>eps)
{
c = pow(x,2)/(2*k+1);
a *= c;
sum += a;
k++;
}
return sum;
} |
|
|