Advertisement
Guest User

Untitled

a guest
Nov 9th, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp6
  4. {
  5.     class Program
  6.     {
  7.         static double foo(double x)
  8.         {
  9.             return x * x - 2 * x - 2 * Math.Cos(x);
  10.         }
  11.         static void Main(string[] args)
  12.         {
  13.             double masheps = 1;
  14.             while (1 + masheps > 1)
  15.                 masheps = masheps / 2;
  16.             double a = -0.5;
  17.             double b = 1;
  18.             double L = b - a;
  19.             //=================================================================
  20.             // Шаг 2
  21.             //  var x0 = (foo(a)-foo(b)+L*(a+b))/(2*L)
  22.             double x0 = 1 / (2 * L) * (foo(a) - foo(b) + L * (a + b));
  23.             double y0 = 1 / 2 * (foo(a) + foo(b) + L * (a - b));
  24.             double phimin = y0;
  25.             // Шаг 3
  26.             double delta = 1 / (2 * L) * (foo(x0) - phimin);
  27.  
  28.             // Шаг 4
  29.  
  30.            
  31.  
  32.  
  33.                 if (2 * L * delta < masheps)
  34.                 {
  35.                    
  36.                     Console.WriteLine($"х = {x0}");
  37.                
  38.                 }
  39.                 else
  40.                 {
  41.                     //шаг 5
  42.                     double x1L = x0 - delta;
  43.                     double x1R = x0 + delta;
  44.                     double phi = 1 / 2 * (foo(x0) + phimin);
  45.                     //шаг 6
  46.                     if (foo(x1L) < foo(x1R))
  47.                     {
  48.                         x0 = x1L;
  49.                         Console.WriteLine(x0);
  50.                         Console.WriteLine("x1l");
  51.                     }
  52.                     else
  53.                     {
  54.                         x0 = x1R;
  55.                         Console.WriteLine(x0);
  56.                         Console.WriteLine("x1r");
  57.                     }
  58.                     y0 = phi;
  59.                 }
  60.  
  61.            
  62.  
  63.             Console.ReadKey();
  64.  
  65.         }
  66.     }
  67. }
  68.  
  69.  
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement