Untitled
unknown
plain_text
8 months ago
914 B
5
Indexable
Never
#include <cmath> #include <cstdio> #include <cstdlib> using namespace std; int main() { int x, desired_error, exp_approx, i, factorial = 1, number; cout << "calculate exp(x) with the Taylor series approximation "; cout << "\nexp_approx = 1 + x + x^2 / 2! + x^3 / 3! + x^i/i!\n"; cout << " input x, i and a desired number of series\n "; cin >> x; cin >> number; cin >> desired_error; for(i= number; i<=number; i++){ if(abs( exp_approx - exp(x) ) < desired_error) { break; } else if(number < 0){ cout << "Error ! factorial of a negative number doesn't exist"; break; } else{ factorial *= i; } exp_approx = 1 + x + pow(x,2)/2*factorial + pow(x,3)/3*factorial + pow(x,i)/i*factorial; } cout << "exp_approx =" << exp_approx; return 0; }
Leave a Comment