Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using int3;
- namespace int3
- {
- class Matrix
- {
- #region variables
- private int columns, equation, size,m;
- private double interval_start, interval_end, x, h;
- private double[,] table;
- private double[] X;
- double Alfa = 1 / 2, Beta = -1 / 2;
- #endregion
- public void SetInterval()
- {
- Console.WriteLine("Podaj początek przedziału:");
- interval_start = Convert.ToDouble(Console.ReadLine());
- Console.WriteLine("Podaj koniec przedziału:");
- interval_end = Convert.ToDouble(Console.ReadLine());
- Console.WriteLine("Podaj liczbę przedziałów:");
- size = Convert.ToInt32(Console.ReadLine());
- columns = size + 1;
- equation = size;
- Console.Clear();
- }
- public void SetTable()
- {
- table = new double[size,columns];
- }
- public void FillTable()
- {
- SetTable();
- for (int x = 0; x < equation; x++)
- {
- for (int y = 0; y < equation; y++)
- {
- if (x == y)
- table[x,y] = 4;
- else if (x - 1 == y || x == y - 1)
- table[x,y] = 1;
- else
- table[x,y] = 0;
- }
- }
- table[0,1] = 2;
- table[size - 1,size - 2] = 2;
- }
- public void ShowTable()
- {
- for (int x = 0; x < table.GetLength(0); x++)
- {
- for (int y = 0; y < table.GetLength(1); y++)
- {
- Console.Write($"{table[x,y]} ");
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- }
- public void ReadX()
- {
- Console.WriteLine("Podaj x:");
- x = Convert.ToDouble(Console.ReadLine());
- Console.Clear();
- h = (interval_end - interval_start) / size;
- if (x != interval_end)
- m = (int)Math.Floor((x-interval_start)/h);
- else m = size - 1;
- X = new double[size];
- for (int y = 0; y < size; y++)
- X[y] = interval_start + y * h;
- }
- public void Derivatives()
- {
- for (int x = 0; x < size; x++)
- {
- if (x == 0)
- table[x, 6] = 1 / (1 + Math.Pow(X[x], 2)) + (1 / 3 * h * Alfa);
- else if (x == size - 1)
- table[x, 6] = 1 / (1 + Math.Pow(X[x], 2)) - (1 / 3 * h * Beta);
- else
- table[x, 6] = 1 / (1 + Math.Pow(X[x], 2));
- }
- }
- public void GaussMethod()
- {
- for (int column = 0; column < size; column++)
- {
- double max = table[column, column];
- equation = column;
- for (int x = column; x < size; x++)
- {
- if (Math.Abs(table[x, column]) > Math.Abs(max))
- {
- max = table[x, column];
- equation = x;
- }
- }
- if (Math.Abs(max) < 1e-30)
- {
- throw new Exception();
- }
- for (int x = column; x < size + 1; x++)
- {
- SwapElements(column, x, equation, x);
- }
- double p = table[column, column];
- for (int x = column; x < size + 1; x++)
- {
- table[column, x] = table[column, x] / p;
- }
- for (int x = 0; x < size ; x++)
- {
- if (x != column)
- {
- double z = table[x, column];
- for (int y = column; y < size + 1; y++)
- {
- table[x, y] = table[x, y] - z * table[column, y];
- }
- }
- }
- }
- }
- public void CalculateResults()
- {
- double Xm = interval_start + m * h, t;
- if (x != interval_end) t = (x - Xm) / h;
- else t = 1;
- double[] tab = new double[size + 2];
- for (int y = 1; y < size + 1; y++)
- {
- tab[y] = table[y - 1, 6];
- }
- tab[0] = tab[2] - 1 / 3 * h * Alfa;
- tab[size + 1] = tab[size - 1] + 1 / 3 * h * Beta;
- for (int i = 0; i < size + 2; i++)
- {
- Console.WriteLine($"C({ i - 1}) = {tab[i]}");
- }
- double S = CalculateS(tab, m, t);
- Console.WriteLine($"Y= { S }");
- }
- public void SwapElements(int x1, int y1, int x2, int y2)
- {
- double temp = table[x1, y1];
- table[x1, y1] = table[x2, y2];
- table[x2, y2] = temp;
- }
- public static double CalculateS(double[] tab, int m, double t) =>
- tab[m] * Math.Pow(1 - t, 3) +
- tab[m + 1] * (Math.Pow(2 - t, 3) - 4 * Math.Pow(1 - t, 3)) +
- tab[m + 2] * (Math.Pow(1 + t, 3) - 4 * Math.Pow(t, 3)) +
- tab[m + 3] * Math.Pow(t, 3);
- }
- class Program
- {
- static void Main(string[] args)
- {
- var user = new Matrix();
- user.SetInterval();
- user.FillTable();
- user.ShowTable();
- user.ReadX();
- user.Derivatives();
- user.ShowTable();
- user.GaussMethod();
- user.ShowTable();
- user.CalculateResults();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement