Está en la página 1de 4

 Código de programación del método de Biseccion

#include <cstdlib>
#include <iostream>
#include <math.h>
#include <iomanip>
#include <stdio.h>

using namespace std;


float pol(float);
int bis(float ,float , float , int, float*);

main ()
{
int nit,con;
float x1, x2, Eps, raiz;

cout<<"\n\n\n Programa para calcular valores de las raices de un polinomio"<< endl;


cout<<"\n 'METODO DE BISECCION' \n\n"<< endl;

cout<<"\n\n INTRODUZCA VALORES DONDE LA FUNCION CAMBIA DE


SIGNO"<<endl;
cout<<"\n\n Introduzca valor inicial, x1 \n"<<endl;
cout<<" x1= ";cin>>x1;
cout<<"\n\n Introduzca valor final, x2 \n"<<endl;
cout<<" x2= ";cin>>x2;
cout<<"\n\n Introduzca el valor del Epsilon, Eps. \n"<<endl;
cout<<" Eps= ";cin>>Eps;
cout<<"\n\n Introduzca el numero de iteraciones, nit \n"<<endl;
cout<<" nit= ";cin>>nit;
//system("cls");
con=0;

if (bis (x1,x2,Eps,nit,&raiz)==1)
{
cout<<"\t \n El sistema converge, Raiz = "<<raiz<<endl;
cout<<"\n"<<endl;
system("pause");
}

else
{
cout<<"\t \n El sistema no converge "<<endl;
cout<<"\n"<<endl;
system("pause");
}
}

int bis(float x1, float x2, float Eps, int nit, float *raiz)
{

int con,i;
float x3,x4,Ea,y1,y2,y3;
con=0;

printf ("\n nit x1 x2 x3 |Xi+1 - Xi| Ea y1 y2 y3 ");

for(i=1;i<=nit;i++)
{
x3=(x1+x2)/2;
y1=pol(x1);
y2=pol(x2);
y3=pol(x3);

if (pol(x3)==0)
{
*raiz=x3;
con=1;
break;
}

if (i==1)
{
printf("\t \n %2d %8.4f %8.4f %8.4f \t \t \t %8.4f %8.4f %8.4f",i,x1,x2,x3,y1,y2,y3);
}

else
{
Ea=fabs((x3-x4)/x3)*100;
printf("\t %2d %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f
%8.4f",i,x1,x2,x3,fabs(x3-x4),Ea,y1,y2,y3);

if(fabs(x3-x4)<=Eps)
{
*raiz=x3;
con=1;
break;
}
}

x4=x3;

if(pol(x3)*pol(x1)>0)
{
x1=x3;
}
else
{
x2=x3;
}
}

if(con==1)
{
return (con);
}
}

float pol(float x)
{

return (4.0*pow(x,3)-30.0*pow(x,2)+70.0*x-50.0);

También podría gustarte