So, I'm pretty inexperienced with calculus, and in C++, so please bear with me if I'm misunderstanding this completely. I'm supposed to be testing this Taylor series program for convergence, which I'm told is when the output value does no change. However, the way mine is written out, it seems impossible as I'm iterating through for loops while implementing it. After around 12 it's no longer accurate to the library sin(); but I'm not sure if that's the same thing because it doesn't seem to be. Advice on what I'm looking for would be grand, I really appreciate it. Apologies again if this question is stupid!
Here is my code:
#include <iostream>
#include<cmath>
using namespace std;
double getFactorial (double num)
{
long double factorial = 1.0;
for (int i = 1; i <= num; ++i)
{
factorial *= i; //iterates from 1 to num value, multiplying by each number
}
return factorial;
}
double taylorSin(double num){
double value=0;
for(int i=0;i<20;i++){
value+=pow(-1.0,i)*pow(num,2*i+1)/getFactorial(2*i+1);
}
return value;
}
int main ()
{ cout<<getFactorial(6);
for(double i=1;i<=12;i++){
//loops through given amount of values to test function
double series=i; //assign double type variable with value of i
//cout<<"Taylor function result is "<<taylorSin(series)<<endl;
//cout<<"Library sin result is "<<sin(series)<<endl;
}
return 0;
}