you would like to find the area under the curve y=f(x) between the lines x=a and
ID: 3630368 • Letter: Y
Question
you would like to find the area under the curve y=f(x) between the lines x=a and x=b, which is . Trapezoidal rule and Simpson's method are two commonly used methods to approximate the area by dividing the area into V sub-divisions. Write a program to read in a, b and N, and calculate the approximate area under the curve f(x) = 2x2 +x between the lines x =a and x=b using Trapezoidal's rule and Simpson's method. you are required to use and implement the folowing functions in this code.
double f(double x); /* implement the function f(x)= 2x2 +x */
double trapezoid(double a, doubleb, int N); /*implement Trapezoidal's rule */
double simpson(double a, doubleb, int N); /*implement Simpson's rule */
1) Trapezoidal Rule
the area under the curve y=f(x) between two points x =a and x =b can be approximated using the trapezoidal rule as follows:
(1) Divide the area into N smaller strips, each of width h=(b-a)/N
(2) Connect two adjacent points on the curve separated by distance 'h' along x-axis
(3) the approximate area under the curve is equal to the sum of area of all N trapezoids
for each trapezoids; area= height*(base1+base2)/2
Total area= h/2(f0+f1)+ h/2(f1+f2)+....+h/2(fN-1 +fN)
where fi= value of f(a +i * h), for i=0, 1,.....,N
2) Simpson's Method:
although the trapezoidal rule can give a good approximation to the area under a curve when the number of sub-divisions 'N' is large number of calculations that arise as a result of this can often slow the calculations, particularly if f(x) is quit complex, Simpsons method, on the other hand offer similar accuracy with smaller 'N' by attempting to fit a parabolic curve, rather than a straight line between two points and the area is then given by the equation:
Total area= i=0N-2 h/3( fi +4fi+1 +fi+2) for all even values of i, ( i=0, 2, 4,.......N-2),
provided that N is an even number, where fi = calue of f(a+i *h), for i=0, 1, ........,N
Explanation / Answer
#include<stdio.h>
#include<math.h>
/* implement the function f(x)= 2x^2 +x */
double fn(double x)
{
double y;
y=2*x*x+x;
return(y);
}
/*implement Trapezoidal's rule */
double trapezoid(double a, double b, int n)
{
/*Tripazoidal rule*/
int i;
double h, sum, t, area;
h=(b-a)/n;
sum=0;
for(i=1;i<n;i++)
{
t=a+i*h;
sum=sum+(2*fn(t));
}
sum=sum+fn(a)+fn(b);
area=h*sum/2;
printf(" The area by tripazoidal rule =%lf ",area);
}
/*implement Simpson's rule */
double simpson(double a, double b, int n)
{
/* Simpsons rule for even segments*/
int i;
double h, sum, t, area;
printf("%lf",b-a);
h=(b-a)/n;
if(n%2==0)
{
sum=0;
for(i=1;i<n;)
{
t=a+(i*h);
sum=sum+(4*fn(t));
i=i+2;
}
for(i=2;i<n-1;)
{
t=a+(i*h);
sum=sum+(2*fn(t));
i=i+2;
}
sum=sum+fn(a)+fn(b);
area=h*sum/3;
printf(" The area by simpson`s rule = %lf.",area);
}
else
{
sum=0;
sum=h*(fn(a)+fn(b+h))/2;
for(i=2;i<n;)
{
t=a+i*h;
sum=sum+(4*fn(t));
i=i+2;
}
for(i=3;i<n-1;)
{
t=a+i*h;
sum=sum+(2*fn(t));
i=i+2;
}
sum=sum+fn(a)+fn(b);
area=h*sum/3;
printf(" The area by simpson`s rule = %lf.",area);
}
}
main()
{
int n;
double a,b;
double fn(double);
double trapezoid(double, double, int);
double simpson(double, double, int);
printf(" Enter the values of a,b,n : ");
scanf("%lf %lf %d",&a,&b,&n);
trapezoid(a,b,n);
simpson(a,b,n);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.