fork download
  1. //Котельникова Любовь, 113 группа, курсовая работа
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <fstream>
  6. #include <cfloat>
  7. #include <clocale>
  8. #include <float.h>
  9. using namespace std;
  10. typedef double (*Func)(double*);
  11. class RungeKut
  12. {
  13. private:
  14. int N;
  15. Func* FuncArray;
  16. double *x0;
  17. double *yR; //текущий столбец значений-решений
  18. double t; //независимая переменная, изменяющаяся от а до b
  19. const char* stroka;
  20. public:
  21. RungeKut(int n);
  22. void Init(double *X0, Func* Funcarray, const char* a);
  23. void Start(double T_0, double T_end, double Step);
  24. ~RungeKut();
  25. };
  26. RungeKut::RungeKut(int n)
  27. {
  28. N=n;
  29. x0=new double[N];
  30. yR=new double[N];
  31. }
  32. void RungeKut::Init(double *X0, Func* Funcarray, const char* a)
  33. {
  34. FuncArray=new Func[N];
  35. for(int i=0;i<N;i++) {
  36. FuncArray[i]=Funcarray[i];
  37. x0[i]=X0[i];
  38. }
  39. int len=strlen(a);
  40. stroka=new char[len+1];
  41. stroka=a;
  42. }
  43. void RungeKut::Start(double T_0, double T_end, double Step)
  44. {
  45. ofstream fout(stroka);
  46. t=T_0;
  47. int ii=(int)((T_end-T_0)/Step);
  48. fout<<t;
  49. for (int j=0;j<N;j++) {
  50. fout<<" "<<x0[j];
  51. yR[j]=x0[j];
  52. }
  53. fout<<endl;
  54. for(int k=0;k<ii;k++)
  55. {
  56. double *K0=new double[N];
  57. double *K1=new double[N];
  58. double *K2=new double[N];
  59. double *K3=new double[N];
  60. double *Khelp=new double[N];
  61. for(int i=0; i<N;i++){
  62. K0[i]=Step*FuncArray[i](yR);
  63. Khelp[i]=yR[i]+K0[i]/2;
  64. }
  65. for(int i=0;i<N;i++){
  66. K1[i]=Step*FuncArray[i](Khelp);
  67. }
  68. for(int i=0;i<N;i++){
  69. Khelp[i]=yR[i]+K1[i]/2;
  70. }
  71. for(int i=0;i<N;i++){
  72. K2[i]=Step*FuncArray[i](Khelp);
  73. }
  74. for(int i=0;i<N;i++){
  75. Khelp[i]=yR[i]+K2[i];
  76. }
  77. for(int i=0;i<N;i++){
  78. K3[i]=Step*FuncArray[i](Khelp);
  79. }
  80. for(int i=0;i<N;i++){
  81. yR[i]=yR[i]+(K0[i]+2*K1[i]+2*K2[i]+K3[i])/6;
  82. }
  83. t=t+Step;
  84. fout<<t;
  85. for (int j=0;j<N;j++) {
  86. fout<<" "<<yR[j]; // В файле столбики: t,yR[1],yR[2],...,yR[N]
  87. }
  88. fout<<endl;
  89. delete [] K0;
  90. delete [] K1;
  91. delete [] K2;
  92. delete [] K3;
  93. delete [] Khelp;
  94. }
  95. fout.close();
  96. }
  97. RungeKut::~RungeKut()
  98. {
  99. delete [] x0;
  100. delete [] yR;
  101. delete [] stroka;
  102. delete [] FuncArray;
  103. }
  104. double xxx(double *y)
  105. {
  106. //return 5.*(y[1]-y[0]);
  107. return y[0];
  108. }
  109. double yyy(double *y)
  110. {
  111. //return y[0]*(4.-y[2])-y[1];
  112. return y[1];
  113. }
  114. double zzz(double *y)
  115. {
  116. //return y[0]*y[1]-y[2];
  117. return y[2];
  118. }
  119. int main()
  120. {
  121. int n=3;
  122. double a,b,h;
  123. a=0;
  124. b=100;
  125. h=0.01;
  126. double *X0=new double[n];
  127. /*cout<<"Vvedite stolbets X0 ";
  128. for (int i=0;i<n;i++) {
  129. cin>>X0[i];
  130. }*/
  131. X0[0]=0.1;
  132. X0[1]=0.1;
  133. X0[2]=0.1;
  134. if ((X0[0]==0)&&(X0[1]==0)&&(X0[2]==0)) {
  135. double a=DBL_EPSILON;
  136. X0[0]=X0[1]=X0[2]=a;
  137. }
  138. Func* Funcarray=new Func[n];
  139. Funcarray[0]=xxx;
  140. Funcarray[1]=yyy;
  141. Funcarray[2]=zzz;
  142. RungeKut rk=RungeKut(n);
  143. rk.Init(X0,Funcarray, "ab.txt");
  144. rk.Start(a,b,h);
  145. return 0;
  146. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In member function ‘void RungeKut::Init(double*, double (**)(double*), const char*)’:
prog.cpp:39:21: error: ‘strlen’ was not declared in this scope
     int len=strlen(a);
                     ^
stdout
Standard output is empty