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 ConsoleApp1
- {
- class Program
- {
- //ilosc punktow
- public static int ilosc_pkt = 5-1;
- //2
- public static int stopien_wielomianu = 5;
- //punkt do liczenia
- public static double punkt = 2;
- //punkty z mojego przykladu na sztywno
- //public static double[] pkt_x = { -1, -0.5, 0, 0.5, 1 };
- //public static double[] pkt_y = { 2.65, 1.8, 1.42, 1.8, 2.65 };
- public static double[] pkt_x = {1,2,3,4};
- public static double[] pkt_y = {6,19,40,69};
- static void Main(string[] args)
- {
- //macierz ktora bede wypelniac sn
- double[,] macierz_moja_sn = new double[ilosc_pkt,ilosc_pkt];
- double[] macierz_moja_T = new double[ilosc_pkt];
- double[] macierz_wsp_a = new double[ilosc_pkt];
- for(int i = 0; i < macierz_moja_sn.GetLength(0);i++)
- {
- for(int j = 0; j < macierz_moja_sn.GetLength(1);j++)
- {
- macierz_moja_sn[i, j] = policz_sumke_skj(i, j);
- }
- }
- foreach(double n in macierz_moja_sn)
- {
- Console.WriteLine(n);
- }
- Console.ReadKey();
- //tutaj licze t
- for(int a = 0;a < macierz_moja_T.GetLength(0);a++)
- {
- macierz_moja_T[a] = policz_sumke_tk(a);
- }
- //policzyc gaussem wspolczynniki
- macierz_wsp_a=GaussElimination(macierz_moja_sn, macierz_moja_T, macierz_moja_T.GetLength(0));
- Console.WriteLine("--------------wspolczynniki----------");
- for(int b = 0; b < macierz_wsp_a.Length;b++)
- {
- Console.WriteLine(macierz_wsp_a[b]);
- }
- Console.ReadKey();
- //konczymy impreze
- Console.WriteLine(koniec(macierz_wsp_a, punkt));
- Console.ReadKey();
- }
- //zaczynam metody statyczne do wspolczynnikow
- // policzmy teraz sume zeby wypelnic macierz
- public static double policz_sumke_skj(int i, int j )
- {
- //dobrze juz liczy skj
- double var = 0;
- int potega = i + j;
- for(int k = 0; k <=ilosc_pkt;k++)
- {
- var += Math.Pow(pkt_x[k], potega);
- }
- return var;
- }
- public static double policz_sumke_tk(int j)
- {
- //dobrze juz liczy
- double var = 0;
- for(int i =0;i<=ilosc_pkt;i++)
- {
- var += Math.Pow(pkt_x[i], j) * pkt_y[i];
- }
- Console.WriteLine("tk" + var);
- Console.ReadKey();
- return var;
- }
- //koncowa wartosc do policzenia
- public static double koniec(double[]macierz,double pkt)
- {
- int stp = stopien_wielomianu;
- double wartosc_w_pkt = 0;
- for(int i = 0; i < macierz.Length;i++)
- {
- //macierz tutaj robi za wspolczynniki
- wartosc_w_pkt += macierz[i] * Math.Pow(pkt, i);
- }
- return wartosc_w_pkt;
- }
- //elimacja gaussa
- public static double[] GaussElimination(double[,] A, double[] b, int n)
- {
- double[] x = new double[n];
- double[,] tmpA = new double[n, n + 1];
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- tmpA[i, j] = A[i, j];
- }
- tmpA[i, n] = b[i];
- }
- double tmp = 0;
- for (int k = 0; k < n - 1; k++)
- {
- for (int i = k + 1; i < n; i++)
- {
- tmp = tmpA[i, k] / tmpA[k, k];
- for (int j = k; j < n + 1; j++)
- {
- tmpA[i, j] -= tmp * tmpA[k, j];
- }
- }
- }
- for (int k = n - 1; k >= 0; k--)
- {
- tmp = 0;
- for (int j = k + 1; j < n; j++)
- {
- tmp += tmpA[k, j] * x[j];
- }
- x[k] = (tmpA[k, n] - tmp) / tmpA[k, k];
- }
- return x;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement