Advertisement
Konark

Untitled

Dec 2nd, 2015
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 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.  
  7. namespace ConsoleApplication24
  8. {
  9.     class Program
  10.     {
  11.         static double Fi1 (double x, double y)
  12.         {
  13.             return 1 + 3 * Math.Sin(Math.PI * x / 2);
  14.         }
  15.  
  16.         static double Fi2 (double x, double y)
  17.         {
  18.             return 1 + Math.Sin(Math.PI * y);
  19.         }
  20.  
  21.         public static double roundValues (double x)
  22.         {
  23.             return (double)Math.Round(x * 1000) / (double)1000;
  24.         }
  25.  
  26.         static void printM(double[,] m)
  27.         {
  28.             for (int i = 0; i < m.GetLength(0); i++)
  29.             {
  30.                 for (int j = 0; j < m.GetLength(1); j++)
  31.                 {
  32.                     Console.Write(roundValues(m[j, j]) + "   ");
  33.                 }
  34.                 Console.WriteLine("");
  35.             }
  36.         }
  37.  
  38.         static void Main(string[] args)
  39.         {
  40.             int nx, ny;
  41.             nx = 10;
  42.             ny = 10;
  43.             double[] x = new double[nx];
  44.             double[] y = new double[ny];
  45.             double[,] u = new double[nx,ny];
  46.             double Hx, Hy, alpha, d, du, Uold, Unew;
  47.             Hx = 0.01;
  48.             Hy = 0.01;
  49.             alpha = Hx / Hy;
  50.             for (int i = 0; i < nx; i++)
  51.             {
  52.                 x[i] = i * Hx;
  53.                 u[i, 0] = Fi1(x[i], 0);
  54.                 u[1, ny - 1] = Fi1(x[i], 1);
  55.             }
  56.  
  57.             for(int j = 1; j < nx; j++)
  58.             {
  59.                 y[j] = j * Hy;
  60.                 u[0, j] = Fi2(0, y[j]);
  61.                 u[nx - 1, j] = Fi2(1, y[j]);
  62.             }
  63.             du = 0;
  64.             d = 0;
  65.             do
  66.             {
  67.                 for (int i = 1; i < nx - 1; i++)
  68.                 {
  69.                     for (int j = 1; j < ny - 1; j++)
  70.                     {
  71.                         Uold = u[i, j];
  72.                         u[i, j] = 1 / (2 * (1 + alpha * alpha)) * ((u[i, j + 1]) + u[i, j - 1] * alpha * alpha + (u[i + 1, j] + u[i - 1, j]));
  73.                         Unew = u[i, j];
  74.                         if (Math.Abs(Unew) < 0.0001)
  75.                         {
  76.                             du = Math.Abs((Unew - Uold) / Unew);
  77.                         }
  78.                         if (du > d)
  79.                         {
  80.                             d = du;
  81.                         }
  82.                         u[i, j] = Unew;
  83.                     }
  84.                 }
  85.             } while (d > 0.0001);
  86.             printM(u);
  87.             Console.Read();
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement