Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- double F(double x)
- {
- return x - sin(x);
- }
- double integral(double a, double b)
- {
- return (pow(b,2)/2 + cos(b)) - (pow(a,2)/2 + cos(a));
- }
- double solve(double a, double b, int n)
- {
- int i;
- double dx, sum = 0, lastF, nextF;
- dx = (b-a)/n;
- lastF = F(a);
- for(i = 1; i <= n; i++)
- {
- nextF = F(a + i*dx);
- sum += ((lastF + nextF)/2)*dx;
- lastF = nextF;
- }
- return sum;
- }
- int main() {
- int n;
- double a, b, result;
- printf("Enter number of segments: ");
- scanf("%d", &n);
- printf("\n");
- printf("Enter range (0 9.81): ");
- scanf("%lf %lf", &a, &b);
- printf("\n");
- printf("n = %d a = %lf b = %lf\n\n", n, a, b);
- result = solve(a, b, n);
- printf("result %lf\n", result);
- printf("real integral %lf\n", integral(a, b));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement