Secent // C++ Code

Posted: March 5, 2012 by muhammadakif in Algorithms
Tags: , , , ,

#include<iostream.h>
#include<conio.h>
#include<math.h>

double f(double x)
{

	return ((3*x)+ sin(x) - exp(x)); // f(x) = x^3 + sin(x) - e^x

}

void main(void)
{
	double x0 = 1, x1 = 2, x2 = 0;
	cout<<"x0 = " << x0 << " | x1 = " << x1 << " | x2 = " << x2 << endl;

		if(abs(f(x0)) < abs(x1))
		{
			x0 = x1;
		}
		cout<<"x0 = " << x0 << " | x1 = " << x1 << " | x2 = " << x2 << endl;

		do
		{
			x2 = x0 - f(x0)*(x0 - x1) / f(x0) - f(x1);

			x0 = x1;
			x1 = x2;

			cout<<"x0 = " << x0 << " | x1 = " << x1 << " | x2 = " << x2 << endl;

		}
		while(abs(f(x2)) < 0.000000000001);

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s