Advertisement
mrAnderson33

Untitled

Dec 20th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. namespace нелинейная_система
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             const double eps = 0.0001;
  11.             Console.WriteLine("Пара корней системы ");
  12.             var xn = MPI(new double[2] { 0.6, 0.5 }, eps);
  13.             Console.WriteLine("x=[{0}  {1}]", xn[0],xn[1]);
  14.         }
  15.  
  16.  
  17.         public static double[] MPI(double[] xn, double eps)
  18.         {
  19.             int counter = 0;
  20.  
  21.             while (Norma(F(xn), xn) > eps && Norma(xn) > eps)
  22.             {
  23.                 xn = F(xn);
  24.                 Console.WriteLine("Итерация № {0} x1 = {1}  x2 = {2}", counter++, xn[0], xn[1]);
  25.             }
  26.  
  27.             xn = new double[] { Math.Round(xn[0], 4), Math.Round(xn[1], 4) };
  28.             return xn;
  29.         }
  30.  
  31.         public static double[] F(double []  x) => new double[] { Math.Sqrt(1 - Math.Pow(x[1], 2)), 2 * x[0] * Math.Exp(-x[0]) };
  32.         public static double Norma(double[] a, double[] b) => a.Diff(b).Abs().Max();
  33.         public static double Norma(double[] a) => a.Abs().Max();
  34.  
  35.  
  36.     }
  37.     public static class MyExtension
  38.     {
  39.         public static double[] Diff(this double[] a, double[] b)
  40.         {
  41.             if (a.Length != b.Length) throw new ArgumentException();
  42.             var ar = new double[a.Length];
  43.             for (int i = 0; i < a.Length; ++i) ar[i] = a[i] - b[i];
  44.             return ar;
  45.         }
  46.  
  47.         public static double Max(this double [] a)
  48.         {
  49.             double max = a[0];
  50.             foreach (var n in a) if (n > max) max = n;
  51.             return max;
  52.         }
  53.  
  54.         public static double[] Abs(this double[] a)
  55.         {
  56.             for (int i = 0; i < a.Length; ++i) a[i] *= a[i] > 0 ? 1 : -1;
  57.             return a;
  58.         }
  59.  
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement