C++ code help, I need the results to go into an output file to tell the iterario
ID: 3791912 • Letter: C
Question
C++ code help, I need the results to go into an output file to tell the iterarions, integral value, and percent error.
n is the number of sub-intervals.
I have attached a link to what the rules are: http://www.ece.mcmaster.ca/~xwu/part6.pdf
I have attached what I have so far... I NEED OF HELP This is due in 24 hours
//Trapezoidal Rule
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
double function(double x)
{
double fun;
fun=(1.0 / (1 + exp(x)));
return fun;
}
/*
double pe(double num_ans)
{
double error, per_error,ana_ans;
ana_ans = 0.145413;
error = ((num_ans - ana_ans) / ana_ans); //% Error
per_error = error * 100;
return per_error;
}
*/
int main()
{
FILE* solution;
solution = fopen("Results_trapezoidal.txt", "w");
fprintf(solution, "NAME ");
fprintf(solution, " Homework 3 ");
fprintf(solution, " ");
fprintf(solution, "Integral: (1 / (1 + exp(x))); ");
fprintf(solution, " ");
//int n;
double a, b, h, integral, sum = 0, analytical, error, percent_e;
double x[512];
analytical =0.14541;
printf("Trapezoidal Rule ");
printf("Enter the inital limit of integration, a = ");
scanf("%lf", &a);
printf("Enter the final limit of integration, b = ");
scanf("%lf", &b);
string iteration("i");
string answer("x_i");
string p_error = ("Percent Error");
fprintf(solution, "Trapezoidal Rule ");
fprintf(solution, "------------------------------------------------------------------------------------- ");
fprintf(solution, "%2s%16s%26s ", iteration.c_str(), answer.c_str(), p_error.c_str());
fprintf(solution, "------------------------------------------------------------------------------------- ");
for (int n = 1; n <= 512; n*=2)
{
h = (b - a) / n;
x[0] = a;
for (int i = 1; i <= n; i++)
{
x[i] = x[i - 1] + h;
}
integral = 0.5*h*function(a);
integral += 0.5*h*function(b);
for (int j = 1; j <= n - 1; j++)
{
sum = function(x[j]) + sum;
}
integral += 0.5*h * 2 * sum;
fprintf(solution, "%2i ", n);
fprintf(solution, "%11.3lf ", integral);
error = (integral - analytical) / analytical;
percent_e = error * 100;
fprintf(solution, "%11.3lf ", percent_e);
}
fclose(solution);
return 0;
}
Explanation / Answer
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
double function(double x)
{
double fun;
fun=(1.0 / (1 + exp(x)));
return fun;
}
/*
double pe(double num_ans)
{
double error, per_error,ana_ans;
ana_ans = 0.145413;
error = ((num_ans - ana_ans) / ana_ans); //% Error
per_error = error * 100;
return per_error;
}
*/
int main()
{
FILE* solution;
solution = fopen("Results_trapezoidal.txt", "w");
fprintf(solution, "NAME ");
fprintf(solution, " Homework 3 ");
fprintf(solution, " ");
fprintf(solution, "Integral: (1 / (1 + exp(x))); ");
fprintf(solution, " ");
//int n;
double a, b, h, integral, sum = 0, analytical, error, percent_e;
double x[512];
analytical =0.14541;
printf("Trapezoidal Rule ");
printf("Enter the inital limit of integration, a = ");
scanf("%lf", &a);
printf("Enter the final limit of integration, b = ");
scanf("%lf", &b);
string iteration("i");
string answer("x_i");
string p_error = ("Percent Error");
fprintf(solution, "Trapezoidal Rule ");
fprintf(solution, "------------------------------------------------------------------------------------- ");
fprintf(solution, "%2s%16s%26s ", iteration.c_str(), answer.c_str(), p_error.c_str());
fprintf(solution, "------------------------------------------------------------------------------------- ");
for (int n = 1; n <= 512; n*=2)
{
h = (b - a) / n;
x[0] = a;
for (int i = 1; i <= n; i++)
{
x[i] = x[i - 1] + h;
}
integral = 0.5*h*function(a);
integral += 0.5*h*function(b);
for (int j = 1; j <= n - 1; j++)
{
sum = function(x[j]) + sum;
}
integral += 0.5*h * 2 * sum;
fprintf(solution, "%2i ", n);
fprintf(solution, "%11.3lf ", integral);
error = (integral - analytical) / analytical;
percent_e = error * 100;
fprintf(solution, "%11.3lf ", percent_e);
}
fclose(solution);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.