1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include<iostream>
#include<cmath>
#include<cstdlib>
using std::cout;
using std::cin;
using std::endl;
/*This function returns the value of the point on the graph of f(x) >>
ex. receives the value of the start point -- then returns the value of f(x) of that point.
then takes the value of the end point -- then returns the value of f(x) of that point*/
double calcPt(double x)
{
//returns the value of f(x) -- ONE POINT AT A TIME
return (sqrt(x-1)); //change this equation to whatever f(x) you want to evaluate.
}
/*This function calculates the area of 1 of the slices of the graph of f(x)*/
double trapArea(double pt1, double pt2, double deltaX)
{
//returns the area of 1 particular slice of the graph
return ((pt1+pt2)/2)*deltaX;
}
/*This function takes the points (the user inputs and calculated width) sent to it by the call from main()*/
void check_f1(double width, double start, double finish)
{
//initialize the value of each variable used in the calculation
double sum = 0, value1 = 0, value2 = 0, deltax = width;
/*for loop that starts at the first point, finishes at the end point and is incremented by >>
the width of each slice of the graph of f(x)*/
for(double k=start; k<finish; k+=width)
{
//sets value1 = to the value of f(x) of the first point
value1 = calcPt(start);
//sets value2 = the value of f(x) of the first point PLUS the width of the slice
value2 = calcPt(start+deltax);
//sets sum = the current sum PLUS the area calculated from the function trapArea
sum += trapArea(value1,value2,deltax);
//move the left-hand edge of the slice 'width' distance down the graph to the beginning of the next slice
start += deltax;
}
//print out the answer for the area under the curve of f(x) from a to b
cout << sum << " is the area under a curve for sqrt(x-1)" << endl;
}
int main()
{
int choice_f;
float a;
float b;
int n;
double delta;
cout << "Choose a function (1,2,3,4,5, other(quit)):" << endl;
cin >> choice_f;
if (choice_f == 1)
cout << "Please select a starting point, a: ";
cin >> a;
cout << "Please select a end point, b: ";
cin >> b;
cout << "How many trapezoids do you want? ";
cin >> n;
/*After the user inputs the operation they want to perform, we need to do some preliminary calculations to determine >>
the width of each slice of the graph that we want to analyze and send them to a function to finish the calculations*/
delta = (b - a)/n; //takes the inputs and determines the width of each 'slice'
//the check_f1 function is called and sent 3 parameters to run calculation based on user inputs
check_f1(delta,a,b); //a = starting point
//b = end point
//delta = width of each slice
return 0;
}
|