Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdio.h"
- #include <stdlib.h>
- #include "math.h"
- #include "main.h"
- // #include "user.c"
- // #include "no_lin.c"
- // #include "print_f.c"
- double V_nplus1_m(double v_n_mplus1, double v_n_m);
- double V_nplus1_m_nelin(double v_n_mplus1, double v_n_m);
- double proverka(double value);
- int stepx_by_x(double x);
- int stept_by_t(double t);
- double t_by_stept(int step);
- double x_by_stepx(int step);
- void print_block(double * block, int length);
- double delta(double v_n_m, double x, double t);
- double delta_nel(double v_n_m, double x, double t);
- void norma_c(double delta, double * max);
- void norma_l(double v_n_m, double *norma);
- void print_matrix(double * matrix, int length);
- void print_block_to_file(double * block, int length, FILE * fout);
- void begin_value(double *block, int length);
- void characteristic(double * block);
- void characteristic_for_b(double * b);
- void next_block(double * block, double step_by_t,double * norma_DELTA_c_line, double * norma_delta_c_line,double * norma_DELTA_l_line, double * norma_delta_l_line);
- void next_block_nelin(double * block, double step_by_t,double * norma_DELTA_c_line, double * norma_delta_c_line, double * norma_DELTA_l_line, double * norma_delta_l_line);
- void matrix_b(double * b);
- void matrix_b1(double * b);
- void matrix_U( double * U_1, double * U_2, double * b);
- void matrix_V_hat(double * U_1, double * U_2, double * b, double * V_hat, double step_by_t,//неявный случай
- double * norma_DELTA_c_line, double * norma_delta_c_line, //[c]
- double * norma_DELTA_l_line, double * norma_delta_l_line);
- void equall(double * b, double * V_hat);
- void equal1(double * b, double * V_hat);
- void main_loop_lin_simple(void);
- void main_loop_lin_nelin(void);
- void begin_value_pristrel(double * block, int length);
- void matrix_J( double *U_1, double * U_2, double * V_pr, double *G_2);
- void matrix_G(double * V_pr, double * G, double * V);
- void matrix_G1(double * V_pr, double * G, double * V);
- void new_vector_X(double * U_1, double * U_2, double * G_2, double * X);
- void new_V_pr(double * X, double * V_pr);
- void equal_G2_G1(double * G_1, double G_2[m-2]);
- void equal_V_Vpr(double * V, double * V_pr);
- void equal_V_Vpr_print(double * V, double * V_pr, int t);
- double norma_G(double * G);
- double norma_G_2(double * G);
- void print_block__xvaluex(double * block, int length, FILE * fout);
- void equal(double * b, double * V_hat);
- // ----------------------------------------------------------------
- double proverka(double value){
- if (fabs(value) < 1e-100) value = 1e-100;
- return value;
- }
- int stepx_by_x(double x){
- return (int)(fabs(x - x_begin)/h);
- }
- int stept_by_t(double t){
- return (int)(fabs(t - t_begin)/tao);
- }
- double t_by_stept(int step){
- return (double)(t_begin + step*tao);
- }
- double x_by_stepx(int step){
- return (double)(x_begin + step*h);
- }
- // ----------------------------------------------------------------
- double V_nplus1_m(double v_n_mplus1, double v_n_m){
- return (tao/(2*h))*(v_n_mplus1 - v_n_m) + v_n_m;
- }
- double V_nplus1_m_nelin(double v_n_mplus1, double v_n_m){
- return ((tao/(2.0*h))*(v_n_mplus1*v_n_mplus1 - v_n_m*v_n_m) + v_n_m);
- }
- // ----------------------------------------------------------------
- void print_matrix(double * matrix, int length){
- int i,j;
- printf("\nMATRIX PRINT\n");
- for (i = 0; i < length; i++){
- for (j = 0; j < length; j++){
- printf("%-4.1f ", *(matrix + i*length + j));
- }
- printf("\n");
- }
- printf("\n");
- }
- void print_block(double * block, int length){
- int i;
- for (i = 0; i < length; ++i)
- {
- printf("%f %-4.5g \n", x_by_stepx(i), block[i]);
- }
- printf("\n");
- }
- void print_block_to_file(double * block, int length, FILE * fout){
- int i;
- for (i = 0; i < length; ++i)
- {
- fprintf(fout, "%-8g ", block[i]);
- }
- fprintf(fout, "\n");
- }
- void print_block__xvaluex(double * block, int length, FILE * fout){
- int i;
- for (i = 0; i < length; ++i)
- {
- fprintf(fout, "%-8g %-8g \n", x_by_stepx(i), block[i]);
- }
- fprintf(fout, "\n");
- }
- // ----------------------------------------------------------------
- double delta(double v_n_m, double x, double t){
- double delta;
- double eps=1e-6;
- if (x<=-0.5*t){
- delta = fabs(v_n_m - 1);
- // printf("delta %g\n", delta);
- }
- else if ((fabs(x) < 0.5*t + eps)&&(fabs(x) > 0.5*t - eps)){
- delta = fabs(v_n_m - 0.5);
- // printf("delta %g\n", delta);
- }
- else {
- delta = fabs(v_n_m - 0);
- }
- return delta;
- }
- double delta_nel(double v_n_m, double x, double t){
- double delta;
- // printf("[t] %g\n", t);
- if (x<=-t){
- delta = fabs(v_n_m - 1);
- }
- else if((x>-t)&&(x<=0)){
- delta = fabs(v_n_m + x/t);
- }
- else if (x>0) {delta = fabs(v_n_m);
- };
- return delta;
- }
- void norma_c(double delta, double * max){
- if (fabs(delta) > *max) *max = fabs(delta);
- }
- void norma_l(double v_n_m, double * norma){
- *norma += fabs(v_n_m);
- }
- // ----------------------------------------------------------------
- //double delta_izm (double * izm, double * V_pr, M){
- // for
- //}
- // ----------------------------------------------------------------
- double res(double x, double t){
- double res;
- double eps = 1e-6;
- if (x<=-0.5*t){
- res = 1;
- }
- else if ((fabs(x) < 0.5*t + eps)&&(fabs(x) > 0.5*t - eps)){
- res = 0.5;
- }
- else{
- res = 0;
- }
- return res;
- }
- double res_nel( double x, double t){
- double res;
- if (x<=-t){
- res = 1;
- }
- else if((x<=0)&&(x>-t)){
- res = -x/t;
- }
- else {res = 0;};
- return res;
- }
- double res_test(double x, double t){
- return (1-x)/(t+2);
- }
- // ----------------------------------------------------------------
- void begin_value(double * block, int length){
- int i;
- for (i = 0; i < length; ++i)
- {
- if (x_by_stepx(i) <= 0)
- block[i] = 1.0;
- else
- block[i] = 0.0;
- }
- }
- void characteristic(double * block){
- block[0] = 1.0;
- block[m-1] = 0;
- }
- void characteristic_for_b(double * b){
- b[0] = 1.0 - tao/(4.0*h);
- b[m-3] = 0.0;
- }
- void next_block(double * block, double step_by_t,
- /*явный случай*/
- double * norma_DELTA_c_line, double * norma_delta_c_line, //[c]
- double * norma_DELTA_l_line, double * norma_delta_l_line //[l]
- ){
- int i;
- double x = x_begin;
- double del;
- *norma_DELTA_c_line = 0; *norma_delta_c_line = 0; //начальное максимальное значение [c]
- *norma_DELTA_l_line = 0; *norma_delta_l_line = 0; //начальное значение [l]
- characteristic(block);
- for (i = 0; i < m-1; ++i)
- {
- block[i] = V_nplus1_m(block[i+1], block[i]);
- del = delta(block[i], x, (step_by_t+1)*tao); // разность между численным решением и точным решением
- norma_c(del, norma_DELTA_c_line); //[c]
- norma_c(block[i], norma_delta_c_line); //[c]
- norma_l(del, norma_DELTA_l_line); //[l]
- norma_l(block[i], norma_delta_l_line); //[l]
- printf("V: %g x: %g del: %g \n", block[i], x, del);
- x+=h;
- if (del >= 1) { printf("t: %g\n ", tao*(step_by_t+1));};
- }
- // printf("NORMA 1 %g\n", *norma_DELTA_c_line);
- // printf("NORMA 2 %g\n", *norma_delta_c_line);
- }
- void next_block_nelin(double * block, double step_by_t,
- /*нелинейный случай*/
- double * norma_DELTA_c_line, double * norma_delta_c_line, //[c]
- double * norma_DELTA_l_line, double * norma_delta_l_line //[l]
- ){
- int i;
- double x = x_begin;
- double del;
- *norma_DELTA_c_line = 0; *norma_delta_c_line = 0; //начальное максимальное значение [c]
- *norma_DELTA_l_line = 0; *norma_delta_l_line = 0; //начальное значение [l]
- characteristic(block);
- for (i = 0; i < m-1; ++i)
- {
- block[i] = V_nplus1_m_nelin(block[i+1], block[i]);
- del = delta_nel(block[i], x, (step_by_t+1)*tao); // разность между численным решением и точным решением
- norma_c(del, norma_DELTA_c_line); //[c]
- norma_c(block[i], norma_delta_c_line); //[c]
- norma_l(del, norma_DELTA_l_line); //[l]
- norma_l(block[i], norma_delta_l_line); //[l]
- // printf("V: %g x: %g del: %g \n", block[i], x, del);
- printf("%g %g \n", x, del);
- if (del >= 1) { printf("t: %g\n ", tao*(step_by_t+1));};
- x+=h;
- }
- printf("kek\n");
- // printf("NORMA 1 %g\n", *norma_DELTA_c_line);
- // printf("NORMA 2 %g\n", *norma_delta_c_line);
- }
- // ----------------------------------------------------------------
- void matrix_b(double * b){
- int i;
- characteristic_for_b(b);
- for (i = 1; i < m-1; i++)
- {
- if (x_by_stepx(i) <= 0)
- b[i] = 1.0;
- else
- b[i] = 0.0;
- }
- }
- void matrix_b1(double * b){
- /* omega = 1 */
- int i;
- characteristic_for_b(b);
- for (i = 1; i < m-2; i++)
- {
- if ((x_by_stepx(i+2)==0)&&(x_by_stepx(i+1)!=0)){ // ÎÁßÇÀÒÅËÜÍÎÅ ÏÎÏÀÄÀÍÈÅ Â 0
- b[i] = 1 - tao/8;
- } else if ((x_by_stepx(i+1)==0)&&(x_by_stepx(i)!=0)){
- b[i] = 1 + (3*tao)/8;
- } else if ((x_by_stepx(i)==0)&&(x_by_stepx(i-1)!=0)){
- b[i] = -(3*tao)/8;
- } else if ((x_by_stepx(i-1)==0)&&(x_by_stepx(i-2)!=0)){
- b[i] = tao/8;
- } else if (x_by_stepx(i+2) < 0){
- b[i] = 1.0;
- } else
- b[i] = 0.0;
- }
- }
- // ----------------------------------------------------------------
- void matrix_U( double * U_1, double * U_2, double * b){
- int i;
- double l=0; // íà÷àëüíîå l
- for (i=0; i<m-2; i++){
- if (i==0) { //âûðîæäåííûé ñëó÷àé
- U_1[i] = 1;
- U_2[i] = -tao/(4*h);
- b[i] = b[i];
- l = (tao/(4*h))/U_1[i];
- } else if (i!=m-3) {
- U_1[i] = 1 - l*U_2[i-1];
- U_2[i] = -tao/(4*h);
- b[i] = b[i] - l*b[i-1];
- l = (tao/(4*h))/U_1[i];
- } else {
- U_1[i] = 1 - l*U_2[i-1];
- b[i] = b[i] - l*b[i-1];
- }
- }
- }
- void matrix_V_hat(double * U_1, double * U_2, double * b, double * V_hat, double step_by_t, //неявный случай
- double * norma_DELTA_c_line, double * norma_delta_c_line,
- double * norma_DELTA_l_line, double * norma_delta_l_line){
- int i;
- double del;
- *norma_DELTA_c_line = 0; *norma_delta_c_line = 0; //начальное максимальное значение [c]
- *norma_DELTA_l_line = 0; *norma_delta_l_line = 0;
- characteristic(V_hat);
- //printf("V_hat[%d]: %g\n", m-1,V_hat[m-1]);
- for(i=m-2; i>=1; i--){
- if (i==m-2) V_hat[i] = b[i-1]/U_1[i-1];
- else {
- V_hat[i] = (b[i-1] - U_2[i-1]*V_hat[i+1])/U_1[i-1];
- }
- del = delta(V_hat[i], x_by_stepx(i), (step_by_t+1)*tao); // разность между численным решением и точным решением
- norma_c(del, norma_DELTA_c_line); //[c]
- norma_c(V_hat[i], norma_delta_c_line); //[c]
- norma_l(del, norma_DELTA_l_line);
- norma_l(V_hat[i], norma_delta_l_line);
- printf("%g %g \n", x_by_stepx(i), del);
- // printf("NORMA_DELTA_c_line %g\n", *norma_DELTA_c_line);
- // printf("V_hat[%d]: %g\n", i,V_hat[i]);
- }
- }
- // ----------------------------------------------------------------
- void equal(double * b, double * V_hat){
- /* omega = 0 */
- int i;
- b[0] = V_hat[1] - tao/(4*h);
- for(i=1; i<m-2; i++){
- b[i] = V_hat[i+1];
- }
- }
- void equal1(double * b, double * V_hat){
- /* omega = 1 */
- int i;
- b[0] = V_hat[1] - (tao/8.)*(V_hat[3] - 4*V_hat[2] + 6*V_hat[1] - 3.0) - tao/(4*h);
- b[1] = V_hat[2] - (tao/8.)*(V_hat[4] - 4*V_hat[3] + 6*V_hat[2] - 4.0*V_hat[1] + 1);
- for(i=2; i<m-4; i++){
- 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]);
- }
- 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]);
- b[m-3] = V_hat[m-2] - (tao/8.)*(V_hat[m-4] - 4*V_hat[m-3] + 6*V_hat[m-2]);
- }
- /* ---------------- Явные схемы --------------------------------- */
- void main_loop_lin_simple(){
- int i;
- double block[m];
- double norma_DELTA_c_line; double norma_delta_c_line;
- double norma_DELTA_l_line; double norma_delta_l_line;
- FILE * fout = fopen("output_1.txt", "w+");
- int length = (int)(sizeof(block)/sizeof(double));
- begin_value(block, length);
- for (i = 0; i < n; ++i){
- next_block(block, i, &norma_DELTA_c_line, &norma_delta_c_line, &norma_DELTA_l_line, &norma_delta_l_line);
- /*norma_c(norma_DELTA_c_line, &norma_DELTA_c);
- norma_c(norma_delta_c_line, &norma_delta_c);
- norma_l(norma_DELTA_l_line, &norma_DELTA_l);
- norma_l(norma_delta_l_line, &norma_delta_l);*/
- print_block_to_file(block, length, fout);
- }
- printf("NORMA_DELTA_c %g\n", norma_DELTA_c_line);
- printf("NORMA_delta_c %g\n", norma_DELTA_c_line/norma_delta_c_line);
- printf("NORMA_DELTA_l %g\n", h*norma_DELTA_l_line);
- printf("NORMA_delta_l %g\n", norma_DELTA_l_line/norma_delta_l_line);
- printf("LIN__________\n");
- printf("tao h DeltaC DeltaL deltaC delta l\n");
- printf("%g & %g & %e & %e & %e & %e \n", 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);
- }
- /* ---------------- Нелинейный случай (неявный) -------------------*/
- void main_loop_lin_nelin(){
- int i;
- double block[m];
- double norma_DELTA_c_line; double norma_delta_c_line;
- double norma_DELTA_l_line; double norma_delta_l_line;
- FILE * fout = fopen("output_1.txt", "w+");
- int length = (int)(sizeof(block)/sizeof(double));
- begin_value(block, length);
- for (i = 0; i < n; ++i){
- next_block_nelin(block, i, &norma_DELTA_c_line, &norma_delta_c_line, &norma_DELTA_l_line, &norma_delta_l_line);
- // print_block_to_file(block, length, fout);
- }
- print_block__xvaluex(block, length, fout);
- printf("NORMA_DELTA_c %g\n", norma_DELTA_c_line);
- printf("NORMA_delta_c %g\n", norma_DELTA_c_line/norma_delta_c_line);
- printf("NORMA_DELTA_l %g\n", h*norma_DELTA_l_line);
- printf("NORMA_delta_l %g\n", norma_DELTA_l_line/norma_delta_l_line);
- printf("NO LIN__________\n");
- printf("tao%-7s h%-9s %-7sDeltaC %-7sDeltaL %-7sdeltaC %-7sdelta l\n", "","","","","","");
- printf("%-10g & %-10g & %-10e & %-10e & %-10e & %-10e \n\n", 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);
- }
- /* ---------------- Нелинейный случай (неявный) -------------------*/
- void begin_value_pristrel(double * block, int length){
- int i;
- for (i = 0; i < length; ++i){
- block[i] = 1.0; //-x_by_stepx(i)*0.5+0.5;
- }
- }
- void matrix_J( double * U_1, double * U_2, double * V_pr, double * G_2){
- int i;
- double l = 0; // íà÷àëüíîå l
- double a = 2*tao/(4*h);
- for (i=0; i<m-2; i++){
- if (i==0) { //âûðîæäåííûé ñëó÷àé
- U_1[i] = 1;
- U_2[i] = proverka(-a*V_pr[2]);
- G_2[i] = proverka(G_2[i]);
- l = proverka(a*V_pr[1]/U_1[i]); // ???????? 1==0?
- } else if (i!=m-3) {
- U_1[i] = proverka(1 - l*U_2[i-1]);
- U_2[i] = proverka(-a*V_pr[i+2]);
- G_2[i] = proverka(G_2[i] - l*G_2[i-1]);
- l = proverka(a*V_pr[i+1]/U_1[i]); // ??????????
- } else {
- U_1[i] = proverka(1 - l*U_2[i-1]);
- G_2[i] = proverka(G_2[i] - l*G_2[i-1]);
- }
- }
- // for (int i = 0; i < m-3; ++i){
- // printf("U_1[%-2d]: %-13g, U_2[%-2d]: %-13g \n", i, U_1[i], i, U_2[i]);
- // }
- }
- void matrix_G(double * V_pr, double * G, double * V){
- int i;
- double a = tao/(4*h);
- for (i=0; i<m-2; i++){
- if (i==0) {
- G[0] = -(V_pr[1] - V[1] - a*(V_pr[2]*V_pr[2] - 1));
- } else if (i!=m-3) {
- G[i] = -(V_pr[i+1] - V[i+1] - a*(V_pr[i+2]*V_pr[i+2] - V_pr[i]*V_pr[i]));
- } else {
- G[m-3] = -(V_pr[m-2] - V[m-2] - a*(0 - V_pr[m-3]*V_pr[m-3]));
- }
- }
- }
- void matrix_G_w1(double * V_pr, double * G, double * V){
- int i;
- double a = tao/(4*h); //111111111111111111111100000000000000000000000
- for (i=0; i<m-2; i++){
- if (i==0) {
- G[0] = -(V_pr[1] - V[1] - a*(V_pr[2]*V_pr[2] - 1) - tao*0.125*(-3 + V[3] - 4*V[2] + 6*V[1] )) ;
- } else if (i==1){
- 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+1] - 4*V[i] + 1));
- }
- else if ((i!=m-3)&&(i!=m-4)) {
- 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+1] - 4*V[i] + V[i-1]));
- } else if (i==m-4){
- G[m-4] = -(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*(0 - 4*V[i+2] + 6*V[i+1] - 4*V[i] + V[i-1]));
- } else if (i==m-3){
- G[m-3] = -(V_pr[i+1] - V[i+1] - a*(0 - V_pr[i]*V_pr[i]) - tao*0.125*(0 - 0 + 6*V[i+1] - 4*V[i] + V[i-1]));
- }
- }
- }
- void new_vector_X(double * U_1, double * U_2, double * b, double * X){
- int i;
- for(i=m-3; i>=0; i--){
- if (i == m-3) {
- X[i] = proverka(b[i]/U_1[i]);
- }
- else {
- X[i] = proverka((b[i] - U_2[i]*X[i+1])/U_1[i]);
- }
- }
- }
- void new_V_pr(double * X, double * V_pr){
- for (int i = 0; i < m-2; ++i){
- V_pr[i+1] = V_pr[i+1] + X[i];
- }
- }
- void equal_G2_G1(double * G_1, double * G_2){
- for (int i = 0; i < m-2; ++i)
- {
- G_2[i] = G_1[i];
- }
- }
- void equal_V_Vpr(double * V, double * V_pr){
- for (int i = 0; i < m; ++i)
- {
- V[i] = V_pr[i];
- }
- }
- double norma_G(double * G){
- double norma = 0;
- for (int i = 0; i < m-2; ++i){
- norma += fabs(G[i]);
- }
- return norma*h;
- }
- /* ------------------ ТЕСТ -----------------*/
- double delta_test(double v_n_m, double x, double t){
- double delta;
- delta = fabs(v_n_m-((1- x)/(t+2.0)));
- return delta;
- }
- void begin_value_test(double block[m], int length){
- int i;
- for (i = 0; i < length; ++i)
- {
- block[i] = -0.5*x_by_stepx(i)+0.5;
- }
- //printf("BLOCK[%g] = %g\n", x_by_stepx(i), block[i]);
- }
- void characteristic_test(double block[m], double t){
- block[0] = 2.0/(2.0+t);
- block[m-1] = 0.0;
- }
- void matrix_G_test(double V_pr[m], double G[m-2], double V[m], double t){
- int i;
- double a = tao/(4.0*h);
- for (i=0; i<m-2; i++){
- if (i==0) {
- G[0] = -(V_pr[1] - V[1] - a*(V_pr[2]*V_pr[2] - (2.0/(2.0+t))*(2.0/(2.0+t))));
- } else if (i==m-3) {
- G[m-3] = -(V_pr[m-2] - V[m-2] - a*(0.0- V_pr[m-3]*V_pr[m-3]));
- } else {G[i] = -(V_pr[i+1] - V[i+1] - a*(V_pr[i+2]*V_pr[i+2] - V_pr[i]*V_pr[i]));
- }
- }
- }
- void matrix_G1_test(double V_pr[m], double G[m-2], double V[m], double t){
- int i;
- double a = tao/(4.0*h);
- for (i=0; i<m-2; i++){
- if (i==0) {
- G[0] = -(V_pr[1] - V[1] - a*(V_pr[2]*V_pr[2] - (2.0/(2.0+t))*(2.0/(2.0+t))) -tao*0.125*(V[3]-4*V[2]+6*V[1]-4*(2.0/(2.0+t))+(3+h)/(t+2)));
- } else if (i == 1)
- {
- G[1] = -(V_pr[2] - V[2] - a*(V_pr[3]*V_pr[3] - V_pr[1]*V_pr[1]) -tao*0.125*(V[4]-4*V[3]+6*V[2]-4*V[1]+(2.0/(2.0+t))));
- }
- else if ((i!=m-3)&&(i!=m-4) ){
- 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+1]-4*V[i]+V[i-1]));
- }
- else if (i== m-4){
- 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*(0-4*V[i+3]+6*V[i+1]-4*V[i]+V[i-1]));
- }
- else { //i = m-3
- G[m-3] = -(V_pr[m-2] - V[m-2] - a*(0- V_pr[m-3]*V_pr[m-3])- tao*0.125*((-h)/(t+2)+6*V[m-2]-4*V[m-3]+V[m-4]));
- }
- }
- }
- void equal_V_Vpr_print(double V[m], double V_pr[m], int t){
- int i;
- double del;
- for (i = 0; i < m-1; ++i)
- {
- V[i] = V_pr[i];
- del = delta_test(V[i], x_by_stepx(i), t);
- if(t>0.9) printf("%g %g %g\n", V[i], del, x_by_stepx(i));
- }
- }
- void test(void){
- int i;
- double V_hat[m];
- double U_1[m-2]; double U_2[m-3];
- int length = (int)(sizeof(V_hat)/sizeof(double));
- //по всей сетке [c]
- double norma_DELTA_c_line; double norma_delta_c_line; //по линиям
- //по всей сетке [l]
- double norma_DELTA_l_line; double norma_delta_l_line; //по линиям
- FILE * fout_test = fopen("test.txt", "w");
- //################ ТЕСТ ##########################
- int j; double del;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;
- begin_value_test(V, length);
- equal_V_Vpr(V_0, V);
- for (i = 0; i < n; ++i)
- {
- equal_V_Vpr(V_pr, V_0);
- characteristic_test(V_pr,(i+1)*tao);
- while (fabs(norma_for_G) > 0.00001) {
- printf("i %d\n", i);
- printf("%e - NORMAAAAAAAAAAAAAAAAAAAAAAAAAA GGGGGGGGGGGGG\n\n", norma_for_G);
- matrix_G_test(V_pr, G_1, V, (i+1)*tao );
- equal_G2_G1(G_1, G_2);
- matrix_J(U_1, U_2, V_pr, G_2);
- new_vector_X(U_1, U_2, G_2, X);
- new_V_pr(X, V_pr);
- matrix_G_test(V_pr, G_1, V, (i+1)*tao);
- norma_for_G = norma_G(G_1);
- }
- norma_for_G = 1;
- equal_V_Vpr(V_0,V);
- equal_V_Vpr_print(V, V_pr, (i+1)*tao);
- printf("------------------------------`");
- norma_DELTA_c_line = 0; norma_delta_c_line = 0; //начальное максимальное значение [c]
- norma_DELTA_l_line = 0; norma_delta_l_line = 0; //начальное значение [l]
- for (j = 0; j < m-1; ++j)
- {
- del = delta_test(V_pr[j], x_by_stepx(j), (i+1)*tao); // разность между численным решением и точным решением
- norma_c(del, &norma_DELTA_c_line); //[c]
- norma_c(V_pr[j], &norma_delta_c_line); //[c]
- norma_l(del, &norma_DELTA_l_line); //[l]
- norma_l(V_pr[j], &norma_delta_l_line); //[l]
- }
- }
- print_block__xvaluex(V_pr, m, fout_test);
- printf("tao h DeltaC DeltaL deltaC delta l\n");
- printf("%g & %g & %e & %e & %e & %e \n", 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);
- }
- int main(){
- // INIT FOR IMPLICIT CASE
- double U_1[m-2]; double U_2[m-3]; double b[m-2]; double V_hat[m];
- double V_pr[m]; double G_1[m-2]; double G_2[m-2]; double V[m]; double X[m-2]; double norma_for_G = 1;
- double tochno_lin[m];
- double tochno_nelin[m];
- double tochno_test[m];
- double izm[m];
- int length = (int)(sizeof(V_hat)/sizeof(double)); // equal m
- double norma_DELTA_c_line; double norma_delta_c_line;
- double norma_DELTA_l_line; double norma_delta_l_line;
- FILE * fout = fopen("output_2.txt", "w+");
- FILE * fout_once = fopen("output_once.txt", "w");
- FILE * f_lin = fopen("lin.txt", "w");
- FILE * f_nelin = fopen("nelin.txt", "w");
- FILE * f_test = fopen("test.txt", "w");
- FILE * f_izm = fopen("izm.txt", "w");
- double del; // нужно для нормы
- printf("Size: %d x %d\n", m,n);
- for(int i; i<m; i++){
- tochno_lin[i] = res(x_by_stepx(i), 1);
- fprintf(f_lin, "%g %g\n", x_by_stepx(i), tochno_lin[i]);
- }
- for(int i; i<m; i++){
- tochno_nelin[i] = res_nel(x_by_stepx(i), 1);
- fprintf(f_nelin, "%g %g\n", x_by_stepx(i), tochno_nelin[i]);
- }
- for(int i; i<m; i++){
- tochno_test[i] = res_test(x_by_stepx(i), 1);
- fprintf(f_test, "%g %g\n", x_by_stepx(i), tochno_test[i]);
- }
- /*#########################
- Тест
- ###########################*/
- test();
- //return 0;
- /*#########################
- Явная схема
- ###########################*/
- //main_loop_lin_simple();
- //main_loop_lin_nelin();
- //return 0;
- /*#########################
- Линейный случай
- ###########################*/
- /*
- matrix_b1(b);
- for(int i=0; i<n; i++){
- //printf("\nTao: %f, Step %d\n\n", t_by_stept(i+1), i);
- matrix_U(U_1, U_2, b);
- 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);
- equal1(b, V_hat);
- print_block_to_file(V_hat, length, fout);
- //printf("NORMA_DELTA_c_line %g\n", norma_DELTA_c_line);
- //printf("NORMA_DELTA_c %g\n", norma_DELTA_c);
- }
- print_block__xvaluex(V_hat, length, fout_once);
- //printf("NORMA_DELTA_c %g\n", norma_DELTA_c);
- //printf("NORMA_delta_c %g\n", norma_DELTA_c/norma_delta_c);
- //printf("NORMA_DELTA_l %g\n", h*norma_DELTA_l);
- printf("NELIN \n");
- printf("tao h DeltaC DeltaL deltaC delta l\n");
- printf("%g & %g & %e & %e & %e & %e \n", 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);
- return 0;
- */
- /*#########################
- Нелинейный случай
- ###########################*/
- for(int k=0; k<4; k++){
- begin_value(V, length);
- for (int i = 0; i < n; ++i)
- {
- begin_value_pristrel(V_pr, length);
- V_pr[0] = 1; //1/(1 + 0.5*(tao*(i+1)))
- V_pr[m-1] = 0;
- while (norma_for_G > 1e-13) {
- // printf("%g - NORMAAAAAAAAAAAAAAAAAAAAAAAAAA GGGGGGGGGGGGG\n\n", norma_for_G);
- matrix_G(V_pr, G_1, V);
- equal_G2_G1(G_1, G_2);
- matrix_J(U_1, U_2, V_pr, G_2);
- new_vector_X(U_1, U_2, G_2, X);
- new_V_pr(X, V_pr);
- matrix_G(V_pr, G_1, V); // изменение G для нового вектора пристреленного
- // printf("%s", "lol ");
- // printf("%s", "kek ");
- norma_for_G = norma_G(G_1);
- printf("%g\n", norma_for_G);
- }
- norma_for_G = 1;
- }
- norma_DELTA_c_line = 0; norma_delta_c_line = 0; //начальное максимальное значение [c]
- norma_DELTA_l_line = 0; norma_delta_l_line = 0; //начальное значение [l]
- for (int j = 0; j < m-1; ++j)
- {
- del = delta_nel(V_pr[j], x_by_stepx(j), (i+1)*tao); // разность между численным решением и точным решением
- printf("%s %f %f %f \n", "X and T and del",x_by_stepx(j), (i+1)*tao, del);
- norma_c(del, &norma_DELTA_c_line); //[c]
- norma_c(V_pr[j], &norma_delta_c_line); //[c]
- norma_l(del, &norma_DELTA_l_line); //[l]
- norma_l(V_pr[j], &norma_delta_l_line); //[l]
- }
- print_block(V_pr, m);
- equal_V_Vpr(V, V_pr);
- printf("I : %d\n", i);
- equal_V_Vpr(izm, V_pr);
- if (k>=1){
- }
- k++;
- print_block__xvaluex(V_pr, m, fout_once);
- printf("NELIN \n");
- printf("tao h DeltaC DeltaL deltaC delta l\n");
- printf("%g & %g & %e & %e & %e & %e \n", 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);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment