For the integral below, write a program to do the trapezoid rule using the sequence of mesh sizes h = (b – a)/2, (b – a)/4, (b – a)/8, ..., (b – a)/128, where b – a is the length of the given interval: f(x) = e−x sin(4x), [0, pi], I(f) = (4/17)(1 − e^-pi) = 0.2251261368. Verify that the expected rate of error decrease is obtained. Attach your code and a plot of error vs. h.
Please use comments section for queries:
We write the C-Program using Function. We define a function where .
The code is as follows:
#include<stdio.h>
#include<math.h>
main()
{
int n,i;
float a,b,h,x,sum=0,int;
printf("Enter the number of subintervals");
scanf("%d",&n);
printf("Enter the lower limit of the integral");
scanf("%f",&a);
printf("Enter the upper limit of the integral");
scanf("%f",&b);
h=fabs(a-b)/n;
for(i=1,i<n;i++)
x=a+i*h;
sum+=f(x);
}
int=(0.5)*h*(f(a)+f(b)+2*sum);
printf("the integral is %f",int);
}
float f(float x)
{
return exp(-x)*sin(4*x);
}
Plot of error vs h:
Considering
we get the following graph:
Get Answers For Free
Most questions answered within 1 hours.