PROGRAMMING IN C Exact area under the curve created by function f(x) in interval
ID: 671280 • Letter: P
Question
PROGRAMMING IN C
Exact area under the curve created by function f(x) in interval [a,b] is given by: excat area = f (x)dx a b For an interval divided into n equally spaced trapezoids, a = x1 < x2 < ... < xn+1 = b, where the grid spacing is h=(b-a)/n, the trapezoidal rule approximation to the integral becomes approximate area = f (x)dx h 2 ( f (xi+1) i=1 n a b + f (xi)) As the number of trapezoids increases the approximate result converges to the exact result. Write a program which: 1. Prints out all of the required information for all assignments (see style sheet in D2L) 2. Which gets an interval from the user (i.e., values for a and for b). And gets the number n of trapezoids. 3. Has a function that calculates the exact integral over the interval ab for the cosine function 4. Has a function that calculates the area of a trapezoid given x1,y1 and x2,y2 (the other two points on the trapezoid can be assumed to be x1,0 and x2,0). 5. Has a function that calculates the area under the curve for the given interval of the cosine function using n trapezoids 6. Write your program so that it outputs the interval entered, the exact solution, the approximation, the number of trapezoids and the difference between the exact and approximate solutions.
Explanation / Answer
Your question is not clear. and based on your given question please find the code below
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main ()
{
float a,b;
float trapizo;
float Intergral;
float cosine=0;
float Area;
printf("Enter values for a and for b ");
scanf("%d,%d", &a,&b);
printf("Enter the Number of trapezoids ");
scanf("%d", &trapizo);
Intergral = CalculateIntegral(cosine,a,b,n);
Area= TrapezoidArea(a,b,n);
printf("Area of the Trapezoid is :",Area);
}
float CalculateIntegral(float (*f)(float x), float a, float b, int n);
int i;
double x;
double k = (b - a) / n;
double s = 0.5 * (f(a) + f(b));
for (i = 1; i < n; i++) {
x = a + k * i;
s = s + f(x);
}
return s * k;
}
float TrapezoidArea(float a, float b, int n)
{
float h,area;
h=(b-a)/n;
area = 0.5 * (a + b) * h ;
return area;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.