Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.37 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 Integral
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int N = 20;
  14.             double a = 1;
  15.             double b = 2;
  16.             double[] xi = new double[N+1];
  17.             double[] fxi = new double[N + 1];
  18.             double[] rectangle = new double[N + 1];
  19.             double[] trapeze = new double[N + 1];
  20.             int x0 = 0;
  21.             double A = 0;
  22.             double B = 2;
  23.             double dx = (B - A) / N;
  24.             xi = GetXi(xi, dx, x0);
  25.             fxi = GetfXi(xi, a, b);
  26.             rectangle = GetRectangleSolution(fxi, dx);
  27.             trapeze = GetTrapeziumSolution(fxi, dx);
  28.             double sumRectangle = GetSumArray(rectangle);
  29.             double sumTrapeze = GetSumArray(trapeze);
  30.             double calcualtionError = Math.Abs(sumRectangle - sumTrapeze);
  31.             double upperValue = (1 / Math.Pow(a, 3)) * ((a * 2 + b) - 2 * b * Math.Log(Math.Abs(a * 2 + b)) - (Math.Pow(b, 2) / (a * 2 + b)));
  32.             double lowerValue = (1 / Math.Pow(a, 3)) * ((a * 0 + b) - 2 * b * Math.Log(Math.Abs(a * 0 + b)) - (Math.Pow(b, 2) / (a * 0 + b)));
  33.             double exactValue=upperValue - lowerValue;
  34.            
  35.             ShowResult(xi, fxi, rectangle, trapeze,exactValue);
  36.            
  37.             Console.ReadKey();
  38.  
  39.         }
  40.      
  41.         static double[] GetXi(double[] xi,double dx, int x0)
  42.         {
  43.             for (int i = 0; i < xi.Length; i++)
  44.                 xi[i] = x0 + dx * i;
  45.  
  46.             return xi;
  47.         }
  48.         static double[] GetfXi(double[] xi, double a, double b)
  49.         {
  50.             double[] result = new double[xi.Length];
  51.             for (int i = 0; i < xi.Length; i++)
  52.                 result[i] = Math.Pow(xi[i], 2) / Math.Pow((xi[i] * a + b), 2);
  53.  
  54.             return result;
  55.         }
  56.         static double[] GetRectangleSolution(double[] fxi, double dx)
  57.         {
  58.             double[] result = new double[fxi.Length-1];
  59.             for (int i = 0; i < result.Length; i++)
  60.                 result[i] = fxi[i] * dx;
  61.  
  62.             return result;
  63.         }
  64.         static double GetSumArray(double[] array)
  65.         {
  66.             double result = 0;
  67.             for (int i = 0; i < array.Length; i++)
  68.                 result += array[i];
  69.  
  70.             return result;
  71.         }
  72.         static void ShowArray(double[] array)
  73.         {
  74.             for (int i = 0; i < array.Length; i++)
  75.                 Console.WriteLine(" " + array[i]);  
  76.         }
  77.         static void ShowResult(double[] xi, double[] fxi, double[] rectangle, double[] trapeze,double exactValue)
  78.         {
  79.             Console.WriteLine("xi\tfxi\trec\ttrape");
  80.             for (int i = 0; i < xi.Length-1; i++)
  81.                 Console.WriteLine("{0}\t{1:0.000}\t{2:0.0.000}\t{3:0.000}", xi[i],fxi[i],rectangle[i],trapeze[i]);
  82.             Console.WriteLine();
  83.             Console.WriteLine("Точное значение ={0:0.000}",exactValue);
  84.            
  85.         }
  86.         static double[] GetTrapeziumSolution(double[] fxi,double dx)
  87.         {
  88.             double[] result = new double[fxi.Length - 1];
  89.             for (int i = 0; i < fxi.Length-1; i++)
  90.                 result[i] = (fxi[i] + fxi[i + 1]) / 2 * dx;
  91.  
  92.             return result;
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement