All pastes #2054782 Raw Edit

Mine

public text v1 · immutable
#2054782 ·published 2011-05-07 06:26 UTC
rendered paste body
#include <iostream>
#include <cmath>

using namespace std;

const double eps = 10e-5;
const double dd = 10;

double f(double x) {

	return pow(2,x)*pow((x-1.1),2)-2;
}

double fp(double x) {
	
	return pow(2,x)*(x-1.1)*((x-1.1)*log(2.0)+2);
}

double fpp(double x) {

	return pow(2,x)*(pow(log(2.0),2)*pow(x-1.1,2)+4*(x-1.1)*log(2.0)+2);
}

double x0() {

	double h = 10e-2, x;
	do {
		x = 0;
		do {
			if (f(x)*f(x+h) <= 0) {
				if (f(x)*fpp(x) >= 0) 
					return x;
				if (f(x+h)*fpp(x+h) >= 0) 
					return x+h;
			} 		
			
			if (f(-x)*f(-x-h) <= 0) {
				if (f(-x)*fpp(-x) >= 0) 
					return -x;
				if (f(-x-h)*fpp(-x-h) >= 0) 
					return -x-h;
			}
			x += h;
		} while (x < dd);
		
		h /= 10;
	} while (h <= eps);
}

int main() {
	
	double d = 0;

	double x = x0();
	int t = 0; 

	cout << "x0: " << x << endl; 

	do {		
		d = f(x)/fp(x);
		x -= d;
		t++;
	} while (fabs(d) > eps);

	cout << "Solution: " << x << endl;
	cout << "Iter.: " << t << endl;

	system("pause");
}