Finding difficulty creating a user function program:
Given: tanh (x) = 1-2e^(-2x) + 2e^(-4x) - 2e^(-6x) + 2e^(-8x) -2e^(-10x) + ...
cos(x) = 1-x^(2)/(2!) + x^(4)/(4!) - x^(6)/(6!) + x^(8)/(8!) +...
Trying to write two functions as follows:
tanh(ax) where a and x are passed to the function and
cos (ax) where a and x are passed to the function.
The functions must give answers that are accurate at least to six
significant digits.
With this in mind, I'm trying to write a program that tests the functions, by
computing F= 5tanh(ax) +4cos(ax) where 0 less than or equal to x less than or equal to 2*pi
with the values of a and x entered by the user.
*I would probably check the answers with a calculator.
Check the functions with this data:
a
0
2.78
0
1
1.5
3.78
x
0
0
2.78
2.78
2.6
1
F
4
4
4
1.220321454
2.092175112
1.782610497
Have these programs so far:
e^(x)
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
|
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i,j, n;
double ex = 1, factorial, x, xpower = 1;
cout << "This program will approximate e value using a finite terms \n";
cout << "in the exponential series.";
cout << "\nEnter an integer n to specify the number of terms in series: ";
cin >> n;
cout << "\nEnter the value x for e to x: ";
cin >> x;
for (i=0; i < n-1; i++) //number of terms: n
{
factorial = 1;
for ( j=0; j< i+1; j++)
factorial = factorial * (j+1); //calculate 1 * 2 * 3* .. * i
xpower = xpower * x;
ex = ex + xpower / factorial;
}
cout << setprecision(20);
cout << "For 10 terms, the e to x value = " << ex << endl;
}
|
sin(x)
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 39 40 41 42 43 44 45 46 47
|
#include <iostream>
#include <iomanip>
#define PI 3.14159265358979
using namespace std;
int main()
{
int i,j, n;
double x, xsmall, temp, sin_x, x_power, factorial;
cout << "This program will approximate sin(x) value using a finite terms \n";
cout << "in the exponential series.";
cout << "\nEnter an integer n to specify the number of terms in series: ";
cin >> n;
cout << "\nEnter the value of x: ";
cin >> x;
//Transform x to less than 2 PI; i.e. xsmall is less than 2 PI
temp = x / (2 * PI);
xsmall = ( temp - int (temp)) * 2 * PI;
//Another way to transform x to less than 2 PI; i.e. xsmall is less than 2 PI
// xsmall = x;
// while ( xsmall >= 2 * PI)
// xsmall = xsmall - (2 * PI);
sin_x = xsmall;
for (i=0; i < n-1; i++) //number of terms: n
{
factorial = 1;
x_power = 1;
for ( j=0; j< 2*i+3; j++)
{
factorial = factorial * (j+1); //calculate 1*2*3*(j+1)!
x_power = x_power * xsmall; //calculate x_power exp(k)
}
if ( i%2 == 0)
sin_x = sin_x - x_power / factorial; //for i even
else
sin_x = sin_x + x_power / factorial; //for i odd
}
cout << setprecision(10);
cout << "For " << n << " terms, the sin(" << x <<") value = "
<< sin_x << endl;
}
|
cos(x):
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 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include <iostream>
#include <iomanip>
#define PI 3.14159265358979
using namespace std;
int main()
{
int i,j, n;
double x, xsmall, temp, cos_x, x_power, factorial;
cout << "This program will approximate cos(x) value using a finite terms \n";
cout << "in the exponential series.";
cout << "\nEnter an integer n to specify the number of terms in series: ";
cin >> n;
cout << "\nEnter the value of x: ";
cin >> x;
//Transform x to less than 2 PI; i.e. xsmall is less than 2 PI
temp = x / (2 * PI);
xsmall = ( temp - int (temp)) * 2 * PI;
//Another way to transform x to less than 2 PI; i.e. xsmall is less than 2 PI
// xsmall = x;
// while ( xsmall >= 2 * PI)
// xsmall = xsmall - (2 * PI);
cos_x = 1;
for (i=0; i < n-1; i++) //number of terms: n
{
factorial = 1;
x_power = 1;
for ( j=0; j< 2*i+2; j++)
{
factorial = factorial * (j+1); //calculate 1*2*3*(j+1)!
x_power = x_power * xsmall; //calculate x_power exp(k)
}
if ( i%2 == 0)
cos_x = cos_x - x_power / factorial; //for i even
else
cos_x = cos_x + x_power / factorial; //for i odd
}
cout << setprecision(10);
cout << "For " << n << " terms, the cos(" << x <<") value = "
<< cos_x << endl;
}
|