Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<iostream>
- #include<math.h>
- #include<windows.h>
- #include<time.h>
- using namespace std;
- bool choice;
- double
- GetPerformanceCounter()
- {
- LARGE_INTEGER li;
- QueryPerformanceFrequency(&li);
- double
- freq =
- double
- (li.QuadPart) / 1000.0;
- QueryPerformanceCounter(&li);
- return
- double
- (li.QuadPart) / freq;
- }
- float funkcja(float x)
- {
- float y;
- if (!choice)
- y = 2*x*x + 5 * x + 2;
- else
- y = fabs(sin(x));
- return y;
- }
- int main()
- {
- cout << "[0] Wielomian" << endl << "[1] Sinus " << endl;
- cin >> choice;
- double start, stop;
- double czas;
- int n;
- float y[300], x[300];
- float przedzial, a, b, x0, F = 0, h;
- cout << "Podaj liczbe wezlow" << endl;
- cin >> n;
- cout << "Podaj a" << endl;
- cin >> a;
- cout << "Podaj b" << endl;
- cin >> b;
- cout << endl;
- przedzial = fabs(b - a) / (n - 1);
- x0 = a;
- for (int i = 0; i <= n - 1; i++)
- {
- y[i] = funkcja(x0);
- x[i] = x0;
- x0 += przedzial;
- }
- start = GetPerformanceCounter();
- for (int i = 1; i <= n - 1; i++){
- h = (x[i] - x[i - 1]);
- F += (y[i] + y[i - 1])*h / 2.0;
- }
- stop = GetPerformanceCounter();
- czas = stop - start;
- cout << "Czas wykonania obliczeń = " << endl;
- cout << czas << endl;
- cout << "Oto otrzymane wartosci : " << endl;
- cout << "Metoda Trapezow: " << F << endl;
- F = 0;
- start = 0;
- stop = 0;
- czas = 0;
- start = GetPerformanceCounter();
- for (int i = 1; i <= n - 1; i++)
- {
- h = (x[i] - x[i - 1]) / 2.0;
- F += (h / 3.0)*(y[i] + 4.0*funkcja((x[i] + x[i - 1]) / 2.0) + y[i - 1]);
- }
- stop = GetPerformanceCounter();
- czas = stop - start;
- cout << "Czas wykonania obliczeń = " << endl;
- cout << czas << endl;
- cout << "Metoda Simpsona:" << F << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement