Develop a C-code that numerically integrates the following function f(x) = {1000
ID: 3805829 • Letter: D
Question
Develop a C-code that numerically integrates the following function f(x) = {1000 * e^(7x) * cos(0.3 pi x), elementof [-5, 0] x^3 - 0.23x + 30.67, elementof [0, 5] using N of different values, where N is the number of steps or intervals. Print out all x_i f_i = f(x_i) 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
NOTE : From other tools (MATLAB) we found out that the integration value of given function is 447.039
NOTE : I have commented out print statements for x, f(x). Please uncomment them
#include<stdio.h>
#include "math.h"
double F1(double x)
{
return 1000 * exp(7*x) * cos(0.3*M_PI*x);
}
double F2(double x)
{
return pow(x,3) - (0.23*x) + 30.67;
}
void calculateIntegeralBySimpson(int n) {
int i;
/* Calculating for first half of the interval */
double lowerLimit = -5, upperLimit = 0, x[n+1], y[n+1];
double h = (upperLimit - lowerLimit) / n;
for(i=0; i<=n; i++) {
x[i]=lowerLimit + (i*h);
y[i]=F1(x[i]); /* Call first function */
// printf("x, y = (%f, %f) ",x[i],y[i]);
}
double sumOfOdds=0;
double sumOfEvens=0;
for(i=1; i<n; i++) {
if(i%2==1) {
sumOfOdds=sumOfOdds+y[i];
}
else {
sumOfEvens=sumOfEvens+y[i];
}
}
double firstIntervalSol = (h/3) * (y[0] + y[n] + 4*sumOfOdds + 2*sumOfEvens);
/*
* Calculating for second half interval
*/
lowerLimit = 0, upperLimit = 5;
h = (upperLimit - lowerLimit) / n;
for(i=0; i<=n; i++) {
x[i]=lowerLimit + (i*h);
y[i]=F2(x[i]); /* Call second function */
// printf("x, y = (%f, %f) ",x[i],y[i]);
}
sumOfOdds=0;
sumOfEvens=0;
for(i=1; i<n; i++) {
if(i%2==1) {
sumOfOdds=sumOfOdds+y[i];
}
else {
sumOfEvens=sumOfEvens+y[i];
}
}
double secondIntervalSol = (h/3) * (y[0] + y[n] + 4*sumOfOdds + 2*sumOfEvens);
double solution = firstIntervalSol + secondIntervalSol;
printf("solution to the given integration function is %.10f ",solution);
}
void calculateIntegeralByTrapezoidal(int n) {
int i;
/* Calculating for first half of the interval */
double lowerLimit = -5, upperLimit = 0, x[n+1], y[n+1];
double h = (upperLimit - lowerLimit) / n;
double sum = 0;
for(i=0; i<=n; i++) {
x[i]=lowerLimit + (i*h);
y[i]=F1(x[i]); /* Call first function */
// printf("x, y = (%f, %f) ",x[i],y[i]);
sum = sum+y[i];
}
double firstIntervalSol = (y[0] + y[n] + 2*sum)*(h/2.0) ;
/*
* Calculating for second half interval
*/
lowerLimit = 0, upperLimit = 5;
h = (upperLimit - lowerLimit) / n;
sum = 0;
for(i=0; i<=n; i++) {
x[i]=lowerLimit + (i*h);
y[i]=F2(x[i]); /* Call second function */
// printf("x, y = (%f, %f) ",x[i],y[i]);
sum = sum+y[i];
}
double secondIntervalSol = (y[0] + y[n] + 2*sum)*(h/2.0);
double solution = firstIntervalSol + secondIntervalSol;
printf("solution to the given integration function is %.10f ",solution);
}
void main() {
/* Simpson's Method
* After running for different values of N, I found out that a
* good approximation is obtained at N = 40
*
* Trapezoidal's Method
* We found out that the value of N>=7000 to get a good approximation
*
* To get a more precise value we can increase the value of N
*/
calculateIntegeralBySimpson(40);
calculateIntegeralByTrapezoidal(15000);
}
Output :
solution to the given integration function is 447.4457464763
solution to the given integration function is 447.4336892507
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.