Advertisement
Guest User

Untitled

a guest
May 19th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. DERIVATIVE
  2.  
  3. template <class T>
  4. class Difference{
  5. T a, b, x;
  6. T fun(T x){
  7. return cos(x/2);
  8. }
  9. public:
  10. Difference(T a, T b, T x){
  11. try{
  12. if(x<a || x>b){
  13. std::string boo {"Podane x nie nalezy do siatki.\n"};
  14. throw boo;
  15. }
  16. this->a = a;
  17. this->b = b;
  18. this->x = x;
  19. }catch(std::string &ex){
  20. std::cout<<ex;
  21. exit(0);
  22. }
  23. }
  24.  
  25. //For derivatives where x is in inside node.
  26. T forawardDifference(T h){
  27. if(h!=0 && x>a && x<b){
  28. return (fun(x+h) - fun(x))/h;
  29. }
  30. return -100;
  31. }
  32. T backwardDifference(T h){
  33. if(h!=0 && x>a && x<b){
  34. return (fun(x)-fun(x-h))/h;
  35. }
  36. return -100;
  37. }
  38. T centralDifference(T h){
  39. if(h!=0 && x>a && x<b){
  40. return (fun(x+h) - fun(x-h))/(2*h);
  41. }
  42. return -100;
  43. }
  44. //To calculate derivative where x is in first or end node
  45. T firstOrEndNodeDerivativeTwoPoint(T h){
  46. if(h!=0 && x==a){
  47. return (fun(a+h)-fun(a))/h;
  48. }
  49. else if(h!=0 && x==b){
  50. return (fun(b)-fun(b-h))/h;
  51. }
  52. return -100;
  53. }
  54. T firstOrEndNodeDerivativeThreePoint(T h){
  55. if(h!=0 && x==a){
  56. return (-3.0/2.0*fun(a)+2*fun(a+h)-0.5*fun(a+2*h))/h;
  57. }
  58. else if(h!=0 && x==b){
  59. return (0.5*fun(b-2*h)-2*fun(b-h)+3.0/2.0*fun(b))/h;
  60. }
  61. return -100;
  62. }
  63.  
  64. void changeX(T x){
  65. try{
  66. if(x<a || x>b){
  67. std::string x{"Podane x wychodzi poza siatke.\nX nie zostalo zmienione.\n"};
  68. throw x;
  69. }
  70. this->x = x;
  71. }catch(std::string &ex){
  72. std::cout<<ex;
  73. }
  74. }
  75. };
  76.  
  77.  
  78. MAIN
  79.  
  80. #include <iostream>
  81. #include <cmath>
  82. #include <vector>
  83. #include <fstream>
  84. #include <cmath>
  85. #include "derivative.h"
  86.  
  87. #define M_PI 3.14159265359
  88.  
  89. template <typename T>
  90. T difference(T x){
  91. return -sin(x/2.0)*1.0/2.0;
  92. }
  93.  
  94. int main(){
  95. std::cout<<difference(0)<<" "<<difference(M_PI)<<" "<<difference(M_PI/2.0)<<std::endl;
  96. const double stepD = 0.0000000001;
  97. const double difABD = difference(M_PI/2.0);
  98. Difference<double> diffD(0.0, M_PI, (M_PI)/2);
  99. std::vector<double> forwDiffD, backwDiffD, centralDiffD, firstDiffTwoPointD,
  100. firstDiffThreePointD, lastDiffTwoPointD, lastDiffThreePointD;
  101. for(double h = stepD; h<1.4; h+=stepD*100000){
  102. forwDiffD.push_back(fabs(difABD-diffD.forawardDifference(h)));
  103. backwDiffD.push_back(fabs(difABD-diffD.backwardDifference(h)));
  104. centralDiffD.push_back(fabs(difABD-diffD.centralDifference(h)));
  105. }
  106. const double difAD = difference(0);
  107. diffD.changeX(0.0);
  108. for(double h = stepD; h<1.4; h+=stepD*100000){
  109. firstDiffTwoPointD.push_back((difAD-diffD.firstOrEndNodeDerivativeTwoPoint(h)));
  110. firstDiffThreePointD.push_back(fabs(difAD-diffD.firstOrEndNodeDerivativeThreePoint(h)));
  111. }
  112. const double difBD = difference(M_PI);
  113. diffD.changeX(M_PI);
  114. for(double h = stepD; h<1.4; h+=stepD*100000){
  115. lastDiffTwoPointD.push_back(fabs(difBD-diffD.firstOrEndNodeDerivativeTwoPoint(h)));
  116. lastDiffThreePointD.push_back(fabs(difBD-diffD.firstOrEndNodeDerivativeThreePoint(h)));
  117. }
  118. std::ofstream file1;
  119. file1.open("dataDouble.dat");
  120. file1<<"H"<<" "<<"ErrorForw"<<" "<<"ErrorBack"<<" "<<
  121. "ErrorCent"<<" "<<"ErrorFirstTw"<<" "<<"ErrorFirstTh"<<
  122. " "<<"ErrorEndTw"<<" "<<"ErrorEndTh"<<std::endl;
  123. int i = 0;
  124. for(double h = stepD; h<1.4; h+=stepD*100000){
  125. file1 << h << " "<<forwDiffD.at(i);
  126. file1 <<" "<<backwDiffD.at(i);
  127. file1 <<" "<<centralDiffD.at(i);
  128. file1 <<" "<<firstDiffTwoPointD.at(i);
  129. file1 <<" "<<firstDiffThreePointD.at(i);
  130. file1 <<" "<<lastDiffTwoPointD.at(i);
  131. file1 <<" "<<lastDiffThreePointD.at(i)<<std::endl;
  132. i++;
  133. }
  134. file1.close();
  135. std::cout<<difference(0)<<" "<<difference(M_PI)<<" "<<difference(M_PI/2.0)<<std::endl;
  136. const float step = 0.0000000001;
  137. const float DIFAB = difference(M_PI/2.0);
  138. Difference<float> diff(0.0, M_PI, (M_PI)/2);
  139. std::vector<float> forwDiff, backwDiff, centralDiff, firstDiffTwoPoint,
  140. firstDiffThreePoint, lastDiffTwoPoint, lastDiffThreePoint;
  141. for(float h = step; h<1.4; h+=step*100000){
  142. forwDiff.push_back(fabs(DIFAB-diff.forawardDifference(h)));
  143. backwDiff.push_back(fabs(DIFAB-diff.backwardDifference(h)));
  144. centralDiff.push_back(fabs(DIFAB-diff.centralDifference(h)));
  145. }
  146. const float DIFA = difference(0);
  147. diff.changeX(0.0);
  148. for(float h = step; h<1.4; h+=step*100000){
  149. firstDiffTwoPoint.push_back((DIFA-diff.firstOrEndNodeDerivativeTwoPoint(h)));
  150. firstDiffThreePoint.push_back(fabs(DIFA-diff.firstOrEndNodeDerivativeThreePoint(h)));
  151. }
  152. const float DIFB = difference(M_PI);
  153. diff.changeX(M_PI);
  154. for(float h = step; h<1.4; h+=step*100000){
  155. lastDiffTwoPoint.push_back(fabs(DIFB-diff.firstOrEndNodeDerivativeTwoPoint(h)));
  156. lastDiffThreePoint.push_back(fabs(DIFB-diff.firstOrEndNodeDerivativeThreePoint(h)));
  157. }
  158. file1.open("dataFloat.dat");
  159. file1<<"H"<<" "<<"ErrorForw"<<" "<<"ErrorBack"<<" "<<
  160. "ErrorCent"<<" "<<"ErrorFirstTw"<<" "<<"ErrorFirstTh"<<
  161. " "<<"ErrorEndTw"<<" "<<"ErrorEndTh"<<std::endl;
  162. i = 0;
  163. for(float h = step; h<1.4; h+=step*100000){
  164. file1 << h << " "<<forwDiff.at(i);
  165. file1 <<" "<<backwDiff.at(i);
  166. file1 <<" "<<centralDiff.at(i);
  167. file1 <<" "<<firstDiffTwoPoint.at(i);
  168. file1 <<" "<<firstDiffThreePoint.at(i);
  169. file1 <<" "<<lastDiffTwoPoint.at(i);
  170. file1 <<" "<<lastDiffThreePoint.at(i)<<std::endl;
  171. i++;
  172. }
  173. file1.close();
  174. return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement