Advertisement
Guest User

Untitled

a guest
Nov 17th, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace HeatEquation {
  9.     class Function {
  10.         double h;
  11.         double tao;
  12.  
  13.         public int K;
  14.         public int J;
  15.  
  16.         public double lStepH;
  17.         public double lStepT;
  18.                
  19.         double[] mu;
  20.         double[] mu1;
  21.         double[] mu2;
  22.  
  23.         public string[,] graphAcc;
  24.  
  25.         double e = 2.71828182846;
  26.         public double a = 0.018;
  27.  
  28.         public Function(double hi, double taoi) {
  29.             h = hi;
  30.             tao = taoi;
  31.  
  32.             J = Convert.ToInt32(1 / h) + 1;
  33.             K = Convert.ToInt32(1 / tao) + 1;
  34.  
  35.             lStepH = h - 1 - h * J;
  36.             lStepT = 1 - tao * K;
  37.  
  38.             double t = 0;
  39.             double x = 0;
  40.  
  41.             mu = new double[J + 1];
  42.             mu1 = new double[K + 1];
  43.             mu2 = new double[K + 1];
  44.  
  45.             graphAcc = new string[11, 11];
  46.             double taog = 0.1;
  47.             double hg = 0.1;
  48.             for (int i = 0; i < 11; ++i)
  49.                 for (int j = 0; j < 11; ++j)
  50.                     graphAcc[i, j] = Convert.ToString(Value(i * hg, j * taog));
  51.  
  52.             for (int i = 0; i < J; ++i) {
  53.                 mu[i] = Value(x, 0);
  54.                 x += h;
  55.             }
  56.             //mu[J] = Value(1, 0);
  57.  
  58.             for(int i = 0; i < K; ++i) {
  59.                 mu1[i] = Value(0, t);
  60.                 mu2[i] = Value(1, t);
  61.                 t += tao;
  62.             }
  63.  
  64.             mu1[K] = Value(0, t);
  65.             mu1[K] = Value(0, t);
  66.         }
  67.  
  68.         public double Value(double x, double t) {
  69.             return 2 * Math.Pow(x, 4) - 3 * Math.Pow(t, 3) + 3 * Math.Pow(t, 2) * x - 2 * Math.Pow(e, x);
  70.         }
  71.  
  72.         public double f(double x, double t) {
  73.             return -9 * Math.Pow(t, 2) + 6 * t * x - a * (24 * Math.Pow(x, 2) - 2 * Math.Pow(e, x));
  74.         }
  75.  
  76.         public double dTwoFx(double x, double t) {
  77.             return -a * (48 - 2 * Math.Pow(e, x));
  78.         }
  79.  
  80.         public double F(double x, double t) {
  81.             return f(x, t) + h * h * dTwoFx(x, t) / 12;
  82.         }
  83.  
  84.  
  85.         public double[] Mu() {
  86.             return mu;
  87.         }
  88.  
  89.         public double[] Mu1() {
  90.             return mu1;
  91.         }
  92.  
  93.         public double[] Mu2() {
  94.             return mu2;
  95.         }
  96.     }
  97.  
  98.     class Program {
  99.         static double tao = 0.01;
  100.         static double h = 0.02;
  101.  
  102.         static void Main(string[] args) {
  103.  
  104.             Function u = new Function(h, tao);
  105.  
  106.             double a = u.a;
  107.             double sigma = 0.5 - h * h / (12 * a * tao);
  108.  
  109.             int J = u.J;
  110.             int K = u.K;
  111.  
  112.             int xg = Convert.ToInt32(J / 2);
  113.  
  114.             string[,] graphApp = new string[J, K];
  115.  
  116.             double[] uNext = new double[J];
  117.             double[] uCur = new double[J];
  118.  
  119.             double[] error = new double[J];
  120.             double[] rightPart = new double[J];
  121.  
  122.             double x = h;
  123.             double t = 0;
  124.  
  125.             double mainDiag = 1 / tao + 2 * a * sigma / (h * h);
  126.             double secnDiag = -a * sigma / (h * h);
  127.  
  128.             double[] al = new double[J];
  129.             double[] bt = new double[J];
  130.  
  131.             for (int i = 0; i < J; ++i) {
  132.                 uCur[i] = u.Mu()[i];
  133.                 graphApp[i, 0] = Convert.ToString(u.Mu()[i]);
  134.             }
  135.  
  136.             double maxError = 0;
  137.  
  138.             double k = a * (1 - sigma) / (h * h);
  139.  
  140.             for (int l = 1; l < K; ++l) {
  141.                 rightPart[1] = u.F(x, t + tao / 2) + k * uCur[2] + k * uCur[0] + (1 / tao - 2 * k) * uCur[1] + a * sigma / (h * h) * u.Mu1()[l];
  142.                 for (int i = 2; i < J - 2; ++i) {
  143.                     x += h;
  144.                     rightPart[i] = u.F(x, t + tao / 2) + k * uCur[i + 1] + k * uCur[i - 1] + (1 / tao - 2 * k) * uCur[i];
  145.                 }
  146.                 x += h;
  147.                 rightPart[J - 2] = u.F(x, t + tao / 2) + k * uCur[J - 1] + k * uCur[J - 3] + (1 / tao - 2 * k) * uCur[J - 2] + a * sigma / (h * h) * u.Mu2()[l];
  148.  
  149.                 x = h;
  150.  
  151.                 al[1] = secnDiag / mainDiag;
  152.                 bt[1] = rightPart[1] / mainDiag;
  153.                 for (int i = 2; i < J - 1; ++i) {
  154.                     al[i] = secnDiag / (mainDiag - al[i - 1] * secnDiag);
  155.                     bt[i] = (rightPart[i] - bt[i - 1] * secnDiag) / (mainDiag - al[i - 1] * secnDiag);
  156.                 }
  157.  
  158.                 uNext[J - 1] = u.Mu2()[l];
  159.                 uNext[J - 2] = bt[J - 2];
  160.                 for (int i = J - 3; i > 0; --i) {
  161.                     uNext[i] = -al[i] * uNext[i + 1] + bt[i];
  162.                 }
  163.                 uNext[0] = u.Mu1()[l];
  164.  
  165.                 t += tao;
  166.  
  167.                 for (int i = 0; i < J; ++i) {
  168.                     error[i] = u.Value(i * h, t) - uNext[i];
  169.                     uCur[i] = uNext[i];
  170.                     graphApp[i, l] = Convert.ToString(uNext[i]);
  171.                 }
  172.  
  173.                 double curError = 0;
  174.                 for (int i = 0; i < J; ++i) {
  175.                     error[i] = Math.Abs(error[i]);
  176.  
  177.                     if (curError < error[i]) {
  178.                         curError = error[i];
  179.                     }
  180.  
  181.                     //Console.WriteLine(error[i]);
  182.                     // Console.ReadKey();
  183.                 }
  184.  
  185.                 if (maxError < curError)
  186.                     maxError = curError;
  187.  
  188.                 Console.WriteLine(maxError);
  189.                 //Console.ReadKey();
  190.             }
  191.             Console.WriteLine(maxError);
  192.  
  193.             using (StreamWriter app = new StreamWriter(@"C:\UserDocs\HeatEquationApproximation.txt")) {
  194.                 app.WriteLine("Approximation:");
  195.                 for (int i = 0; i < J; ++i) {
  196.                     app.WriteLine(i + ".   ");
  197.                     for (int j = 0; j < K; ++j) {
  198.                         app.WriteLine(graphApp[i, j]);
  199.                     }
  200.                     app.WriteLine();
  201.                 }
  202.                
  203.             }
  204.  
  205.             using (StreamWriter acc = new StreamWriter(@"C:\UserDocs\HeatEquationAccuraci.txt")) {
  206.                 acc.WriteLine("Accuraci:");
  207.                 for (int i = 0; i < 11; ++i) {
  208.                     acc.WriteLine(i + ".   ");
  209.                     for (int j = 0; j < 11; ++j) {
  210.                         acc.WriteLine(u.graphAcc[i, j]);
  211.                     }
  212.                     acc.WriteLine();
  213.                     acc.WriteLine();
  214.                 }
  215.             }
  216.  
  217.           //  System.IO.File.WriteAllLines(@"C:\UserDocs\HeatEquation.txt", u.graphAcc);
  218.             //System.IO.File.WriteAllLines(@"C:\UserDocs\HeatEquation.txt", graphApp);
  219.  
  220.             // Console.ReadKey()  ;
  221.             Console.WriteLine();
  222.             //Console.WriteLine(maxError);
  223.             Console.ReadKey();
  224.             Console.ReadKey();
  225.         }
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement