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
#include <iostream>
#include <iomanip>
usingnamespace 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;
}
#include <iostream>
#include <iomanip>
#define PI 3.14159265358979
usingnamespace 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;
}
#include <iostream>
#include <iomanip>
#define PI 3.14159265358979
usingnamespace 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;
}
can anyone help me with writing this code:
Trying to write a larger program incorporating each of the above programs as functions. Also using each to generate a result and then check the result with another new function which uses the actual functions provided by the math..h header (for which we use this in C++ #include <cmath> )
Also like I said before:
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.