Metodo de biseccion en C o C++
Por favor envienme el metodo de biseccion en C o C++, estare sumamente agradecida...
En otro lenguaje :(
http://mondragon.angeltowns.net/paradiso/MetodoBiseccion.html
http://mondragon.angeltowns.net/paradiso/MetodoBiseccion.html
// program k2c6b.CPP - page 39
// numerical solution - bisection
// solve the equation : x^3 + 7xý + 6x - 14 = 0.
// c++ exercices book - dr. gershon kagan (first edition : 2001)
// written in Borland CPP ver 3.1
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
const double aprox = 0.0001;
const char *sign[2] = {" - "," + "};
int a2 = 7, a1 = 6,a0 = -14;
double f(double x) // aqui podes cambiar por la funcion que quieras
{
return ((x+a2)*x+a1)*x+a0;
} // F
int FindRootArea()
{
double previous = f(0);
for(int x = 1;; x++)
{
double now = f(x);
if(previous*now <= 0)
break;
previous = now;
}
return x;
} // FIND ROOT AREA
double Process(double a, double b)
{
double c = (a + b)/2;
cout << setw(15) << a << setw(15) << c << setw(15) << b
<< setw(15) << b-a << endl;
if(fabs(a-b)<aprox)
return c;
if(f(a)*f(c) <= 0)
return Process(a,c);
else
return Process(c,b);
} // PROCESS
void main()
{
clrscr();
cout.setf(ios::fixed);
cout << setprecision(5);
cout << "numerical solution - bisection " << endl << endl;
cout << "solve the equation : " << "x^3"
<< sign[a2 > 0] << abs(a2) << "xý"
<< sign[a1 > 0] << abs(a1) << "x"
<< sign[a0 > 0] << abs(a0) << " = 0 "
<< " aproximation = " << aprox << endl << endl;
double b = FindRootArea();
double a = b-1;
double x = Process(a,b);
cout << endl;
cout << "x = " << x << setw(15) << "f(" << x << ") = " << f(x) << endl;
cout << endl << endl;
getch();
} // MAIN
// numerical solution - bisection
// solve the equation : x^3 + 7xý + 6x - 14 = 0.
// c++ exercices book - dr. gershon kagan (first edition : 2001)
// written in Borland CPP ver 3.1
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
const double aprox = 0.0001;
const char *sign[2] = {" - "," + "};
int a2 = 7, a1 = 6,a0 = -14;
double f(double x) // aqui podes cambiar por la funcion que quieras
{
return ((x+a2)*x+a1)*x+a0;
} // F
int FindRootArea()
{
double previous = f(0);
for(int x = 1;; x++)
{
double now = f(x);
if(previous*now <= 0)
break;
previous = now;
}
return x;
} // FIND ROOT AREA
double Process(double a, double b)
{
double c = (a + b)/2;
cout << setw(15) << a << setw(15) << c << setw(15) << b
<< setw(15) << b-a << endl;
if(fabs(a-b)<aprox)
return c;
if(f(a)*f(c) <= 0)
return Process(a,c);
else
return Process(c,b);
} // PROCESS
void main()
{
clrscr();
cout.setf(ios::fixed);
cout << setprecision(5);
cout << "numerical solution - bisection " << endl << endl;
cout << "solve the equation : " << "x^3"
<< sign[a2 > 0] << abs(a2) << "xý"
<< sign[a1 > 0] << abs(a1) << "x"
<< sign[a0 > 0] << abs(a0) << " = 0 "
<< " aproximation = " << aprox << endl << endl;
double b = FindRootArea();
double a = b-1;
double x = Process(a,b);
cout << endl;
cout << "x = " << x << setw(15) << "f(" << x << ") = " << f(x) << endl;
cout << endl << endl;
getch();
} // MAIN
