Given: tanh(x) = 1 - 2 e^-2x + 2 e^x^-4x - 2 e^-6x + 2 e^-8x - 2 e^-10x + .... c
ID: 3833827 • Letter: G
Question
Given: tanh(x) = 1 - 2 e^-2x + 2 e^x^-4x - 2 e^-6x + 2 e^-8x - 2 e^-10x + .... cos(x)= 1 -x^2/(2!) + x^4/(4!) - x^6/(6!) + x^8/(8!) + .... where 0 lessthanorequalto x lessthanorequalto 2 pi Write two functions: 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. Write a program to test the functions by computing F = 5 tanh(ax) + 4 cos(ax) where 0 lessthanorequalto xExplanation / Answer
#include<iostream>
#include<cmath>
using namespace std;
double fact(double n)
{
double fact=1;
for(int i = 1; i <=n; ++i)
{
fact *= (double)i;
}
return fact;
}
double tanh(double a, double x){
double sum = 0,t=1.00,y=0;
int i=0;
x = x*a;
for(i=0;i<=10;i+=2)
{
y = t*2.00*exp(-2.00*x);
sum+=y;
t*=-1.00;
}
return sum;
}
double cos(double a, double x)
{
int i;
double sum=0.00,t=1.00,y=0.00;
x = x*a;
for(i=0;i<=8;i+=2)
{
sum+=(t*pow(x,(double)i)/fact(i));
t*=-1.00;
}
return sum;
}
int main()
{
double a,x,res;
cout<<"Enter a: ";
cin>>a;
cout<<"Enter x: ";
cin>>x;
res = 5.00*tanh(a,x) + 4.00*cos(a,x);
cout<<"F = "<<res<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.