Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <vector>
  4.  
  5. #include <cmath>
  6.  
  7. #include <fstream>
  8.  
  9. using namespace std;
  10.  
  11. vector<double> operator+ (vector<double>, vector<double>);
  12.  
  13. //vector<double> operator- (vector<double>, vector<double>);
  14.  
  15. vector<double> operator+= (vector<double> &, vector<double>);
  16.  
  17. vector<double> operator* (vector<double>, double);
  18.  
  19. ostream& operator« (ostream&, const vector<double>&);
  20.  
  21. vector<double> operator+ (vector<double> v, vector<double> v1)
  22.  
  23. {
  24.  
  25. return v += v1;
  26.  
  27. }
  28.  
  29. vector<double> operator- (vector<double> &v, vector<double> v1)
  30.  
  31. {
  32.  
  33. for(vector<double>::iterator iter = v.begin(), i = v1.begin(); iter != v.end(); iter++, i++)
  34.  
  35. {
  36.  
  37. (*iter) - (*i);
  38.  
  39. }
  40.  
  41. return v;
  42.  
  43. }
  44.  
  45. vector<double> operator+= (vector<double> &v, vector<double> v1)
  46.  
  47. {
  48.  
  49. for(vector<double>::iterator iter = v.begin(), i = v1.begin(); iter != v.end(); iter++, i++)
  50.  
  51. {
  52.  
  53. (*iter) += (*i);
  54.  
  55. }
  56.  
  57. return v;
  58.  
  59. }
  60.  
  61. vector<double> operator* (vector<double> v, double d)
  62.  
  63. {
  64.  
  65. for(vector<double>::iterator iter = v.begin(); iter < v.end(); iter++) *iter *= d;
  66.  
  67. return v;
  68.  
  69. }
  70.  
  71. ostream& operator« (ostream& os, const vector<double>& v)
  72.  
  73. {
  74.  
  75. os « "(";
  76.  
  77. for (vector<double>::const_iterator iter = v.begin(); iter != v.end() - 1; iter++)
  78.  
  79. {
  80.  
  81. os « *iter « "; ";
  82.  
  83. }
  84.  
  85. os « *(v.end() - 1) « ")";
  86.  
  87. return os;
  88.  
  89. }
  90.  
  91. vector<double> f(double t, vector<double> vect)
  92.  
  93. {
  94.  
  95. vector<double> vec(3);
  96.  
  97. vec[0] = vect[0]*2.0 + vect[1];
  98.  
  99. vec[1] = vect[0]+vect[1]*3.0-vect[2];
  100.  
  101. vec[2]=vect[1]*2.0+vect[2]*3.0-vect[0];
  102.  
  103. return vec;
  104.  
  105. }
  106.  
  107. void RungeCutt(vector<double> t, double h, int n, vector<vector<double> >& vect)
  108.  
  109. {
  110.  
  111. vector<vector<double> > K(4, vector<double>(3));
  112.  
  113. vector<vector<double> > temp(4, vector<double>(3));
  114.  
  115. for(int i = 0; i < n; i ++)
  116.  
  117. {
  118.  
  119. K[0] = f(t[i], vect[i])*h;
  120.  
  121. K[1] = f(t[i] + (h/2.0), vect[i] + K[0]*(1.0/2.0))*h;
  122.  
  123. K[2] = f(t[i] + (1.0/2.0)*h, vect[i] + K[1]*(1.0/2.0))*h;
  124.  
  125. K[3]= f(t[i]+h, vect[i]+K[2])*h;
  126.  
  127. vect[i + 1] = (K[0] + K[1]*2.0+K[2]*2.0+K[3])*(1.0/6.0) + vect[i];
  128.  
  129. }
  130.  
  131. }
  132.  
  133. vector<vector<double> > func(vector<double> t, const vector<double> vect)
  134.  
  135. {
  136.  
  137. int n = t.size();
  138.  
  139. double fs,sc,r,C1, C2,C3;
  140.  
  141. vector<vector<double> > vect1(n, vector<double>(3));
  142.  
  143. vect1[0] = vect;
  144.  
  145. fs = vect[0]+vect[2];
  146.  
  147. sc = vect[0]*3.0-vect[2]-vect[1];
  148. r=vect[0]+vect[1];
  149.  
  150. C1 = sc*exp(t[0]*4.0)*(1.0/2.0);
  151.  
  152. C2 = (fs*(cos(t[0])+sin(t[0]))+vect[1]*(sin(t[0])-cos(t[0])))*exp(t[0]*3.0)*((-1.0)/2.0);
  153.  
  154. C3=(r*(sin(t[0])-cos(t[0]))-vect[2]*(cos(t[0])-sin(t[0])))*exp(t[0]*3.0)*((-1.0)/2.0);
  155. cout«"C1="«C1«endl;
  156. cout«"C2="«C2«endl;
  157. cout«"C3="«C3«endl;
  158.  
  159. for (int i = 1 ; i < n; i++)
  160.  
  161. {
  162.  
  163. vect1[i][0] = C1*exp(t[i]*2.0) + C2*exp(t[i]*3.0)*cos(t[i])+ C3*exp(t[i]*3.0)*sin(t[i]);
  164.  
  165. vect1[i][1] = C2*exp(t[i]*3.0)*(cos(t[i])-sin(t[i]))+ C3*exp(t[i]*3.0)*(sin(t[i])+cos(t[i]));
  166.  
  167. vect1[i][2]=C1*exp(t[i]*2.0)+C2*exp(t[i]*3.0)*(cos(t[i])*2.0+sin(t[i]))+C3*exp(t[i]*3.0)*(sin(t[i])*2.0-cos(t[i]));
  168.  
  169. }
  170.  
  171. return vect1;
  172.  
  173. }
  174.  
  175. int main()
  176.  
  177. {
  178.  
  179. int n;
  180.  
  181. double a, b, h;
  182.  
  183. double Max = 0.0;
  184.  
  185. cout « "a = ";
  186.  
  187. cin » a;
  188.  
  189. cout « "b = ";
  190.  
  191. cin » b;
  192.  
  193. cout « "n = ";
  194.  
  195. cin » n;
  196.  
  197. h = (b - a)/n;
  198.  
  199. cout « "h = " « h « endl;
  200.  
  201. vector<vector<double> > vect(n + 1, vector<double>(3));
  202.  
  203. vector<vector<double> > vect1(n + 1, vector<double>(3));
  204.  
  205. vector<double> t(n + 1);
  206.  
  207. double k = a;
  208.  
  209. for (int i = 0; i <= n ; i++)
  210.  
  211. {
  212.  
  213. t[i] = k;
  214.  
  215. k += h;
  216.  
  217. }
  218.  
  219. cout « "x0 = ";
  220.  
  221. cin » vect[0][0];
  222.  
  223. cout « "y0 = ";
  224.  
  225. cin » vect[0][1];
  226.  
  227. cout«"z0=";
  228.  
  229. cin»vect[0][2];
  230.  
  231. RungeCutt(t, h, n, vect);
  232.  
  233. double p=0.0;
  234.  
  235. vect1 = func(t, vect[0]);
  236.  
  237. for(int i = 0; i <= n; i++)
  238.  
  239. {
  240.  
  241. cout «"t[" « i « "] = " « t[i] « endl;
  242.  
  243. cout « "u(x,y,z) = " « vect[i] « endl;
  244.  
  245. cout « "u1(x,y,z) = " « vect1[i] « endl;
  246.  
  247. if((p = fabs(vect[i][0] - exp(t[i]*2.0))) > Max ) Max = p;
  248.  
  249. if((p = fabs(vect[i][1] - 0 )) > Max) Max = p;
  250.  
  251. if((p=fabs( vect[i][2]-exp(t[i]*2.0)))>Max) Max=p;
  252.  
  253. cout « "u2(x,y,z) = " « exp(t[i]*2.0)«" "«0«" "«exp(t[i]*2.0) « endl;
  254. }
  255.  
  256. cout « "\n(x(0), y(0),z(0)) = " « vect[0] « "\na = " « a « "\nb = " « b « "\nn = " « n « "\nmax eps = " « Max « endl;
  257.  
  258. return 0;
  259.  
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement