Table of values of the function

Hello, does anyone understand exactly how this is done? Otherwise, I don't understand how can make such a code at all


Task:
Display a table of values of the function Y (x) and its expansion in a series S (x) with an accuracy of Σ. Print the number of iterations required to achieve the specified precision. Calculate S (x) and Y (x) as a function.

Task image
https://imgur.com/a/s1Ox4PS
Last edited on
not with those instructions. I think the instructions have some assumptions you would get from having listened in class that were left unstated.
what is x? Is this a symbolic math task or a numerical one?

I mean its easy to make Y as a function, and easy to sum up Y over (what is x) an interval. From there though, what expansion? Taylor? Something else? Unclear to people not in your class....
Now with this picture it should be clearer, at the picture shows which is which.
https://imgur.com/a/06yWltJ
You mean you want output like this?

BTW, are you sure that your functions Y(x) and S(x) have the correct sign? One appears to be minus the other.
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.

You really need to work at explaining your task a bit better @Obeliks2

              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
Last edited on

You mean you want output like this?

As I sort of understood from the task, then yes


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

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.



Display a table of values of the function Y (x) and its expansion in a series S (x) with Sigma precision. Print the number of iterations required to achieve the specified precision. Calculate S (x) and Y (x) as a function.


Probably it is how it should be more correct, I think. It means that I just translated it wrong before.


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

Then you need a better textbook. Y(X) and S(X) have opposite signs, as you could tell simply by putting x = 0.


Display a table of values of the function Y (x) and its expansion in a series S (x) with Sigma precision.

Simply putting it in bold type ... doesn't explain anything.


Probably it is how it should be more correct, I think. It means that I just translated it wrong before.

So, are you happy now?
I thought it might be confusing because of the translation, but even if it's so confusing, I don't even know what to do.

Anyway, can you show me the code, what do you think should be solved correctly? I think you know more than my teacher, who most likely knows nothing and has such a bad textbook
I'm not going to give you the code as you have yet to write any in this forum, but here's a template.

#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 ( ... )
   {
      ...
   }
}

Using a lighter example, I probably figured out how to do code, but what should I insert into these lines from my task?

#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;
}
That code has nothing to do with your example.
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
#include <iostream>
#include <cmath>
 
const double eps (1e-5), a (-1), b (1.3);
 
double sx(double x) {
    double sum (0.), an;
    int k (2);
    
    do {
        an = cos(k * x) / (k * k - 1);
        if (!(k % 2)) {
            an *= -1;
        }
        sum += an;
        k++;
    } while (fabs(an) >= eps);
    
    return sum;
}
 
double yx(double x) {
    return (2 * x * sin(x) - 2 + cos(x)) / 4.;
}
 
int main() {
    double sum (0.), x, h (.1);
    
    std::cout << "x = ";
    std::cin >> x;
    
    std::cout << "x\t\ts(x)\t\ty(x)" << std::endl;
    for (double i = a; i <= b; i += h) {
        std::cout << i << "\t\t" << sx(i) << "\t" << yx(i) << std::endl;
    }
    
    return 0;
}
Last edited on
Is it correct now?
Your code is more correct than the original problem in the images that you posted.

Your code has (effectively) a (-1)k+1 term, which gives the correct expansion. The images posted have a (-1)k term, which would give the wrong sign.

You haven't counted iterations, but apart from that it's OK.

Did you write the code?

Topic archived. No new replies allowed.