Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- najwiekszy moduł:
- wyjście: lamba, x, N
- zbieżność ?
- N = f (eps)
- czy zbieżność zależy od y0
- Kryłow: - rozmiar n
- y0,..,yn -> ukl. rownan -> p1,...,pn -> r char -> lamba -> X
- najlepiej Gauss/Kramer
- A = [2 -2 3; 1 1 1; 1 3 -1];
- B - [3 -4 3; -4 6 3; 3 3 1];
- C = [3 30 -48; 3 14 -24; 3 15 -25];
- ROZWIAZANIE ANALITYCZNE BY WOLFRAM!
- */
- #include <iostream>
- #include <cmath>
- #include <vector>
- using namespace std;
- vector<float> Gauss(float** in, int n);
- int main()
- {
- const int n = 3; // Rzad macierzy
- float A[n][n]={{1,1,1},{1,2,3},{1,3,6}};//{{2, -2, 3}, {1 ,1 ,1}, {1,3,-1}};
- float *y0 = new float[n];
- float Y[n][n+1];
- Y[0][0] = y0[0] = 1;
- for(int i=1;i<n;i++)
- Y[i][0] = y0[i] = 0;
- float* yp = y0;
- float* y = new float[n]; //nowy wektor y
- for(int k=1;k<n+1;k++)
- {
- for(int i=0;i<n;i++)
- y[i] = 0;
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<n;j++)
- y[i] += A[i][j]*yp[j];
- Y[i][k] = y[i];
- }
- float* temp = yp;
- yp = y;
- y = temp;
- }
- delete[] y,y0;
- for(int i=0;i<n;i++)
- Y[i][n] *=-1;
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<n+1;j++)
- cout<<Y[i][j]<<' ';
- cout<<'\n';
- }
- float** C = new float*[n];
- for(int i=0;i<n;i++)
- {
- C[i] = new float[n+1];
- for(int j=0;j<n+1;j++)
- C[i][j] = Y[i][j];
- }
- vector<float> out;
- try{
- out = Gauss(C,n);
- }
- catch(int x)
- {
- if(x == 1)
- cout<<"Nie ma pojedynczego rozwiazania ukladu\n";
- }
- for(int i=0;i<n;i++)
- cout<<out[i]<<' ';
- for(int i=0;i<n;i++)
- delete[] C[i];
- delete[] C;
- system("pause");
- return 0;
- }
- vector<float> Gauss(float** in, int n)
- {
- vector<float> out;
- float** A = new float*[n];
- for(int i=0;i<n;i++)
- {
- A[i] = new float[n + 1];
- for(int j=0;j<n+1;j++)
- A[i][j] = in[i][j];
- }
- for(int s=0;s<n;s++) // dla kazdego wiersza
- {
- if(A[s][s] == 0) //Nalezy zamienic kolejnosc wierszy
- {
- int w = -1;
- for(int i=s+1;i<n;i++)
- if(A[i][s] != 0)
- {
- w = i;
- break;
- }
- if(w == -1)
- throw 1;
- float temp;
- for(int i=0;i<n;i++)
- {
- temp = A[s][i];
- A[s][i] = A[w][i];
- A[w][i] = temp;
- }
- }
- for(int i=s+1;i<n;i++) //dla każdego niższego wiersza
- {
- for(int j=0;j<n + 1;j++) //dla każdego elementu
- if(j!= s)
- A[i][j] -= A[s][j] * A[i][s]/A[s][s];
- A[i][s] = 0;
- }
- }
- for(int s=n-1;s>0;s--) // dla kazdego wiersza
- {
- for(int i=0;i<s ;i++) //dla każdego wyższego wiersza
- {
- for(int j=s;j<n + 1;j++) //dla każdego elementu
- //cout<<s<<' '<<i<<' '<<j<<'\n';
- if(j!= s)
- A[i][j] -= A[s][j] * A[i][s]/A[s][s];
- A[i][s] = 0;
- }
- }
- for(int i=0;i<n;i++)
- out.push_back(A[i][n] / A[i][i]);
- for(int i=0;i<n;i++)
- delete[] A[i];
- delete[] A;
- return out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement