Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MetodyObliczeniowe
- {
- class Program
- {
- static double h;
- static double Y(double x)
- {
- return Math.Pow(x, 2) - x;
- //return Math.Pow(x, 2) + x + 1;
- }
- static double F(double x, double y)
- {
- return Math.Sqrt(y + x) + x - 1;
- // return 1 + 2 * Math.Sqrt(y - x - 1);
- }
- static double Yk_Euler(double x, double y)
- {
- return y + h * F(x, y);
- }
- static double Yk_ModifiedEuler(double x, double y)
- {
- return y + h * F(x + h / 2, y + (h / 2) * F(x, y));
- }
- static void Main(string[] args)
- {
- int N = -1;
- double a =- 1;
- String DataString = "";
- while (N < 1)
- {
- Console.WriteLine("Podaj liczbę naturalną N >= 1.");
- N = Int32.Parse(Console.ReadLine());
- }
- while(a <= 1)
- {
- Console.WriteLine("Podaj liczbę rzeczywistą a większą od 1.");
- a = Double.Parse(Console.ReadLine());
- }
- h = (a - 1) / N;
- double[] xk = new double[N+1];
- for (int i = 0; i <= N; i++)
- xk[i] = 1 + i * h;
- for (int i = 0; i <= N; i++)
- DataString += "x" + i + " = " + xk[i] + "\n";
- Console.WriteLine("\nN = {0} \nh = {1} \n" + DataString, N, h);
- Console.WriteLine("Metoda Eulera:");
- double[] yk_Euler = new double[N + 1];
- yk_Euler[0] = Y(1);
- for (int i = 1; i <= N; i++)
- yk_Euler[i] = Yk_Euler(xk[i - 1], yk_Euler[i - 1]);
- for (int i = 0; i <= N; i++)
- Console.WriteLine("y" + i + " = {0}", yk_Euler[i]);
- for (int i = 1; i <= N; i++)
- Console.WriteLine("y(x" + i + ") = {0}", Y(xk[i]));
- double[] AbsDiffrenceValueEuler = new double[N+1];
- for (int i = 1; i <= N; i++)
- AbsDiffrenceValueEuler[i] = Math.Abs(yk_Euler[i] - Y(xk[i]));
- Console.WriteLine("Błąd maksymalny tej metody : {0}", AbsDiffrenceValueEuler.Max());
- Console.WriteLine("\nZmodyfikowana metoda Eulera : ");
- double[] yk_ModifiedEuler = new double[N + 1];
- yk_ModifiedEuler[0] = Y(1);
- for (int i = 1; i <= N; i++)
- yk_ModifiedEuler[i] = Yk_ModifiedEuler(xk[i - 1], yk_Euler[i - 1]);
- for (int i = 0; i <= N; i++)
- Console.WriteLine("y" + i + " = {0}", yk_ModifiedEuler[i]);
- for (int i = 1; i <= N; i++)
- Console.WriteLine("y(x" + i + ") = {0}", Y(xk[i]));
- double[] AbsDiffrenceValueMEuler = new double[N + 1];
- for (int i = 1; i <= N; i++)
- AbsDiffrenceValueMEuler[i] = Math.Abs(yk_ModifiedEuler[i] - Y(xk[i]));
- Console.WriteLine("Błąd maksymalny tej metody : {0}", AbsDiffrenceValueMEuler.Max());
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement