Advertisement
Guest User

Untitled

a guest
May 26th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.02 KB | None | 0 0
  1. #include "stdio.h"
  2. #include <stdlib.h>
  3. #include "math.h"
  4. #include "main.h"
  5.  
  6. double V_nplus1_m(double v_n_mplus1, double v_n_m){
  7. return (tao/(2*h))*(v_n_mplus1 - v_n_m) + v_n_m;
  8. }
  9.  
  10. double V_nplus1_m_nelin(double v_n_mplus1, double v_n_m){
  11. // if (t_local > 0.995)
  12. //{ printf(" \n res2: %.25lf \n v_n_m: %.25lf \n res %.25lf \n \n",(tao*0.5)*(pow(v_n_mplus1, 2) - pow(v_n_m, 2))/h, v_n_m, (tao/(2*h))*(v_n_mplus1*v_n_mplus1 - v_n_m*v_n_m) + v_n_m);};
  13. return ((tao*0.5)*(pow(v_n_mplus1, 2) - pow(v_n_m, 2))/h + v_n_m);
  14. }
  15.  
  16.  
  17. double proverka(double value){
  18. if (fabs(value)<1e-15) value = 0.0;
  19. return value;
  20. }
  21.  
  22. int stepx_by_x(double x){
  23. return (int)(fabs(x - x_begin)/h);
  24. }
  25.  
  26. int stept_by_t(double t){
  27. return (int)(fabs(t - t_begin)/tao);
  28. }
  29.  
  30. double t_by_stept(int step){
  31. return (double)(t_begin + step*tao);
  32. }
  33.  
  34. double x_by_stepx(int step){
  35.  
  36. return (double)((x_begin + step*h));
  37. }
  38.  
  39. void print_block(double * block, int length){
  40. int i;
  41. for (i = 0; i < length; ++i)
  42. {
  43. printf("%-4.5g %g \n", block[i], x_by_stepx(i));
  44. }
  45. printf("\n");
  46. }
  47.  
  48.  
  49. double delta(double v_n_m, double x, double t){
  50. double delta;
  51. /*if (x<=-0.5*t){
  52. delta = fabs(v_n_m - 1);
  53. // printf("delta %g\n", delta);
  54. }
  55. else{
  56. delta = fabs(v_n_m);
  57. // printf("delta %g\n", delta);
  58. }*/
  59.  
  60. /*if (x<=-0.5*t){
  61. delta = fabs(v_n_m);
  62. // printf("delta %g\n", delta);
  63. }
  64. else if ((x>-0.5*t)&&(x<=-0.5*t+0.25)) {
  65. delta = fabs(v_n_m - 4*x-2*t);
  66. // printf("delta %g\n", delta);
  67. }
  68. else {delta = fabs(v_n_m-1);}
  69. return delta;*/
  70.  
  71. if (x<=-0.5*t){
  72. delta = fabs(v_n_m);
  73. // printf("delta %g\n", delta);
  74. }
  75. else if ((x>-0.5*t)&&(x<=-0.5*t+0.25)) {
  76. delta = fabs(v_n_m - 4*x-2*t);
  77. // printf("delta %g\n", delta);
  78. }
  79. else {delta = fabs(v_n_m-1);}
  80. return delta;
  81. }
  82.  
  83. double delta_nel(double v_n_m, double x, double t){
  84. double delta;
  85. if (x<=-t){
  86. delta = fabs(v_n_m - 1);
  87. // printf("delta %g\n", delta);
  88. }
  89. else if((x<=0)&&(x>-t)){
  90. delta = fabs(v_n_m+x/t);
  91. // printf("delta %g\n", delta);
  92. }
  93. else {delta = fabs(v_n_m);};
  94.  
  95. /*if ((t<=0.25)&&(x<=0)){
  96. delta = fabs(v_n_m);
  97. // printf("delta %g\n", delta);
  98. }
  99. else if((t<=0.25)&&(x>0)&&(x<=-t+0.25)){
  100. delta = fabs(v_n_m-(4.0*x/(1-4.0*t)));
  101. // printf("delta %g\n", delta);
  102. }
  103. else if ((t<=0.25)&&(x>-t+0.25)){delta = fabs(v_n_m - 1);}
  104. else if ((t>0.25)&&(fabs(x +(0.5*(t-0.25)))<0.00000001)) {delta = fabs(v_n_m -(4.0*x/(1-4.0*t)) );}
  105. else if ((t>0.25)&&(x<-0.5*(t-0.25))){delta = fabs(v_n_m);}
  106.  
  107. else if ((t>0.25)&&(x>-0.5*(t-0.25))) {delta = fabs (v_n_m - 1); };*/
  108. return delta;
  109. }
  110.  
  111.  
  112. void norma_c(double delta, double *max){
  113. if (fabs(delta) > *max) *max = fabs(delta);
  114. }
  115.  
  116.  
  117. void norma_l(double v_n_m, double * norma){
  118. *norma += fabs(v_n_m);
  119. }
  120.  
  121.  
  122. void print_matrix(double * matrix, int length){
  123. printf("\nMATRIX PRINT\n");
  124. int i,j;
  125. for (i = 0; i < length; i++)
  126. for (j = 0; j < length; j++){
  127. printf("%-4.5g ", *(matrix + i*length + j));
  128. }
  129. printf("\n");
  130. }
  131.  
  132.  
  133. void print_block_to_file(double * block, int length, FILE * fout){
  134. int i;
  135. for (i = 0; i < length; ++i)
  136. {
  137. fprintf(fout, "%-8g ", block[i]);
  138. }
  139. fprintf(fout, "\n");
  140. }
  141.  
  142. void begin_value(double * block, int length){
  143. int i;
  144. for (i = 0; i < length; ++i)
  145. {
  146. if (x_by_stepx(i) <= 0)
  147. block[i] = 1.0;
  148. else
  149. block[i] = 0.0;
  150. }
  151. /*for (i = 0; i < length; ++i)
  152. {
  153.  
  154.  
  155. if (x_by_stepx(i) <= 0)
  156. block[i] = 0.0;
  157. else if((x_by_stepx(i)>0)&&(x_by_stepx(i)<=0.25))
  158. {block[i] = 4.0*x_by_stepx(i);}
  159. else {block[i] = 1.0;}*/
  160. //printf("BLOCK[%g] = %g\n", x_by_stepx(i), block[i]);
  161.  
  162.  
  163. }
  164.  
  165. void characteristic(double * block){
  166. block[0] = 1.0;
  167. block[m-1] = 0.0;
  168.  
  169. //block[0] = 0.0;
  170. //block[m-1] = 1.0;
  171. }
  172.  
  173. void characteristic_for_b(double * b){
  174. /*b[0] = 1-tao/(4*h);
  175. b[m-3] = 0.0;*/
  176. b[0] = 0.0;
  177. b[m-3] = 1+tao/(4*h);
  178. }
  179.  
  180. void next_block(double * block, int length, double step_by_t,//явный случай
  181. double * norma_DELTA_c_line, double * norma_delta_c_line, //[c]
  182. double * norma_DELTA_l_line, double * norma_delta_l_line //[l]
  183. ){
  184. int i;
  185. double x = x_begin;
  186. double del;
  187. *norma_DELTA_c_line = 0; *norma_delta_c_line = 0; //начальное максимальное значение [c]
  188. *norma_DELTA_l_line = 0; *norma_delta_l_line = 0; //начальное значение [l]
  189.  
  190. characteristic(block);
  191. for (i = 0; i < m-1; ++i)
  192. {
  193. block[i] = V_nplus1_m(block[i+1], block[i]);
  194. //block[i] = proverka(block[i]);
  195. del = delta(block[i], x, (step_by_t+1)*tao); // разность между численным решением и точным решением
  196. norma_c(del, norma_DELTA_c_line); //[c]
  197. norma_c(block[i], norma_delta_c_line); //[c]
  198. norma_l(del, norma_DELTA_l_line); //[l]
  199. norma_l(block[i], norma_delta_l_line);
  200. // printf("%g %g \n", del, x);
  201. x+=h;
  202. //if (del >= 1) { printf("t: %g\n ", tao*(step_by_t+1));};
  203. } //printf("----------------- \n");
  204.  
  205. // printf("NORMA 2 %g\n", *norma_delta_c_line);
  206.  
  207. }
  208.  
  209. void next_block_nelin(double * block, int length, double step_by_t,//явный случай
  210. double * norma_DELTA_c_line, double * norma_delta_c_line, //[c]
  211. double * norma_DELTA_l_line, double * norma_delta_l_line //[l]
  212. ){
  213. int i;
  214. double x = x_begin;
  215. double del;
  216. *norma_DELTA_c_line = 0; *norma_delta_c_line = 0; //начальное максимальное значение [c]
  217. *norma_DELTA_l_line = 0; *norma_delta_l_line = 0; //начальное значение [l]
  218.  
  219. characteristic(block);
  220. for (i = 0; i<m-1 ; ++i)
  221. {
  222. block[i] = V_nplus1_m_nelin(block[i+1], block[i]);
  223. //block[i] = proverka(block[i]);
  224. del = delta_nel(block[i], x, (step_by_t+1)*tao); // разность между численным решением и точным решением
  225. norma_c(del, norma_DELTA_c_line); //[c]
  226. norma_c(block[i], norma_delta_c_line); //[c]
  227. norma_l(del, norma_DELTA_l_line); //[l]
  228. norma_l(block[i], norma_delta_l_line);
  229. printf("%.15lf %g \n", block[i], x);
  230. //if (del >= 1) { printf("t: %g\n ", tao*(step_by_t+1));};
  231. x+=h;
  232. //x = proverka(x);
  233. }
  234. printf("------------------");
  235. // printf("NORMA 2 %g\n", *norma_delta_c_line);
  236.  
  237. }
  238.  
  239. void matrix_b(double * b){
  240. int i;
  241. characteristic_for_b(b);
  242. //printf("b[%d]: %f\n", 0,b[0]);
  243. for (i = 1; i < m-1; i++)
  244. { if (x_by_stepx(i) <= 0)
  245. b[i] = 0.0;
  246. else if((x_by_stepx(i)>0)&&(x_by_stepx(i)<=0.25))
  247. {b[i] = 4*x_by_stepx(i);}
  248. else { b[i] = 1.0;}
  249. /*if (x_by_stepx(i) <= 0)
  250. b[i] = 1.0;
  251. else
  252. b[i] = 0.0;*/
  253. //printf("b[%d]: %f\n", i,b[i]);
  254. }
  255. //printf("b[%d]: %f\n", m-1,b[m-1]);
  256.  
  257.  
  258.  
  259.  
  260. }
  261.  
  262.  
  263.  
  264. void matrix_b1(double * b){
  265. int i;
  266. characteristic_for_b(b);
  267. for (i = 1; i < m-2; i++)
  268. {
  269. if ((x_by_stepx(i+2)==0)&&(x_by_stepx(i+1)!=0)){ // ОБЯЗАТЕЛЬНОЕ ПОПАДАНИЕ В 0
  270. b[i] = tao/8*(4*x_by_stepx(i+2));
  271. }
  272.  
  273. else if ((x_by_stepx(i+1)==0)&&(x_by_stepx(i)!=0)){
  274. b[i] = tao/8*(4*x_by_stepx(i+2)-16*x_by_stepx(i+1));
  275. }
  276.  
  277. else if ((x_by_stepx(i)==0)&&(x_by_stepx(i-1)!=0)){
  278. b[i] = 4*x_by_stepx(i)+tao/8*(4*x_by_stepx(i+2)-16*x_by_stepx(i+1)+24*x_by_stepx(i));
  279. }
  280.  
  281. else if ((x_by_stepx(i-1)==0)&&(x_by_stepx(i-2)!=0)){
  282. b[i] = 4*x_by_stepx(i+1)+tao/8*(4*x_by_stepx(i+2)-16*x_by_stepx(i+1)+24*x_by_stepx(i)-16*x_by_stepx(i-1));
  283. }
  284.  
  285. else if ((x_by_stepx(i-2)>0)&&(x_by_stepx(i+2)<=0.25)){
  286. b[i] = 4*x_by_stepx(i);
  287. }
  288.  
  289. else if ((x_by_stepx(i+2)>0.25)&&(x_by_stepx(i+1)<=0.25)){ // ОБЯЗАТЕЛЬНОЕ ПОПАДАНИЕ В 0
  290. b[i] = 4*x_by_stepx(i)+tao/8*(1-16*x_by_stepx(i+1)+24*x_by_stepx(i)-16*x_by_stepx(i-1)+4*x_by_stepx(i-2));
  291. }
  292.  
  293. else if ((x_by_stepx(i+1)>0.25)&&(x_by_stepx(i)<=0.25)){
  294. b[i] = 4*x_by_stepx(i)+tao/8*(-3+24*x_by_stepx(i)-16*x_by_stepx(i-1)+4*x_by_stepx(i-2));
  295. }
  296.  
  297. else if ((x_by_stepx(i)>0.25)&&(x_by_stepx(i-1)<=0.25)){
  298. b[i] = 1+tao/8*(3-16*x_by_stepx(i-1)+4*x_by_stepx(i-2));
  299. }
  300.  
  301. else if ((x_by_stepx(i-1)>0.25)&&(x_by_stepx(i-2)<=0.25)){
  302. b[i] = 1+tao/8*(-1+4*x_by_stepx(i-2));
  303. }
  304.  
  305. else if (x_by_stepx(i-2) > 0.25){
  306. b[i] = 1.0;
  307. }
  308. else
  309. b[i] = 0.0;
  310. }
  311. }
  312.  
  313.  
  314. void matrix_U( double U_1[m-2], double U_2[m-3], double b[m-2]){
  315. int i,j;
  316. double l=0; // начальное l
  317. for (i=0; i<m-2; i++){
  318. if (i==0) { //вырожденный случай
  319. U_1[i] = 1;
  320. U_2[i] = -tao/(4*h);
  321. b[i] = b[i];
  322. l = (tao/(4*h))/U_1[i];
  323. } else if (i!=m-3) {
  324. U_1[i] = 1 - l*U_2[i-1];
  325. U_2[i] = -tao/(4*h);
  326. b[i] = b[i] - l*b[i-1];
  327. l = (tao/(4*h))/U_1[i];
  328. } else {
  329. U_1[i] = 1 - l*U_2[i-1];
  330. b[i] = b[i] - l*b[i-1];
  331. }
  332.  
  333. }
  334. }
  335.  
  336. void matrix_V_hat(double U_1[m-2], double U_2[m-3], double b[m-2], double V_hat[m], double step_by_t,//неявный случай
  337. double * norma_DELTA_c_line, double * norma_delta_c_line, //[c]
  338. double * norma_DELTA_l_line, double * norma_delta_l_line){
  339. int i;
  340. double del;
  341. *norma_DELTA_c_line = 0; *norma_delta_c_line = 0; //начальное максимальное значение [c]
  342. *norma_DELTA_l_line = 0; *norma_delta_l_line = 0;
  343.  
  344.  
  345. characteristic(V_hat);
  346. //printf("V_hat[%d]: %g\n", m-1,V_hat[m-1]);
  347. for(i=m-2; i>=1; i--){
  348. if (i==m-2) V_hat[i] = b[i-1]/U_1[i-1];
  349. else {
  350. V_hat[i] = (b[i-1] - U_2[i-1]*V_hat[i+1])/U_1[i-1];
  351. V_hat[i] = proverka(V_hat[i]);
  352. }
  353.  
  354. del = delta(V_hat[i], x_by_stepx(i), (step_by_t+1)*tao); // разность между численным решением и точным решением
  355. //printf("del: %g\n", del);
  356. norma_c(del, norma_DELTA_c_line); //[c]
  357. norma_c(V_hat[i], norma_delta_c_line); //[c]
  358. norma_l(del, norma_DELTA_l_line); //[l]
  359. norma_l(V_hat[i], norma_delta_l_line);
  360. //printf("NORMA_DELTA_c_line %g\n", *norma_DELTA_c_line);
  361. //printf("%.10f %g\n",del, x_by_stepx(i));
  362. } //printf("--------------- \n");
  363.  
  364. }
  365.  
  366. void equal(double b[m-2], double V_hat[m]){
  367. int i;
  368. b[m-3] = V_hat[m-2]+tao/(4*h);
  369. b[m-3] = proverka(b[m-3]);
  370. for(i=0; i<m-3; i++){
  371. b[i] = V_hat[i+1];
  372. }
  373. }
  374.  
  375.  
  376.  
  377. void equal1(double b[m-2], double V_hat[m]){
  378. int i;
  379. b[0] = V_hat[1] +(tao/8.)*(V_hat[3] - 4*V_hat[2] + 6*V_hat[1] ) ;
  380. b[1] = V_hat[2] + (tao/8.)*(V_hat[4] - 4*V_hat[3] + 6*V_hat[2] - 4.0*V_hat[1] );
  381. for(i=2; i<m-4; i++){
  382. b[i] = V_hat[i+1] +(tao/8.)*(V_hat[i+3] - 4*V_hat[i+2] + 6*V_hat[i+1] - 4*V_hat[i] + V_hat[i-1]);
  383. }
  384. b[m-4] = V_hat[m-3] + (tao/8.)*(V_hat[m-5] - 4*V_hat[m-4] + 6*V_hat[m-3] - 4*V_hat[m-2]+1);
  385. b[m-3] = V_hat[m-2] + (tao/8.)*(V_hat[m-4] - 4*V_hat[m-3] + 6*V_hat[m-2]-3)+ tao/(4*h);
  386. }
  387.  
  388.  
  389. void main_loop_lin_simple(){
  390.  
  391. int i;
  392. double block[m];
  393. //по всей сетке [c]
  394. double norma_DELTA_c_line; double norma_delta_c_line; //по линиям
  395. //по всей сетке [l]
  396. double norma_DELTA_l_line; double norma_delta_l_line; //по линиям
  397. //FILE * fout = fopen("output_1.txt", "w+");
  398.  
  399. int length = (int)(sizeof(block)/sizeof(double));
  400. begin_value(block, length);
  401.  
  402. for (i = 0; i < n; ++i){
  403. next_block(block, length, i, &norma_DELTA_c_line, &norma_delta_c_line, &norma_DELTA_l_line, &norma_delta_l_line);
  404. /*norma_c(norma_DELTA_c_line, &norma_DELTA_c);
  405. norma_c(norma_delta_c_line, &norma_delta_c);
  406. norma_l(norma_DELTA_l_line, &norma_DELTA_l);
  407. norma_l(norma_delta_l_line, &norma_delta_l);*/
  408.  
  409. // print_block_to_file(block, length, fout);
  410.  
  411. }
  412.  
  413. printf("NORMA_DELTA_c %g\n", norma_DELTA_c_line);
  414. printf("NORMA_delta_c %g\n", norma_DELTA_c_line/norma_delta_c_line);
  415. printf("NORMA_DELTA_l %g\n", h*norma_DELTA_l_line);
  416. printf("NORMA_delta_l %g\n", norma_DELTA_l_line/norma_delta_l_line);
  417.  
  418. printf("tao h DeltaC DeltaL deltaC delta l\n");
  419. printf("%g & %g & %e & %e & %e & %e ", tao, h, norma_DELTA_c_line, h*norma_DELTA_l_line, norma_DELTA_c_line/norma_delta_c_line, norma_DELTA_l_line/norma_delta_l_line);
  420.  
  421. }
  422.  
  423. void main_loop_lin_nelin(){
  424. // double previous_block[m];
  425. int i;
  426. double block[m];
  427. //по всей сетке [c]
  428. double norma_DELTA_c_line; double norma_delta_c_line; //по линиям
  429. //по всей сетке [l]
  430. double norma_DELTA_l_line; double norma_delta_l_line; //по линиям
  431. FILE * fout = fopen("output_2.txt", "w+");
  432.  
  433. int length = (int)(sizeof(block)/sizeof(double));
  434. begin_value(block, length);
  435.  
  436. for (i = 0; i < n; ++i){
  437. next_block_nelin(block, length, i, &norma_DELTA_c_line, &norma_delta_c_line, &norma_DELTA_l_line, &norma_delta_l_line);
  438.  
  439.  
  440. print_block_to_file(block, length, fout);
  441.  
  442. }
  443.  
  444. printf("NORMA_DELTA_c %g\n", norma_DELTA_c_line);
  445. printf("NORMA_delta_c %g\n", norma_DELTA_c_line/norma_delta_c_line);
  446. printf("NORMA_DELTA_l %g\n", h*norma_DELTA_l_line);
  447. printf("NORMA_delta_l %g\n", norma_DELTA_l_line/norma_delta_l_line);
  448.  
  449. printf("tao h DeltaC DeltaL deltaC delta l\n");
  450. printf("%g & %g & %e & %e & %e & %e ", tao, h, norma_DELTA_c_line, h*norma_DELTA_l_line, norma_DELTA_c_line/norma_delta_c_line, norma_DELTA_l_line/norma_delta_l_line);
  451.  
  452. }
  453.  
  454. //########################НЕЛИНЕЙНЫЙ СЛУЧАЙ###############################
  455. void begin_value_pristrel(double * block, int length){
  456. int i;
  457. for (i = 0; i < length; ++i){
  458. block[i] = 0.1;
  459. }
  460. }
  461.  
  462.  
  463. void matrix_J( double U_1[m-2], double U_2[m-3], double V_pr[m], double G_2[m-2]){
  464. int i,j;
  465. double l = 0; // íà÷àëüíîå l
  466. double a = 2*tao/(4*h);
  467.  
  468. for (i=0; i<m-2; i++){
  469. if (i==0) { //âûðîæäåííûé ñëó÷àé
  470. U_1[i] = 1;
  471. U_2[i] = -a*V_pr[2];
  472. G_2[i] = G_2[i];
  473. l = a*V_pr[1]/U_1[i]; // ????????
  474. } else if (i!=m-3) {
  475. U_1[i] = 1 - l*U_2[i-1];
  476. U_2[i] = -a*V_pr[i+2];
  477. G_2[i] = G_2[i] - l*G_2[i-1];
  478. l = a*V_pr[i+1]/U_1[i]; // ??????????
  479. } else {
  480. U_1[i] = 1 - l*U_2[i-1];
  481. G_2[i] = G_2[i] - l*G_2[i-1];
  482. }
  483. }
  484.  
  485.  
  486.  
  487. for (i = 0; i < m-3; ++i){
  488. //printf("U_1[%-2d]: %-13g, U_2[%-2d]: %-13g \n", i, U_1[i], i, U_2[i]);
  489. }
  490. }
  491.  
  492. void matrix_G(double V_pr[m], double G[m-2], double V[m]){
  493. int i;
  494. double a = tao/(4.0*h);
  495.  
  496. for (i=0; i<m-2; i++){
  497. if (i==0) {
  498. G[0] = -(V_pr[1] - V[1] - a*(pow(V_pr[2],2) - 1));
  499. } else if (i==m-3) {
  500. G[m-3] = -(V_pr[m-2] - V[m-2] - a*(0 - pow(V_pr[m-3],2)));
  501. } else {G[i] = -(V_pr[i+1] - V[i+1] - a*(pow(V_pr[i+2],2) - pow(V_pr[i],2)));
  502.  
  503. }
  504.  
  505. }
  506. }
  507.  
  508. void matrix_G1(double V_pr[m], double G[m-2], double V[m]){
  509. int i;
  510. double a = tao/(4*h);
  511.  
  512. for (i=0; i<m-2; i++){
  513. if (i==0) {
  514. G[0] = -(V_pr[1] - V[1] - a*(V_pr[2]*V_pr[2] - 1) -tao*0.125*(V[i+3]-4*V[i+2]+6*V[i]));
  515. } else if (i == 1)
  516. {
  517. G[1] = -(V_pr[2] - V[2] - a*(V_pr[3]*V_pr[3] - V_pr[1]*V_pr[1]) -tao*0.125*(V[i+3]-4*V[i+2]+6*V[i]-4*V[i-1]));
  518. }
  519. else if ((i!=m-3)&&(i!=m-4) ){
  520. G[i] = -(V_pr[i+1] - V[i+1] - a*(V_pr[i+2]*V_pr[i+2] - V_pr[i]*V_pr[i]) - tao*0.125*(V[i+3]-4*V[i+2]+6*V[i]-4*V[i-1]+V[i-2]));
  521. }
  522. else if (i== m-4)
  523. {G[i] = -(V_pr[i+1] - V[i+1] - a*(V_pr[i+2]*V_pr[i+2] - V_pr[i]*V_pr[i])-tao*0.125*(1-4*V[i+2]+6*V[i]-4*V[i-1]+V[i-2]));
  524. }
  525.  
  526. else { //i = m-3
  527. G[i-3] = -(V_pr[i-2] - V[i-2] - a*(0 - V_pr[i-3]*V_pr[i-3])- tao*0.125*(1-4+6*V[i]-4*V[i-1]+V[i-2]));
  528. }
  529.  
  530. }
  531. }
  532.  
  533.  
  534. void new_vector_X(double U_1[m-2], double U_2[m-3], double G_2[m-2], double X[m-2]){
  535.  
  536. int i;
  537. double del;
  538.  
  539. for(i=m-3; i>=0; i--){
  540. if (i == m-3) {
  541. X[i] = G_2[i]/U_1[i];
  542. }
  543. else {
  544. X[i] = (G_2[i] - U_2[i]*X[i+1])/U_1[i];
  545. }
  546. }
  547. }
  548.  
  549. void new_V_pr(double X[m-2], double V_pr[m]){
  550. int i;
  551. for (i = 0; i < m-2; ++i){
  552. V_pr[i+1] = V_pr[i+1] + X[i];
  553. }
  554. }
  555.  
  556. void equal_G2_G1(double G_1[m-2], double G_2[m-2]){
  557. int i;
  558. for (i = 0; i < m-2; ++i)
  559. {
  560. G_2[i] = G_1[i];
  561. }
  562. }
  563.  
  564. void equal_V_Vpr(double V[m], double V_pr[m]){
  565. int i;
  566. for (i = 0; i < m-2; ++i)
  567. {
  568. V[i] = V_pr[i];
  569. }
  570. }
  571.  
  572. double norma_G(double G[m-2]){
  573. double norma = 0;
  574. int i;
  575. for (i = 0; i < m-2; ++i){
  576. norma += fabs(G[i]);
  577. }
  578.  
  579. return norma*h;
  580. }
  581.  
  582. double norma_G_2(double G[m-2]){
  583. double norma = 0;
  584. int i;
  585. for (i = 0; i < m-2; ++i){
  586. norma += G[i]*G[i];
  587. }
  588. norma = sqrt(norma);
  589.  
  590. return norma;
  591. }
  592.  
  593.  
  594.  
  595. int main(){
  596. printf("Size: %d x %d\n", m,n);
  597.  
  598. // main_loop_lin_simple();
  599. //main_loop_lin_nelin();
  600. //return 0;
  601. int i;
  602. //double A[m][m];
  603. double U_1[m-2]; double U_2[m-3]; double b[m-2]; double V_hat[m];
  604. double V_pr[m]; double V_0[m]; double G_1[m-2]; double G_2[m-2]; double V[m]; double X[m-2]; double norma_for_G = 1; //чтобы зашло
  605.  
  606. int length = (int)(sizeof(V_hat)/sizeof(double));
  607.  
  608. //по всей сетке [c]
  609. double norma_DELTA_c_line; double norma_delta_c_line; //по линиям
  610. //по всей сетке [l]
  611. double norma_DELTA_l_line; double norma_delta_l_line; //по линиям
  612. FILE * fout = fopen("output_2.txt", "w+");
  613. FILE * fout_once = fopen("output_once.txt", "w");
  614. //##########################################
  615.  
  616.  
  617. i = 0;
  618.  
  619.  
  620. begin_value(V, length);
  621. equal_V_Vpr(&V_0, &V); // V === begin_value
  622.  
  623. for (i = 0; i < n; ++i)
  624. {
  625.  
  626. //begin_value_pristrel(V_pr, length); // V_pr === begin_value
  627. equal_V_Vpr(&V_pr, &V_0);
  628. V_pr[m-1] = 0;
  629. V_pr[0]=1;
  630.  
  631. while (fabs(norma_for_G) > 0.0000001) {
  632.  
  633. //printf("%g - NORMAAAAAAAAAAAAAAAAAAAAAAAAAA GGGGGGGGGGGGG\n\n", norma_for_G);
  634.  
  635. matrix_G(&V_pr, &G_1, &V);
  636. //print_block(G_1, length);
  637.  
  638. equal_G2_G1(&G_1, &G_2);
  639.  
  640. matrix_J(&U_1, &U_2, &V_pr, &G_2);
  641. /*printf("\n\n");
  642. print_block(U_1, m-2);
  643. printf("\n\n");
  644. print_block(U_2, m-3);
  645. printf("\n\n");*/
  646.  
  647. new_vector_X(&U_1, &U_2, &G_2, &X);
  648.  
  649. /*print_block(X, m-2);
  650. printf("\n\n");*/
  651.  
  652. new_V_pr(&X, &V_pr);
  653. //printf("XXXXXXX\n\n");
  654. //if (t_by_stept(i+1)>0.45) print_block(X, m-2);
  655.  
  656. matrix_G(&V_pr, &G_1, &V); // изменение G для нового вектора пристреленного
  657.  
  658. //if (t_by_stept(i+1)>0.45) printf("norma_g: %g \n", norma_for_G);
  659. norma_for_G = norma_G(&G_1);
  660. }
  661. printf("V_pr\n\n");
  662. print_block(V_pr, m);
  663. norma_for_G = 1;
  664. equal_V_Vpr(&V_0,&V);
  665. equal_V_Vpr(&V, &V_pr);
  666. printf("t %g\n", t_by_stept(i+1));
  667. //if(t_by_stept(i+1)>0.25) break;
  668. }
  669.  
  670. return 0;
  671. //##############################################################
  672.  
  673. i = 0;
  674. matrix_b1(&b);
  675. for(i=0; i<n; i++){
  676. //printf("\nTao: %f, Step %d\n\n", t_by_stept(i+1), i);
  677.  
  678. matrix_U( &U_1, &U_2, &b);
  679. matrix_V_hat(&U_1, &U_2, &b, &V_hat, i, &norma_DELTA_c_line, &norma_delta_c_line, &norma_DELTA_l_line, &norma_delta_l_line);
  680. equal1(&b, &V_hat);
  681. //printf("NORMA_DELTA_c_line %g\n", norma_DELTA_c_line);
  682. print_block_to_file(V_hat, length, fout);
  683.  
  684. //printf("NORMA_DELTA_c %g\n", norma_DELTA_c);
  685.  
  686. }
  687. //printf("NORMA_DELTA_c %g\n", norma_DELTA_c);
  688. //printf("NORMA_delta_c %g\n", norma_DELTA_c/norma_delta_c);
  689. //printf("NORMA_DELTA_l %g\n", h*norma_DELTA_l);
  690. printf("tao h DeltaC DeltaL deltaC delta l\n");
  691. printf("%g & %g & %.15lf & %e & %e & %e ", tao, h, norma_DELTA_c_line, h*norma_DELTA_l_line, norma_DELTA_c_line/norma_delta_c_line, norma_DELTA_l_line/norma_delta_l_line);
  692.  
  693. return 0;
  694. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement