code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
Terms of Agreement:
By using this code, you agree to the following terms...
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
//IN THE NAME OF GOD //
//Iterative Methods //
// written by Hossam Al Din Ali 1/7/2000
//
#include
#include
using namespace std;
double NewtonRaphson(double,int,double,double(*)(double),double(*)(double));
double RegulaFalsi(double,double,int,double,double(*)(double));
double Secant(double,double,int,double,double(*)(double));
double Halley(double,int,double,double(*)(double),double(*)(double),double(*)(double));
double f(double);
double df(double);
double d2f(double);
int main()
{
cout<<"Secant:"< cout<<"RegulaFalsi:"< cout<<"NewtonRaphson: "< cout<<"Halley:"< } double NewtonRaphson(double x0,int N,double T,double(*fx)(double),double(*dfx)(double)) { int i=1; double x; while(i<=N){ x=x0-(*fx)(x0)/(*dfx)(x0); if(abs(x-x0) i++; x0=x; } return x; } double RegulaFalsi(double a,double b,int N,double T,double(*fx)(double)) { int i=1; double c; while(i<=N){ c=b-((b-a)*(*fx)(b))/((*fx)(b)-(*fx)(a)); if(abs(c-b) if ((*fx)(c)*(*fx)(a)<0)swap(b,c); else if((*fx)(c)*(*fx)(b)<0)swap(a,c); i++; } return c; } double Secant(double x01,double x02,int N,double T,double(*fx)(double)) { int i=1; double x; while(i<=N){ x=x01-((x01-x02)*(*fx)(x01))/((*fx)(x01)-(*fx)(x02)); if(abs(x-x01) i++; x02=x01; x01=x; } return x; } double Halley(double x0,int N,double T,double(*fx)(double),double(*dfx)(double),double(*d2fx)(double)) { int i=1; double x; while(i<=N){ x=x0-(2*(*fx)(x0)*(*dfx)(x0))/(2*pow((*dfx)(x0),2)-((*fx)(x0))*((*d2fx)(x0))); if(abs(x-x0) i++; x0=x; } return x; } /* if your compiler doesn't support the swap function add this code template template { T temp=a; a=b; b=temp; }*/ double f(double x) { return 2*x*x-1; } double df(double x) { return 4*x; } double d2f(double x) { return 4+x*0; }