using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication23
{
class Program
{
const double alpha0 = 3.1;
const double alpha1 = -1.4;
const double A = 8;
const double beta0 = 2.1;
const double beta1 = 5;
const double B = 1;
public static double f(double x)
{
return 3 * Math.Pow(x, 2);
}
public static double p()
{
return -4;
}
public static double q()
{
return 7;
}
public static double Exact(double x)
{
return 0.428571 * (x*x +1.14286*x + 0.154905 * Math.Pow(2.71828,(2*x))*Math.Sin(1.73205*x) + 0.204613 * Math.Pow(2.71828,(2*x))*Math.Cos(1.73205*x)+0.367347);
}
//public static double[] Gauss(double[,] M)
//{
// int rows = M.GetLength(0);
// int cols = M.GetLength(1);
// double[] a = new double[rows];
// double[] b = new double[rows];
// double[] c = new double[rows];
// double[] f = new double[rows];
// double m;
// //for (int i = 0; i < rows; i++)
// //{
// // for (int j = 0; j < cols; j++)
// // {
// // Console.Write("{0:f0} ", M[i, j]);
// // }
// // Console.WriteLine();
// //}
// for (int i = 1; i < rows + 1; i++)
// {
// b[i - 1] = M[i - 1, i - 1];
// c[i - 1] = M[i - 1, i];
// }
// for (int i = 0; i < rows; i++)
// {
// f[i] = M[i, rows];
// }
// for (int i = 0; i < rows - 1; i++)
// {
// a[i+1] = M[i + 1, i];
// }
// a[0] = 0;
// c[b.Length-1] = 0;
// //for (int i = 0; i < rows; i++)
// //{
// // Console.WriteLine("{0:f2} ", a[i]);
// //}
// for (int i = 1; i < rows; i++)
// {
// double mm = a[i] / b[i - 1];
// b[i] = b[i] - mm * c[i - 1];
// f[i] = f[i] - mm * f[i - 1];
// }
// double[] ans = new double[rows];
// ans[rows - 1] = f[rows - 1] / b[rows - 1];
// for (int i = rows - 2; i >= 0; i--)
// ans[i] = (f[i] - c[i] * ans[i + 1]) / b[i];
// return ans;
//}
public static double[] Gauss(double[,] M)
{
int rows = M.GetLength(0);
int cols = M.Length / M.GetLength(0);
for (int x = 0; x < cols - 1; ++x)
{
int index = -1;
for (int y = x; y < rows; ++y)
{
if (Math.Abs(M[y, x]) < 1e-6)
{
continue;
}
if (index == -1)
{
index = y;
}
else
{
double k = -1 * M[y, x] / M[index, x];
for (int x1 = x; x1 < cols; ++x1)
{
M[y, x1] = M[y, x1] + M[index, x1] * k;
}
}
}
}
for (int x = cols - 2; x >= 0; --x)
{
int index = -1;
for (int y = rows - 1; y >= 0; --y)
{
if (Math.Abs(M[y, x]) < 1e-6)
{
continue;
}
if (index == -1)
{
index = y;
double k = 1 / M[y, x];
M[y, x] = M[y, x] * k;
M[y, cols - 1] = M[y, cols - 1] * k;
}
else
{
double k = -1 * M[y, x] / M[index, x];
M[y, x] = M[y, x] + M[index, x] * k;
M[y, cols - 1] = M[y, cols - 1] + M[index, cols - 1] * k;
}
}
}
double[] ans = new double[rows];
for (int i = 0; i < ans.Length; ++i)
{
ans[i] = M[i, cols - 1];
}
return ans;
}
public static double[] Finite_differences(double a, double b, double h, int n)
{
double[,] M = new double[n + 1, n + 2];
for (int i = 1; i < n; ++i)
{
double x = a + i * h;
M[i, i + 1] = 1 + h * p() / 2;
M[i, i] = -(2 - h * h * q());
M[i, i - 1] = 1 - h * p() / 2;
M[i, n + 1] = h * h * f(x);
}
//O(h)
//M[0, 0] = h * alpha0 - alpha1;
//M[0, 1] = alpha1;
//M[0, n + 1] = A * h;
//M[n, n - 1] = -beta1;
//M[n, n] = h * beta0 + beta1;
//M[n, n + 1] = B * h;
//O(h^2)
M[0, 0] = 2 * h * alpha0 - 3 * alpha1;
M[0, 1] = 4 * alpha1;
M[0, 2] = -alpha1;
M[0, n + 1] = 2 * A * h;
M[n, n - 2] = beta1;
M[n, n - 1] = -4 * beta1;
M[n, n] = 2 * h * beta0 + 3 * beta1;
M[n, n + 1] = 2 * B * h;
double[] gauss = Gauss(M);
double[] res = new double[n + 1];
int index = 0;
for (double x = a; x <= b; x += h)
{
res[index] = gauss[index];
++index;
}
return res;
}
static void Main(string[] args)
{
const double a = 1.5;
const double b = 2.7;
const double h = 0.05;
int n = (int)((b - a) / h);
double[] x = new double[n + 1];
x = Finite_differences(a, b, h, n);
Console.WriteLine("X \tТочное значение\t\tY");
Console.WriteLine("---------------------------------------");
for (int i = 0; i < n + 1; i++)
{
string firstValue = string.Format("{0}", (a + i * h));
string secondValue = string.Format("{0:f4}", Math.Round(Exact(a + i * h), 4));
string thirdValue = string.Format("{0:f4}", Math.Round(x[i], 4));
Console.Write(firstValue);
if (firstValue.Length > 7)
{
Console.Write("\t");
}
else
{
Console.Write("\t\t");
}
Console.Write(secondValue);
if (secondValue.Length > 7)
{
Console.Write("\t");
}
else
{
Console.Write("\t\t");
}
Console.WriteLine(thirdValue);
}
}
}
}