Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <locale>
- #include <math.h>
- using namespace std;
- int iterations;
- int iterations2;
- double function(double x) {
- return (x-1)*(x-1) - 3;
- }
- int derivative(double x) {
- return 2 * x - 2;
- }
- double function2(double x) {
- return cos(x)*cos(x)-pow(x, 1/5);
- }
- void dichotomy(double a, double b) {
- double x = (a + b) / 2;
- while (abs(function(x))>0.000001) {
- iterations ++;
- if (function(x) < 0) {
- b = x;
- }
- else a = x;
- x = (a + b) / 2;
- }
- cout << "x0: "<< x << endl;
- }
- int СhordMethod(double a, double b) {
- double t;
- while (fabs(b - a) >= 0.000001) {
- iterations2++;
- t = a + (function(b)*(b - a)) / (function(b) - function(a));
- if (function(a)*function(t)<0) {
- b = t;
- }
- else if (function(t)*function(b)<0) {
- a = t;
- }
- else return t;
- }
- cout << "x0: " << t<<endl;
- return t;
- }
- int main()
- {
- dichotomy(-2.0, 1.0);
- cout << "Number of iterations: " << iterations << endl;
- СhordMethod(-2.0, 1.0);
- cout << "Number of iterations: " << iterations2 << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement