Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <locale>
- #include <cmath>
- #include <iomanip>
- using namespace std;
- double function1(double x) {
- return (x - 2)*(x - 2) - 3;
- }
- double function2(double x, double s) {
- return cos(x)*cos(x) - pow(x, 1 / s);
- }
- void dichotomy(double a, double b, double (*function1)(double)) {
- int iterations = 0;
- cout << setw(10) << "x" << setw(15) << "F(x)" << setw(13) << "iterations " << endl;
- cout << "______________________________________________________" << endl;
- double x = (a + b) / 2;
- while (abs(function1(x))>0.000001) {
- iterations++;
- if (function1(x) < 0) {
- b = x;
- }
- else a = x;
- x = (a + b) / 2;
- }
- cout << setw(10) << x << setw(15) << function1(x) << setw(13) << iterations << endl;
- cout << "______________________________________________________ \n" << endl;
- }
- void dichotomy2(double a0, double b0, double(*function2)(double,double)) {
- double s0 = 1.95, sn = 2, ds = 0.01;
- for (double s = s0; s <= sn; s += ds) {
- double a = a0, b = b0;
- int iterations = 0;
- double x = (a + b) / 2;
- while (abs(function2(x, s))>0.000001) {
- iterations++;
- if (function2(x, s) < 0) {
- b = x;
- }
- else a = x;
- x = (a + b) / 2;
- }
- cout << setw(10) << "s" << setw(15) << "x" << setw(15) << "F(x)" << setw(13) << "iterations " << endl;
- cout << setw(10) << s << setw(15) << x << setw(15) << function2(x, s) << setw(13) << iterations << endl;
- }
- cout << "______________________________________________________" << endl;
- }
- int main()
- {
- dichotomy(-2.0, 1.0, function1);
- dichotomy2(0.0, 1.0, function2);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement