Guest User

Untitled

a guest
Jun 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4. int main()
  5. {
  6.     int i=1;
  7.     float x, result, sum, temp=0, sumhigh, temphigh;
  8.     cout<<"Please give a value of x for the function f(x) = sinh(x)\nx = "; // Tom Ellaby. Tuesday Afternoon Session.
  9.     cin>>x;
  10.     if (fabs(x)>5)                                                          // This checks whether to use the taylor expansion for small values of x
  11.     {                                                                       // or large values. Since sinh is an odd function, the absolute value is
  12.         if (x<0)                                                            // checked.
  13.         {
  14.             sum=-0.5*exp(fabs(x));                                          // This part simply inverts the sign if a negative value was entered,
  15.         }                                                                   // since the large approximation does not deal with this as the small
  16.         else                                                                // approximation does.
  17.         {                                                                   //
  18.             sum=0.5*exp(fabs(x));                                           //
  19.         }                                                                   //
  20.     }
  21.     else                                                                    // if  the entered value was small, the expansion is used as an
  22.     {                                                                       // instead.
  23.         result=x;                                                           //
  24.         sum=result;                                                         //
  25.         do                                                                  //
  26.         {                                                                   //
  27.             result=result*((x*x)/((i+1)*(i+2)));                            // This line calculates each term in the sequence from the previous, and
  28.             temp=sum;                                                       // assigns it to the variable 'result'. These values are then summed,
  29.             sum+=result;                                                    // giving the value of sinh x, which becomes more accurate with each
  30.             i=i+2;                                                          // iteration.
  31.             temphigh=10000*temp;                                            // This sets the current value for sinh(x) and the value from the previous
  32.             sumhigh=10000*sum;                                              // iteration to 10000 times their actual value. This is done so that it
  33.             //cout<<result<<", "<<i<<", "<<sum<<endl;                       // can be seen when the next value calculated is the same as the last to
  34.         }                                                                   // 4 decimal places. The loop is finished when this happens, as the
  35.         while ((int)temphigh!=(int)sumhigh);                                // desired accuracy has been reached.
  36.     }
  37.     cout<<"f(x) = "<<sum;                                                   // This line simply prints the calculated value of sinh(x)
  38.  
  39.     return 0;
  40. }
Add Comment
Please, Sign In to add comment