Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdio.h>
- double func(double x)
- {
- return x*(x+3);
- }
- double simp() {
- double a, b;
- int m = 10;
- printf("Input a\n");
- scanf("%lf", &a);
- printf("Input b\n");
- scanf("%lf", &b);
- double h = (b - a) / m;
- double sum = 0;
- double x0 = a;
- double x1 = a + h;
- for (int i=0; i<=m-1; i++) {
- sum += (h/3)*(2*func(x0 + h/2) + (func(x0)+ func(x1))/2);
- x0 += h;
- x1 += h;
- }
- return sum;
- }
- double rect()
- {
- double a, b;
- printf("Input a\n");
- scanf("%lf", &a);
- printf("Input b\n");
- scanf("%lf", &b);
- int m=10, eps=0.0001;
- double sum = 0.0, t=0.0, tp=eps+1;
- double h = ((b-a)/m);
- for (double x = a; x <=b; x+=h, fabs(t-tp)>eps){
- sum += t;
- tp = t;
- t= h*(func(x-h/2));
- }
- return sum;
- }
- int main()
- {
- int w = 1;
- while(w==1){
- int inp;
- double ans=0;
- printf("Please choose what to execute: 1- Simpson's method, 2 - Rectangles\n");
- scanf("%d", &inp);
- switch (inp)
- {
- case 1:
- {
- ans = simp();
- printf("%f\n",ans);
- break;
- }
- case 2:
- {
- ans = rect();
- printf("%f\n", ans);
- break;
- }
- default:
- {
- printf("Input right value!\n");
- break;
- }
- }
- printf("Continue?\n");
- scanf("%d",&w);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement