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 6.59 KB | None | 0 0
  1. 1000000000000000000000000000000000000000000
  2.  
  3. #include <iostream>
  4. #include <cmath>
  5. #include <fstream>
  6. #include <vector>
  7.  
  8. class DifferentialEquation{
  9. double U(double t){
  10. return (1 - exp(-10*(t+atan(t))));
  11. }
  12. double h;
  13. double t;
  14. std::ofstream file;
  15. public:
  16. DifferentialEquation(double h, double t){
  17. this->h = h;
  18. this->t = t;
  19. file.open("AnaliticData.dat");
  20. if(!file.is_open()){
  21. std::cerr<<"Blad otwarcia pliku.\n";
  22. exit(1);
  23. }
  24. for(double i = 0.0; i<t; i+=h){
  25. file<<i<<" "<<U(i)<<std::endl;
  26. }
  27. file.close();
  28. }
  29.  
  30. std::vector<double> directEulerMethod(){
  31. double y = 0.0;
  32. file.open("DEM.dat");
  33. if(!file.is_open()){
  34. std::cerr<<"Blad otwarcia pliku.\n";
  35. exit(1);
  36. }
  37. std::vector<double> error;
  38. for(double i = 0.0; i<t; i+=h){
  39. error.push_back(fabs(y-U(i)));
  40. y = y + (10.0*i*i+20.0)/(i*i+1.0)*(y-1.0)*h;
  41. file<<i<<" "<<y<<"\n";
  42. }
  43. file.close();
  44. return error;
  45. }
  46.  
  47. std::vector<double> indirectEulerMethod(){
  48. double y = 0.0;
  49. file.open("IEM.dat");
  50. if(!file.is_open()){
  51. std::cerr<<"Blad otwarcia pliku.\n";
  52. exit(1);
  53. }
  54. std::vector<double> error;
  55. for(double i = 0.0; i<t; i+=h){
  56. double t1 = i+h;
  57. double ft1 = (10.0*t1*t1+20.0)/(t1*t1+1);
  58. error.push_back(fabs(y - U(i)));
  59. y = (y/h + ft1)/(1.0/h+ft1);
  60. file<<i<<" "<<y<<std::endl;
  61. }
  62. file.close();
  63. return error;
  64. }
  65.  
  66. std::vector<double> indirectTrapeziumMethod(){
  67. double y = 0.0;
  68. file.open("ITM.dat");
  69. if(!file.is_open()){
  70. std::cerr<<"Blad otwarcia pliku.\n";
  71. exit(1);
  72. }
  73. std::vector<double> error;
  74. for(double i = 0.0; i<t; i+=h){
  75. double t1 = i+h;
  76. double ft = (10.0*i*i+20.0)/(2*i*i+2);
  77. double ft1 = (10.0*t1*t1+20.0)/(2*t1*t1+2);
  78. error.push_back(fabs(y-U(i)));
  79. y = (y/h - ft*(y-1)+ft1)/(1.0/h + ft1);
  80. file<<i<<" "<<y<<std::endl;
  81. }
  82. file.close();
  83. return error;
  84. }
  85. };
  86.  
  87. int main(){
  88. double t = 5;
  89. double h = 0.00001;
  90. DifferentialEquation equation(h, t);
  91. std::vector<double> errorDE, errorIE, errorIT;
  92. errorDE = equation.directEulerMethod();
  93. errorIE = equation.indirectEulerMethod();
  94. errorIT = equation.indirectTrapeziumMethod();
  95.  
  96. std::ofstream file;
  97. file.open("Error.dat");
  98. if(!file.is_open()){
  99. std::cerr<<"Blad otwarcia pliku.\n";
  100. exit(1);
  101. }
  102. int j = 0;
  103. for(double i = 0.0; i<t; i+=h){
  104. file<<i<<" "<<errorDE.at(j)<<" "<<errorIE.at(j)<<" "<<errorIT.at(j++)<<std::endl;
  105. }
  106. file.close();
  107. return 0;
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114. 999999999999999999999999999999999999999999999999999999
  115.  
  116. #include <iostream>
  117. #include <cmath>
  118. #include <vector>
  119. #include <fstream>
  120. #include <algorithm>
  121. //d^2U(x)/(dx^2) + 4*U(x) + tan(x) = 0
  122.  
  123. double U(double x){
  124. return (2*x*cos(2*x)+2*sin(2*x)-log(2)*sin(2*x)-2*log(cos(x))*sin(2*x))/4.0;
  125. }
  126.  
  127. class Discretization{
  128. std::vector<double> l, d, u, b, xn;
  129. double x1, x2;
  130. double h;
  131. int numberOfSteps;
  132. char c;
  133. double U(double x){
  134. return 1.0/4.0*(2*x*cos(2*x)+2*sin(2*x)-log(2)*sin(2*x)-2*log(cos(x))*sin(2*x));
  135. }
  136.  
  137. double maxError(){
  138. double max = 0.0;
  139. for(int i = 0; i<numberOfSteps; i++){
  140. double error = fabs(xn.at(i) - U(h*i));
  141. if(max<error){
  142. max = error;
  143. }
  144. }
  145. return max;
  146. }
  147.  
  148. void clearVectors(){
  149. l.clear();
  150. d.clear();
  151. u.clear();
  152. b.clear();
  153. xn.clear();
  154. }
  155. public:
  156. Discretization(int numberOfSteps){
  157. this->numberOfSteps = numberOfSteps;
  158. x1 = 0.0;
  159. x2 = M_PI/4.0;
  160. h = fabs(x1-x2)/(numberOfSteps-1);
  161. }
  162. void thomasAlgorithm(){
  163. std::vector<double> n, r;
  164. n.push_back(d.at(0));
  165. r.push_back(b.at(0));
  166. for(int i = 1; i<numberOfSteps; i++){
  167. n.push_back(d.at(i)-(l.at(i)*u.at(i-1))/n.at(i-1));
  168. r.push_back(b.at(i)-(l.at(i)*r.at(i-1))/n.at(i-1));
  169. }
  170. xn.push_back(1.0/n.at(numberOfSteps-1)*r.at(numberOfSteps-1));
  171. int j = 0;
  172. for(int i = numberOfSteps-2; i>=0; i--){
  173. xn.push_back((r.at(i)-u.at(i)*xn.at(j++))/n.at(i));
  174. }
  175. std::reverse(xn.begin(), xn.end());
  176. }
  177. double conventionalDiscretization(){
  178. clearVectors();
  179. l.push_back(0.0);
  180. d.push_back(1.0);
  181. u.push_back(0.0);
  182. b.push_back(0.0);
  183. for(int i = 1; i<numberOfSteps-1; i++){
  184. l.push_back(1.0/(h*h));
  185. d.push_back(4.0-2.0/(h*h));
  186. u.push_back(1.0/(h*h));
  187. b.push_back(-tan(h*i));
  188. }
  189. l.push_back(0.0);
  190. d.push_back(1.0);
  191. u.push_back(0.0);
  192. b.push_back(0.5);
  193. thomasAlgorithm();
  194. return maxError();
  195. }
  196.  
  197. double numerovDiscretization(){
  198. clearVectors();
  199. l.push_back(0.0);
  200. d.push_back(1.0);
  201. u.push_back(0.0);
  202. b.push_back(0.0);
  203. for(int i = 1; i<numberOfSteps-1; i++){
  204. l.push_back(4.0/12.0 + 1.0/(h*h));
  205. d.push_back(40.0/12.0 - 2.0/(h*h));
  206. u.push_back(4.0/12.0 + 1.0/(h*h));
  207. b.push_back(-(tan(h*(i-1))/12.0 + (10.0*tan(h*i))/12.0 + tan(h*(i+1))/12.0));
  208. }
  209. l.push_back(0.0);
  210. d.push_back(1.0);
  211. u.push_back(0.0);
  212. b.push_back(0.5);
  213. thomasAlgorithm();
  214. return maxError();
  215. }
  216.  
  217. double getH(){
  218. return h;
  219. }
  220. void setNumberOfSteps(int numberOfSteps){
  221. this->numberOfSteps = numberOfSteps;
  222. h = fabs(x1-x2)/(numberOfSteps-1);
  223. }
  224. double getNumberOfSteps(){
  225. return numberOfSteps;
  226. }
  227. };
  228.  
  229. int main(){
  230. int steps = 10;
  231. Discretization normal(steps);
  232. std::ofstream file;
  233. file.open("Data.dat");
  234. if(!file.is_open()){
  235. std::cerr<<"File wasn't open!";
  236. exit(0);
  237. }
  238. while(normal.getNumberOfSteps()<10000){
  239. file<<normal.getH()<<" "<<normal.conventionalDiscretization()<<" "<<normal.numerovDiscretization()<<"\n";
  240. steps+=100;
  241. normal.setNumberOfSteps(steps);
  242. }
  243. normal.numerovDiscretization();
  244. file.close();
  245. return 0;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement