Faced the following task: Implement a module, the connection of which overloads the operations of placing into 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 screen should display the first n terms of the expansion. The value n is determined by the user.

While implemented this program for sin (x). The problem arises in how to create a formula for the decomposition of the function sin (x) + cos (x) in a Taylor series in C ++. I searched all the sources on the Internet about this - the result is zero. I will 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; } 
  • Frankly, I do not see a formula for the sinus ... And - in what form do you need to derive? Write an example for the same sine for the first few members. What is displayed at you - I'm sorry, some kind of nonsense ... - Harry

1 answer 1

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

enter image description here

so the series for the sine should be quite enough :) - well, we need members for convergence a little more, but not so much ...

If not ... Well, then very small reflections suggest that

enter image description here

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

I hope these tips are enough to write code?

I would have sketched it for you, but from your condition I do not understand in what form the conclusion is needed. Your type inference

 (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 take it as a sample ...

PS The Taylor series has one more parameter - the point x 0 , relative to which it is all built. By default you consider it equal to 0, i.e. working with a number of Maclaurin. This is just a terminological note.

PPS To the one who put a minus - please indicate what mistake you have found in me, and write your own, mathematically more correct answer :)

PPPS For the gifted - the implementation of the above in the form of code .

  • For the formula - thank you so much! Already added everything and corrected the mistakes ... - Mike Taylor