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

F(x) = sin(3x)/x elementof [-infinity, infinity] using N of different values, wh

ID: 3817900 • Letter: F

Question

F(x) = sin(3x)/x elementof [-infinity, infinity] 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

#include<stdio.h>
#include "math.h"

double F1(double x)
{
    return sin(3*x)/x;
}


void calculateIntegeralBySimpson(int n, double lowerLimit, double upperLimit) {

    if (n%2==0) {
       printf("N must be odd as this function cannot be computed at zero value ");
        return;  
    }

    int i;
    double x[n+1], y[n+1];
    double h = (upperLimit - lowerLimit) / n;

    printf("Computing integral using Simpson method ");   
    for(i=0; i<=n; i++) {
        x[i]=lowerLimit + (i*h);
        y[i]=F1(x[i]);   /* Call first function */
       printf("x, f(x) = (%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 solution = (h/3) * (y[0] + y[n] + 4*sumOfOdds + 2*sumOfEvens);
    printf("solution to the given integration function is %.10f ",solution);
}


void calculateIntegeralByTrapezoidal(int n, double lowerLimit, double upperLimit) {

    if (n%2==0) {
       printf("N must be odd as this function cannot be computed at zero value ");
        return;  
    }

    int i;
    double x[n+1], y[n+1];
    double h = (upperLimit - lowerLimit) / n;
    double sum = 0;
  
    printf("Computing integral using Simpson method ");
    for(i=0; i<=n; i++) {
        x[i]=lowerLimit + (i*h);
        y[i]=F1(x[i]);
       printf("x, y = (%f, %f) ",x[i],y[i]);
       sum = sum+y[i];
    }

    double solution = (y[0] + y[n] + 2*sum)*(h/2.0) ;
    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
      * excellent approximation is obtained at N = 91.
   *
   * Trapezoidal's Method
   * I found out that the value at N ~ 1001 to get a good approximation
   *
      * To get a more precise value we can increase the value of N    
   *
   * NOTE : Always keep the value of n odd
      */
   calculateIntegeralBySimpson(91, -5, 5);
   calculateIntegeralByTrapezoidal(1001, -5, 5);
}