Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need the code in c that actually solves this function using the trapezoidal ru

ID: 3109629 • Letter: I

Question

I need the code in c that actually solves this function using the trapezoidal rule.

Then i need code in c to do it using simpsons method

that numerically integrates the integrates the following function 1000 e (7x) cos(0.3Tx), E L-5,01 f(x) x 0.23x 30.67 E [0,5] using N of different values, where Nis the number of steps or intervals. Print out all xi, fi f(xi) and the final result. Determine and print the appropriate value of N based on the results for different N values. Integrate the function by using both Trapezoidal rule and Simpson's rule, then compare the results with it calculated from mathematical software such as Matlab, Mathematica etc. Your C-code must contain the following features: (1) use of arrays, (2) use of pointers, 3) use of structure (4) use of union (5) use of functions and function calls, (6) formatted output on screen and (7) saving of the same output on a data file.

Explanation / Answer

(1) C CODE FOR TRAPEZOIDAL RULE:

IN above question there is two domain first xh=0 and xl=-5 then xh=5 and xl=0.So you have to apply trapezoidal rule two times and at the end add two values and get final integration value.

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f1(float x);
float f2(float x);
float f1(float x)
{
return(1000*exp(7*x)*cos(0.3*3.14*x));
}
float f2(float x)
{
return(pow(x,3)-0.23*x+30.67);
}
int main()
{
int i,n;
float s1=0.0,s2=0.0;
float sum,a1,b1,a2,b2,h1,h2;
printf("Enter upper limit for domain [-5,0]: = ");
scanf("%f",&b1);
printf("Enter lower limit for domain [-5,0]: = ");
scanf("%f",&a1);
printf("Enter upper limit for domain [0,5]: = ");
scanf("%f",&b2);
printf("Enter lower limit for domain [0,5]: = ");
scanf("%f",&a2);
printf("Enter the number of intervals = ");
scanf("%d",&n);
h1=(b1-a1)/n;
h2=(b2-a2)/n;
for(i=1;i<=n-1;i++)
{
s1=s1+f1(a1+i*h1);
s2=s2+f2(a2+i*h2);
}
sum=(f1(a1)+f1(b1)+2*s1)*h1/2+(f2(a2)+f2(b2)+2*s2)*h2/2;
printf("the value of y is=%f",sum);
getch();
}

(2) C CODE FOR SIMPSONS RULE:

IN above question there is two domain first xh=0 and xl=-5 then xh=5 and xl=0.So you have to apply Simpson's rule two times and at the end add two values and get final integration value.

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f1(float x);
float f2(float x);
float f1(float x)
{
return(1000*exp(7*x)*cos(0.3*3.14*x));
}
float f2(float x)
{
return(pow(x,3)-0.23*x+30.67);
}
int main()
{
int n,i;
float s1=0.0,s2=0.0,s3=0.0,s4=0.0;
float sum,a1,b1,a2,b2,h1,h2;
printf("Enter upper limit for domain [-5,0]: = ");
scanf("%f",&b1);
printf("Enter lower limit for domain [-5,0]: = ");
scanf("%f",&a1);
printf("Enter upper limit for domain [0,5]: = ");
scanf("%f",&b2);
printf("Enter lower limit for domain [0,5]: = ");
scanf("%f",&a2);
printf("Enter the number of intervals = ");
scanf("%d",&n);
h1=(b1-a1)/n;
h2=(b2-a2)/n;
if(n%2==0)
{
for(i=1;i<=n-1;i++)
{
if(i%2==0)
{
s1=s1+f1(a1+i*h1);
printf("f1[i] %f ",s1);
s3=s3+f2(a2+i*h2);
printf("f2[i] %f ",s3);
}
else
{
s2=s2+f1(a1+i*h1);
printf("f1[i] %f ",s2);
s4=s4+f2(a2+i*h2);
printf("f2[i] %f ",s4);
}
}
sum=(h1/3*(f1(a1)+f1(b1)+4*s2+2*s1))+(h2/3*(f2(a2)+f2(b2)+4*s3+2*s4));
printf("the value is = %f",sum);
}
else
{
printf("the rule is not appliciable");
}
getch();
}