Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #define _USE_MATH_DEFINES
- #include <iostream>
- #include <cmath>
- using namespace std;
- double f(double x);
- double metoda_trapezow(double a, double b, int n);
- double metoda_simpsona(double a, double b, int n);
- double metoda_kwadratur(double a, double b, int n);
- int main()
- {
- cout << metoda_trapezow(0, M_PI, 2000) << endl;
- cout << metoda_simpsona(0, M_PI, 1000) << endl;
- cout << metoda_kwadratur(0, M_PI, 400) << endl;
- return 0;
- }
- double f(double x) {
- return sin(x);
- }
- double metoda_trapezow(double a, double b, int n) {
- double h = (b - a) / n;
- double sum = 0;
- double podstawa_1 = f(a);
- double podstawa_2;
- for (int i = 0; i < n; i++) {
- podstawa_2 = f(a + h * i);
- sum += (podstawa_1 + podstawa_2);
- podstawa_1 = podstawa_2;
- }
- return 0.5 * sum * h;
- }
- double metoda_simpsona(double a, double b, int n) {
- double h = (b - a) / n;
- double xi = a;
- double xi1, ti;
- double sum = 0;
- for (int i = 0; i < n; i++) {
- xi1 = a + i * h;
- ti = (xi + xi1) / 2;
- sum += ((xi1 - xi) / 6) * (f(xi) + f(xi1) + 4 * f(ti));
- xi = xi1;
- }
- return sum;
- }
- double metoda_kwadratur(double a, double b, int n) {
- int ng = 1;
- double h = (b - a) / (n - 1);
- double psi[2];
- psi[0] = -sqrt(1 / 3); psi[1] = -psi[0];
- double w[2];
- w[0] = 1.0; w[1] = 1.0;
- double calka = 0; double sum = 0;
- double xi = a;
- double xi1;
- for (int i = 0; i < n; i++) {
- sum = 0;
- xi1 = a + i * h;
- for (int j = 0; j <= ng; j++) {
- psi[j] = ((xi1 - xi) / 2) * f((xi1 + xi) / 2 + (xi1 - xi) / 2 * psi[j]);
- sum += psi[j] * w[j];
- }
- xi = xi1;
- calka += sum;
- }
- return calka;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement