Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 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 MetodyObliczeniowe
  8. {
  9.     class Program
  10.     {
  11.         static double h;
  12.         static double Y(double x)
  13.         {
  14.             return Math.Pow(x, 2) - x;
  15.             //return Math.Pow(x, 2) + x + 1;
  16.         }
  17.  
  18.         static double F(double x, double y)
  19.         {
  20.              return Math.Sqrt(y + x) + x - 1;
  21.            // return 1 + 2 * Math.Sqrt(y - x - 1);
  22.         }
  23.         static double Yk_Euler(double x, double y)
  24.         {
  25.             return y + h * F(x, y);
  26.         }
  27.  
  28.         static double Yk_ModifiedEuler(double x, double y)
  29.         {
  30.             return y + h * F(x + h / 2, y + (h / 2) * F(x, y));
  31.         }
  32.  
  33.         static void Main(string[] args)
  34.         {
  35.             int N = -1;
  36.             double a =- 1;
  37.             String DataString = "";
  38.  
  39.             while (N < 1)
  40.             {
  41.                 Console.WriteLine("Podaj liczbę naturalną N >= 1.");
  42.                 N = Int32.Parse(Console.ReadLine());
  43.             }
  44.  
  45.             while(a <= 1)
  46.             {
  47.                 Console.WriteLine("Podaj liczbę rzeczywistą a większą od 1.");
  48.                 a = Double.Parse(Console.ReadLine());
  49.             }
  50.  
  51.  
  52.             h = (a - 1) / N;
  53.  
  54.             double[] xk = new double[N+1];
  55.  
  56.             for (int i = 0; i <= N; i++)
  57.                 xk[i] = 1 + i * h;
  58.  
  59.             for (int i = 0; i <= N; i++)
  60.                 DataString += "x" + i + " = " + xk[i] + "\n";
  61.  
  62.             Console.WriteLine("\nN = {0} \nh = {1} \n" + DataString, N, h);
  63.  
  64.             Console.WriteLine("Metoda Eulera:");
  65.  
  66.             double[] yk_Euler = new double[N + 1];
  67.  
  68.             yk_Euler[0] = Y(1);
  69.  
  70.             for (int i = 1; i <= N; i++)
  71.                 yk_Euler[i] = Yk_Euler(xk[i - 1], yk_Euler[i - 1]);
  72.  
  73.             for (int i = 0; i <= N; i++)
  74.                 Console.WriteLine("y" + i + " =  {0}", yk_Euler[i]);
  75.  
  76.             for (int i = 1; i <= N; i++)
  77.                 Console.WriteLine("y(x" + i + ") = {0}",  Y(xk[i]));
  78.  
  79.  
  80.             double[] AbsDiffrenceValueEuler = new double[N+1];
  81.  
  82.             for (int i = 1; i <= N; i++)
  83.                 AbsDiffrenceValueEuler[i] = Math.Abs(yk_Euler[i] - Y(xk[i]));
  84.  
  85.             Console.WriteLine("Błąd maksymalny tej metody : {0}", AbsDiffrenceValueEuler.Max());
  86.  
  87.             Console.WriteLine("\nZmodyfikowana metoda Eulera : ");
  88.  
  89.             double[] yk_ModifiedEuler = new double[N + 1];
  90.  
  91.             yk_ModifiedEuler[0] = Y(1);
  92.  
  93.             for (int i = 1; i <= N; i++)
  94.                 yk_ModifiedEuler[i] = Yk_ModifiedEuler(xk[i - 1], yk_Euler[i - 1]);
  95.  
  96.             for (int i = 0; i <= N; i++)
  97.                 Console.WriteLine("y" + i + " =  {0}", yk_ModifiedEuler[i]);
  98.  
  99.             for (int i = 1; i <= N; i++)
  100.                 Console.WriteLine("y(x" + i + ") = {0}", Y(xk[i]));
  101.  
  102.             double[] AbsDiffrenceValueMEuler = new double[N + 1];
  103.  
  104.             for (int i = 1; i <= N; i++)
  105.                 AbsDiffrenceValueMEuler[i] = Math.Abs(yk_ModifiedEuler[i] - Y(xk[i]));
  106.  
  107.             Console.WriteLine("Błąd maksymalny tej metody : {0}", AbsDiffrenceValueMEuler.Max());
  108.  
  109.             Console.ReadLine();
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement