Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<math.h>
- using namespace std;
- float trapezy(float *fi, float h, int n)
- {
- float wynik=0;
- for(int i = 0; i <= n; i++)
- {
- if(i == 0 or i == n) wynik += 0.5 * fi[i];
- else wynik += fi[i];
- }
- return h*wynik;
- }
- float simpson(float *fi, float h, int n)
- {
- float wynik=0;
- for(int i = 0; i <= n; i++)
- {
- if(i == 0 or i == n) wynik += fi[i];
- else if(i%2 == 0) wynik += 2*fi[i];
- else wynik += 4*fi[i];
- }
- return (h/3)*wynik;
- }
- float funkcyjka(float x)
- {
- //return cos(x)*cos(x)+1;
- return 1/(1+pow(x,2));
- }
- void wartosci(float *fi, float a, float b, float h, int n)
- {
- float krok =0;
- for(int i=0; i <=n; i++)
- {
- fi[i] = funkcyjka(a+krok);
- cout << krok << " " << fi[i] << endl;
- krok +=h;
- }
- }
- int main()
- {
- int n;
- float h;
- //float a = 0;
- //float b = 2*3.14;
- float a = -1;
- float b = 1;
- cout << "Podaj n" << endl;
- cin >> n;
- h = (b-a)/n;
- float *fi = new float[n+1];
- wartosci(fi, a, b, h, n);
- cout << "Wedlug metody trapezow pi = " << 2 * trapezy(fi, h, n) << endl;
- if( n%2 == 0 )
- cout << "Wedlug Simpsona pi = " << 2 * simpson(fi, h, n) << endl;
- else cout << "n nie jest parzyste, Simpson nie zadziala" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement