How to decompose the sin(x)+cos(x) function into a Taylor series in C++ by overloading the operation of putting in the input stream and extracting from the output stream?

Faced with the following task: Implement a module whose connection overloads the operations of placing in the output stream and extracting from the input stream for a class containing methods for decomposing the sin(x)+cos(x) function into a Taylor series. The first n terms of the decomposition should be displayed on the screen. The value of n is determined by the user.

So far, I have implemented this program for sin(x). There is a problem in how to make a formula for the expansion of the function sin (x)+cos (x) in a series Taylor in C++. I searched all the sources on the Internet about this - the result is zero. I would be very grateful.

#include <iostream>
#include <cmath>

using namespace std;

class Node {
private:
    float x;
    int n;

public:
    Node();
    friend ostream& operator << (ostream&, const Node&);
    friend istream& operator >> (istream&, Node&);
};

Node::Node() {
    x = 0;
    n = 0;

}

istream& operator >> (istream& in, Node& tam) {
    cout << "Введите x: ";
    in >> tam.x;
    cout << "Введите n: ";
    in >> tam.n;
    return in;
}
ostream& operator << (ostream& out, const Node& tam) {
    float gh = 0.0;
    for (int st = 1; st <= tam.n; st++) {
        gh = exp((st*(log(tam.x)))) / st;
        gh = tam.x / st;
        out << "(" << gh << ")" << "+" << gh << "";
    }
    return out;
}

int main() {
    setlocale(LC_ALL, "Russian");
    Node tam;
    cin >> tam;
    cout << tam;
    system("pause");
    return 0;
}
Author: Harry, 2019-05-13

1 answers

If you are able to write for the sine (and you are sure that you did it correctly), then just use the fact that

enter a description of the image here

So the row for the sine should be enough :) "well, it will take a little more than that to converge, but not that much...

If not... Well, then, very little reflection suggests that

enter a description of the image here

Note also that the next term is obtained from the previous one (not counting the sign) by multiplying by x and division by k, so that all sorts of logarithms (also not working for negative values) and exponents are completely unnecessary.

I hope these hints are enough to write the code?

I would draft it for you, but from your condition I do not understand in what form the output is needed. Your output type is

(1)+1(0.5)+0.5(0.333333)+0.333333(0.25)+0.25(0.2)+0.2(0.166667)+0.166667

I can't count as a sample in any way ...

P.S. The Taylor series has another parameter - the point x0, relative to which all this is built. By default, you consider it equal to 0, i.e. you work with the Maclaurin series. This is just so, a terminological note.

P. P. S. To the one who put a minus-please indicate what error you found in me, and write your own, mathematically more correct answer :)

P. P. P. S. For the particularly gifted - implementation of the above in the form of code.

 10
Author: Harry, 2019-05-13 04:11:52