Advertisement
mrAnderson33

Метод простых итераций для СЛАУ

Nov 4th, 2017
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace МПИ_СЛАУ
  7. {
  8.     class Program
  9.     {
  10.         delegate double func(double[] x);
  11.         static void Main(string[] args)
  12.         {
  13.               const double eps = 0.00001;
  14.  
  15.               func x1 = (x) =>  0 *x[0] - 2.42 / 40.41  *  x[1] -3.86/40.41 * x[2] + 20.41/40.41;
  16.               func x2 = (x) => -2.31/21.49 * x[0] + 0 * x[1] - 4.55/21.49 * x[2] + 30.11/21.49;
  17.               func x3 = (x) => -2.49 / 25.69 * x[0] - 4.85 / 25.96 * x[1] + 0 * x[2] + 12.24/25.96;
  18.  
  19.               double norma = 0.31;
  20.  
  21.               var xn = new double[3] { 0.51, 1.4,0.47};
  22.               var f = new double[3] { x1(xn), x2(xn), x3(xn) };
  23.  
  24.               while (norma /(1-norma) *  f.Diff(xn).Abs().Max()  > eps)
  25.               {
  26.                   xn = new double[3] { x1(xn), x2(xn), x3(xn) };
  27.                   f = new double[3] { x1(xn), x2(xn), x3(xn) };
  28.               }
  29.  
  30.             Console.WriteLine("{0}    {1}    {2}", f[0], f[1], f[2]);          
  31.  
  32.         }
  33.  
  34.        
  35.  
  36.     }
  37.     public static class MyExtension
  38.     {
  39.         public static double[] Diff(this double[] a, double[] b)
  40.         {
  41.             if (a.Length == 3 && b.Length == 3)
  42.                 return new double[] { a[0] - b[0], a[1] - b[1], a[2] - b[2] };
  43.             else throw new ArgumentOutOfRangeException();
  44.         }
  45.  
  46.         public static double[] Abs(this double[] a)
  47.         {
  48.             for (int i = 0; i < a.Length; ++i) a[i] *= a[i] > 0 ? 1 : -1;
  49.             return a;
  50.         }
  51.  
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement