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

Using a data file, save the formatted output on the screen to a data file for th

ID: 3815500 • Letter: U

Question

Using a data file, save the formatted output on the screen to a data file for the following code:

#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() {
calculateIntegeralBySimpson(40);
calculateIntegeralByTrapezoidal(100);
}

Explanation / Answer

Here is the modified code.

This will write to two files one for Simpon method and other for Trapezoidal method an

#include<stdio.h>
#include "math.h"
#include <stdlib.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) {
FILE *fp = fopen("data_Simpson_method.txt", "w");
  
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 */
fprintf(fp,"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 */
fprintf(fp,"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;
fprintf(fp,"solution to the given integration function is %.10f ",solution);
fclose(fp);

}

void calculateIntegeralByTrapezoidal(int n) {
FILE *fp = fopen("data_Trapezoidal_method.txt", "w");
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 */
fprintf(fp,"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 */
fprintf(fp,"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;
fprintf(fp,"solution to the given integration function is %.10f ",solution);
fclose(fp);

}
void main() {
calculateIntegeralBySimpson(40);
calculateIntegeralByTrapezoidal(100);
}

Please rate positively if this answered your query. Comment if something is not clear.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote