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 APR
- {
- class UserMatrix
- {
- double[] FirstTable;
- double[] SecondTable;
- public UserMatrix()
- {
- Console.WriteLine("Podaj wielkość tablicy");
- FirstTable = new double[Convert.ToInt32(Console.ReadLine())];
- SecondTable = new double[FirstTable.GetLength(0)];
- FillMatrix();
- }
- private void FillMatrix()
- {
- for(int x=0; x < FirstTable.GetLength(0) ;x++)
- {
- Console.Clear();
- Console.WriteLine($"Podaj {x + 1} liczbe w 1 tablicy: ");
- FirstTable[x] = Convert.ToDouble(Console.ReadLine());
- Console.WriteLine($"Podaj {x + 1} liczbe w 2 tablicy: ");
- SecondTable[x] = Convert.ToDouble(Console.ReadLine());
- Console.Clear();
- }
- }
- public void ShowTables()
- {
- Console.WriteLine("Pierwsza tablica:");
- foreach (var item in FirstTable)
- {
- Console.Write($"{item} ");
- }
- Console.WriteLine("\nDruga tablica:");
- foreach (var item in SecondTable)
- {
- Console.Write($"{item} ");
- }
- }
- public double[] returnFirstTable => FirstTable;
- public double[] returnSecondTable => SecondTable;
- }
- class ProgramMatrix
- {
- double[,] table;
- int nodes;
- int level;
- UserMatrix user;
- public ProgramMatrix()
- {
- user = new UserMatrix();
- Console.WriteLine("Podaj ilość węzłów:");
- nodes = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Podaj stopień wielomianu:");
- level = Convert.ToInt32(Console.ReadLine());
- Console.Clear();
- table = new double[nodes, nodes + 1];
- Console.WriteLine("Macierz przed rozwiązaniem:");
- FillTable();
- }
- private double sum(double[]tab1,double[]tab2,int x,int y)
- {
- double result=0;
- if (x == nodes )
- {
- for (int i = 0; i < level; i++)
- {
- double t = (Math.Pow(tab1[i], y) *tab2[i]);
- result += t;
- }
- }
- else
- {
- for (int i = 0; i < level; i++)
- {
- result += Math.Pow(tab1[i], x + y);
- }
- }
- return result;
- }
- private void FillTable()
- {
- for(int x=0;x<nodes;x++)
- {
- for(int y=0;y<nodes+1;y++)
- {
- table[x, y] = sum(user.returnFirstTable,user.returnSecondTable,y,x);
- }
- }
- ShowTable();
- ShowResult();
- }
- private void ShowTable()
- {
- for (int x = 0; x < nodes; x++)
- {
- for (int y = 0; y < nodes + 1; y++)
- {
- Console.Write($"{table[x,y]} ");
- }
- Console.WriteLine();
- }
- }
- private void ShowResult()
- {
- try
- {
- table = GaussMethod.ExecuteMethod(table, nodes);
- Console.WriteLine("\nMacierz po rozwiazaniu:");
- ShowTable();
- List<double> row = new List<double>();
- row.Add(table[0,3]);
- row.Add(table[1,3]);
- row.Add(table[2,3]);
- Console.WriteLine($"\nS = {GaussMethod.CalculateS(user.returnFirstTable,user.returnSecondTable,row,level)}");
- }
- catch
- {
- Console.WriteLine("\nMacierz osobliwa");
- }
- }
- }
- public static class GaussMethod
- {
- static double max;
- static int row;
- public static double[,] ExecuteMethod(double[,] table,int nodes)
- {
- for(int column=0;column<nodes;column++)
- {
- max = table[column, column];
- row = column;
- for (int x = column; x < nodes; x++)
- {
- if (Math.Abs(table[x,column]) > Math.Abs(max))
- {
- max = table[x,column];
- row = x;
- }
- }
- if (Math.Abs(max) < 1e-30)
- {
- throw new Exception();
- }
- for (int x = column; x < nodes + 1; x++)
- {
- double temp = table[column, x];
- table[column, x] = table[row, x];
- table[row, x] = temp;
- }
- for (int x = column; x < nodes + 1; x++)
- {
- table[column,x] /= max;
- }
- for (int x = 0; x < nodes ; x++)
- {
- if (x != column)
- {
- double z = table[x, column];
- for (int y = column; y < nodes + 1; y++)
- {
- table[x, y] = table[x, y] - z * table[column, y];
- }
- }
- }
- }
- return table;
- }
- public static double CalculateS(double[] tab1, double[] tab2, List<double> row ,int level)
- {
- double result = 0;
- for (int x = 0; x < level; x++)
- {
- result += Math.Pow(((row[0] + row[1] * tab1[x] + row[2] * tab1[x]) - tab2[x]), 2);
- }
- return result;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- ProgramMatrix program = new ProgramMatrix();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement