Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //example 5.9
- #include <bits/stdc++.h>
- using namespace std;
- float expression(float x)
- {
- return 1 / (1 + x);
- }
- float lower, upper, integration = 0.0, stepSize, k;
- int subInterval;
- float third()
- {
- for (int i = 1; i <= subInterval - 1; i++)
- {
- k = lower + i * stepSize;
- if (i % 2 == 0)
- {
- integration = integration + 2 * (expression(k));
- }
- else
- {
- integration = integration + 4 * (expression(k));
- }
- }
- integration = integration * stepSize / 3;
- return integration;
- }
- float eighth()
- {
- for (int i = 1; i <= subInterval - 1; i++)
- {
- k = lower + i * stepSize;
- if (i % 3 == 0)
- {
- integration = integration + 2 * (expression(k));
- }
- else
- {
- integration = integration + 3 * (expression(k));
- }
- }
- integration = integration * stepSize * 3.0 / 8.0;
- return integration;
- }
- float trapezoidal()
- {
- for (int i = 1; i <= subInterval - 1; i++)
- {
- k = lower + i * stepSize;
- integration = integration + 2 * (expression(k));
- }
- integration = integration * stepSize / 2;
- return integration;
- }
- int main()
- {
- cin >> lower >> upper >> subInterval;
- stepSize = (upper - lower) / subInterval;
- integration = expression(lower) + expression(upper);
- cout << "Required value of integration for 3rd rule is: " << third() << endl;
- cout << "Required value of integration for eighth rule is: " << eighth() << endl;
- cout << "Required value of integration for trapezoidal rule is: " << trapezoidal() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement