Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <cstdlib>
- using namespace std;
- double liniowa (double x)
- {
- return exp(-x)*(21*x-37);
- }
- double kwadrat (double x)
- {
- return exp(-x)*2*x*x+3*x-5;
- }
- double tryg (double x)
- {
- return exp(-x)*cos(x)*4;
- }
- double zlozenie (double x)
- {
- return exp(-x)*fabs(x*x*sin(x));
- }
- double wartosci(double x, int wybor, int wyborwaga, double waga)
- {
- double wynik;
- if(wybor==1)
- wynik=liniowa(x);
- else if (wybor==2)
- wynik=kwadrat(x);
- else if (wybor==3)
- wynik=tryg(x);
- else
- wynik=zlozenie(x);
- if(wyborwaga==1)
- return wynik/exp(-x)*waga;
- else
- return wynik;
- }
- double wartosci1(double x, int wybor)
- {
- if(wybor==1)
- return liniowa(x)/exp(-x);
- else if (wybor==2)
- return kwadrat(x)/exp(-x));
- else if (wybor==3)
- return tryg(x)/exp(-x);
- else
- return zlozenie(x)/exp(-x);
- }
- void simpson (double a, double b, int wybor, int wybor2, double &wynik, double &wynik1, int &n)
- {
- double dx, suma2, suma, x;
- int i;
- n++;
- wynik+=wynik1;
- dx=(b-a)/(n);
- suma2=0;
- suma=0;
- for (i=1; i<n; i++)
- {
- x=a+i*dx;
- suma+=wartosci1(x-dx/2, wybor);
- suma2+=wartosci1(x, wybor);
- }
- suma+=wartosci1(b-dx/2, wybor);
- if (wybor2==1)
- wynik1=((b-a)/(6*n))*(wartosci1(a, wybor)+wartosci1(b, wybor)+2*suma2+4*suma);
- else
- wynik1=((b-a)/(6*n))*(wartosci1(a, wybor)+wartosci1(b, wybor)+2*suma2+4*suma);
- }
- void laguerre (int wybor, double wezly[], double siema[], int n, double &wynik, double &wynik1, int wyborwaga, double waga)
- {
- wynik=wynik1;
- wynik1=0;
- for (int i=0; i<=n; i++)
- wynik1+=siema[i]*wartosci(wezly[i], wybor, wyborwaga, waga);
- }
- int main()
- {
- double a, b, eps, wynik=0, wynik1=0;
- int wybor, wybor2, wyborwaga, n=1;
- double waga=1;
- double ile=0;
- double x1[2]= {0.585789, 3.414214};
- double x2[3]= {0.415775, 2.294280, 6.289945};
- double x3[4]= {0.322548, 1.745761, 4.536620, 2.395071};
- double x4[5]= {0.263560, 1.413403, 3.596426, 7.085810, 12.640801};
- double a1[2]= {0.853553, 0.146447};
- double a2[3]= {0.711093, 0.278518, 0.010389};
- double a3[4]= {0.603154, 0.357419, 0.038888, 0.000539};
- double a4[5]= {0.521756, 0.398667, 0.075942, 0.003612, 0.000023};
- double *X[4]= {x1, x2, x3, x4};
- double *A[4]= {a1, a2, a3, a4};
- cout<<"Aby wybrac wlasna wage w obliczeniach metoda Laguerre'a wcisnij 1, jesli nie wpisz cokolwiek innego"<<endl;
- cin>>wyborwaga;
- if(wyborwaga==1)
- {
- cout<<"podaj wage"<<endl;
- cin>>waga;
- }
- cout<<"Wybierz funkcje:"<<endl<<"1. funkcja liniowa"<<endl<<"2. wielomian"<<endl<<"3. funkcja trygonometryczna"<<endl<<"4. zlozenie"<<endl;
- cin>>wybor;
- if(wybor>=1 && wybor<=4)
- {
- cout << "1. Okreslony przedzial calkowania. \n2. Przedzial calkowania [0, inf]." << endl;
- cin >> wybor2;
- if (wybor2==1)
- {
- cout << "Okresl przedzial: " << endl;
- cout << "Poczatek przedzialu: ";
- cin >> a;
- cout << "Koniec przedzialu: ";
- cin >> b;
- cout << "Podaj dokladnosc: ";
- cin >> eps;
- cout << endl;
- a=0;
- wynik1=0;
- do
- {
- simpson(a,b,wybor,wybor2,wynik,wynik1,n);
- ile++;
- }
- while (wynik1>eps);
- }
- else if (wybor2==2)
- {
- ile=0;
- cout << "Podaj dokladnosc: ";
- cin >> eps;
- cout << endl;
- a=0;
- wynik1=0;
- do
- {
- simpson(a,a+1,wybor,wybor2,wynik,wynik1,n);
- a+=1;
- ile++;
- }
- while (fabs(wynik-wynik1)>eps);
- }
- cout<<"Wynik metoda Newtona-Cotesa: "<<wynik1<<endl<<"Dla liczby przedzialow "<<ile<<endl;
- int m=1;
- wynik1=0;
- do
- {
- laguerre(wybor,X[m-1],A[m-1],m,wynik,wynik1,wyborwaga,waga);
- m++;
- }
- while (fabs(wynik1-wynik)>eps and m<=4);
- cout<<"Wynik metoda Laguerre'a: "<<wynik1<<endl<<"Dla liczby wezlow n = "<<m<<endl;
- }
- else
- cout<<"Nie ma takiej funkcji :<"<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement