In this lab, we are going to find the area under a curve using rectangles. We wi
ID: 3786762 • Letter: I
Question
In this lab, we are going to find the area under a curve using rectangles. We will use n rectangles between two points on the x axis to estimate the area under the curve at this location. Background/Example: Rectangle Numerical Method: a C1 x2 x3 x4 b n number of rectangles (user input) a beginning x value (user input) b ending x value (user input) w width of each rectangle, (b-a)/n f(x) height of rectangle Area width height What are you using for the x value to send to the function, f(x), e.g. a, x1, x2, etc. in the above picture? o What is the initial value of x? o How will you determine the subsequent values? What kind of loop are you using? Construct this loop. o What are the loop counter values and condition? o What statements are in the loop?Explanation / Answer
The Program to calculate numerical area under 2x
#include <iostream>
using namespace std;
double f(double x){ return 2*x;} // the function f
double area(double a,double b, int n); // prototype of area function
int main()
{
double Ar,a,b;
int n;
// getting information from user
cout << "Enter the value of begining x point :";
cin >> a;
cout << "Enter the value of ending x point :";
cin >> b;
cout <<"Enter the number of rectangles :";
cin >> n;
Ar = area(a,b,n); // calling function area to calculate the area
cout << " The Numerical area = "<< Ar << endl; // printing the result
return 0;
}
double area(double a,double b,int n) // the function area
{
double sum=0.0,w = (b-a)/n;
int k;
for(k =0;k<n;k++)
{
sum += f(a+k*w)*w; // calculating individual rectangle area and adding
}
return sum; // return the result
}
OUTPUT
[dark_rose@localhost CHEGG]$ ./a.out
Enter the value of begining x point :0
Enter the value of ending x point :1
Enter the number of rectangles :1000
The Numerical area = 0.999
[dark_rose@localhost CHEGG]$ ./a.out
Enter the value of begining x point :0
Enter the value of ending x point :1
Enter the number of rectangles :1000000
The Numerical area = 0.999999
The Quadratic program
#include <iostream>
#include<math.h>
using namespace std;
void quadratic(double a,double b, double c); // prototype of quadratic function
int main()
{
double a,b,c; // variables
// getting information from user
cout << "Enter the value of a :";
cin >> a;
cout << "Enter the value of b :";
cin >> b;
cout <<"Enter the value of c :";
cin >> c;
quadratic(a,b,c); // calling function quadratic to calculate x
return 0;
}
void quadratic(double a,double b,double c) // the function quadratic
{
double rt = b*b - 4*a*c,real = -b/(2*a);
if(rt <0) // b^2 - 4ac is less than zero
{
cout << "x1 = " << real << "+" << sqrt(-1*rt) << "i" << endl;
cout << "x2 = " << real << "-" << sqrt(-1*rt) << "i"<< endl;
}
else // if b^2-4ac is greater than zero
{
cout << "x1 = " << real+sqrt(rt) << endl;
cout << "x2 = " << real-sqrt(rt) << endl;
}
}
OUTPUT
[dark_rose@localhost CHEGG]$ ./a.out
Enter the value of a :2
Enter the value of b :2
Enter the value of c :1
x1 = -0.5+2
x2 = -0.5-2
[dark_rose@localhost CHEGG]$ ./a.out
Enter the value of a :2
Enter the value of b :5
Enter the value of c :2
x1 = 1.75
x2 = -4.25
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.