Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <clocale>
- #include <cmath>
- using namespace std;
- int n;
- double norm(double *y)
- {
- double max=0;
- for (int i=0;i<n;i++)
- {
- if (fabs(y[i])>max)
- max=fabs(y[i]);
- }
- return max;
- }
- double sp(double **A, double *x, double eps)
- {
- double maxl=0,l=0,ltemp=0,*xtemp=new double[n],*y=new double[n];
- int k=0;
- while (k<2)
- {
- for (int i=0;i<n;i++)
- x[i]=xtemp[i];
- for (int j=0;j<n;j++)
- {
- for (int i=0;i<n;i++)
- y[i]+=A[i][j]*x[j];
- }
- for (int i=0;i<n;i++)
- ltemp+=x[i]*y[i];
- for (int i=0;i<n;i++)
- xtemp[i]=y[i]/norm(y);
- k++;
- }
- while (fabs(l-ltemp)>eps)
- {
- for (int i=0;i<n;i++)
- x[i]=xtemp[i];
- for (int j=0;j<n;j++)
- {
- for (int i=0;i<n;i++)
- y[i]+=A[i][j]*x[j];
- }
- l=ltemp;
- ltemp=0;
- for (int i=0;i<n;i++)
- ltemp+=x[i]*y[i];
- for (int i=0;i<n;i++)
- xtemp[i]=y[i]/norm(y);
- k++;
- }
- cout<<"За "<<k<<" итераций"<<endl;
- return ltemp;
- }
- void main()
- {
- cout.precision(20);
- cout.width(20);
- system("cls");
- setlocale(LC_CTYPE,"Russian");
- system("Color 1F");
- double eps,l;
- cout<<"Введите эпсилон eps=";
- cin>>eps;
- cout<<"Введите размерность n=";
- cin>>n;
- double *x=new double [n];
- double **A=new double*[n];
- for (int i=0;i<n;i++)
- A[i]=new double[n];
- for (int i=0;i<n;i++)
- for (int j=0;j<n;j++)
- A[i][j]=360360*(i+j+1);
- cout<<"Матрица A: "<<endl;
- for (int i=0;i<n;i++)
- {
- for (int j=0;j<n;j++)
- cout<<A[i][j]<<"\t";
- cout<<endl;
- }
- /*cout<<"Введите произвольный вектор Yo "<<endl;
- for (int i=0;i<n;i++)
- cin>>x[i];*/
- for (int i=0;i<n;i++)
- {
- x[i]=0.57735;
- }
- l=sp(A,x,eps);
- cout<<"l="<<l<<endl;
- for (int i=0;i<n;i++)
- cout<<x[i]<<" ";
- }
Advertisement
Add Comment
Please, Sign In to add comment