Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- using namespace std;
- int main()
- {
- int i=1;
- float x, result, sum, temp=0, sumhigh, temphigh;
- cout<<"Please give a value of x for the function f(x) = sinh(x)\nx = "; // Tom Ellaby. Tuesday Afternoon Session.
- cin>>x;
- if (fabs(x)>5) // This checks whether to use the taylor expansion for small values of x
- { // or large values. Since sinh is an odd function, the absolute value is
- if (x<0) // checked.
- {
- sum=-0.5*exp(fabs(x)); // This part simply inverts the sign if a negative value was entered,
- } // since the large approximation does not deal with this as the small
- else // approximation does.
- { //
- sum=0.5*exp(fabs(x)); //
- } //
- }
- else // if the entered value was small, the expansion is used as an
- { // instead.
- result=x; //
- sum=result; //
- do //
- { //
- result=result*((x*x)/((i+1)*(i+2))); // This line calculates each term in the sequence from the previous, and
- temp=sum; // assigns it to the variable 'result'. These values are then summed,
- sum+=result; // giving the value of sinh x, which becomes more accurate with each
- i=i+2; // iteration.
- temphigh=10000*temp; // This sets the current value for sinh(x) and the value from the previous
- sumhigh=10000*sum; // iteration to 10000 times their actual value. This is done so that it
- //cout<<result<<", "<<i<<", "<<sum<<endl; // can be seen when the next value calculated is the same as the last to
- } // 4 decimal places. The loop is finished when this happens, as the
- while ((int)temphigh!=(int)sumhigh); // desired accuracy has been reached.
- }
- cout<<"f(x) = "<<sum; // This line simply prints the calculated value of sinh(x)
- return 0;
- }
Add Comment
Please, Sign In to add comment