Está en la página 1de 8

 Diagrama de flujo del método de la regla falsa.

x1, x2, Eps, nit

y1= f(x1)
y2= f(x2)

Si No
y1*y2 > 0.0

“Error en el
intervalo”

Return (0)

i x1 x2 x3 xi+1-xi y1 y2 y3 ea

1
1

Si No
y1==y2

“No se puede
dividir por cero”

Return (0)

x3= x2-[y2*(x1-x2)/(y1-y2)]

y3=f(x3)

Si No
y3==0

res=x3

Return (0)
“El proceso
converge res”=
res

2
2

i=1

1 x1 x2 x3 xi+1-xi y1 y2 y3 ea

Si No
y1*y3> 0

x1=x3 x2=x3

x4=x3

3 for (i=2; i<=nit; i++) 4

y1= f(x1)
y2= f(x2)

Si No

y1==y2

“No se puede
Return (0)
dividir por cero”

5
5
x3= x2-[y2*(x1-x2)/(y1-y2)]
y3=f(x3)

Si No
y3==0

res=x3

Return (0)
“El proceso
converge res”=
res

ea= |(x3-x4)/(x3)|*100

2 x1 x2 x3 xi+1-xi y1 y2 y3 ea

Si No
|x3-x4|<=Eps

res=x3
break

“El proceso
converge res”=
res

6
6
Si No
y1*y3> 0

x1=x3 x2=x3

x4=x3

Si No
|x3-x4|>Eps

“El sistema no
converge”

fin

 Código de programación del método de la regla falsa


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

using namespace std;

#define f(x)(4.0*pow(x,3)-30.0*pow(x,2)+70.0*x-50.0)

main()
{
int i,nit;
float x1,x2,x3,x4,y1,y2,y3,eps,ea,res;

printf("Programa para encontrar la raiz usando la regla falsa\n");


printf("\n");
printf("x1= ");scanf("%f",&x1);
printf("x2= ");scanf("%f",&x2);
printf("eps= ");scanf("%f",&eps);
printf("nit= ");scanf("%f",&nit);
printf("\n");

y1=f(x1);
y2=f(x2);

if (y1*y2>0)
{
cout<<"\nError en el intervalo"<<endl;
return(0);
}
if (y1==y2)
{
cout<<"\nNo se puede dividir por cero\n"<<endl;
return(0);
}
x3=x2-y2*(x1-x2)/(y1-y2);
y3=f(x3);
if (y3==0)
{
res=x3;
cout<<"\nSe encontro la raiz en el primer intervalo\n"<<endl;
return(0);
}
i=1;
cout<<"it"<<" "<<" x1"<<" "<<" x2"<<" "<<" x3"<<" "<<" |xi+1-xi|"<<" "<<" ea
%"<<" "<<" y1"<<" "<<" y2"<<" "<<" y3"<<endl;
printf("\n");
printf("%2d %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f\n",i,x1,x2,x3,y1,y2,y3);
if (y1*y3>0)
x1=x3;
else
x2=x3;
x4=x3;

for (i=2;i<=nit;i++)
{
y1=f(x1);
y2=f(x2);

if (y1==y2)
{
cout<<"\nNo se puede dividir por cero\n"<<endl;
return(0);
}
x3=x2-y2*(x1-x2)/(y1-y2);
y3=f(x3);
if (y3==0)
{
res=x3;
cout<<"\nEl proceso converge res= "<<res<<"\n"<<endl;
return(0);
}
ea=fabs((x3-x4)/x3)*100;
printf("%2d %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f\n",i,x1,x2,x3,fabs(x4-
x3),ea,y1,y2,y3);

if (fabs(x3-x4)<=eps)
{
res=x3;
cout<<"\nEl proceso converge res= "<<res<<"\n"<<endl;
break;
}
if (y1*y3>0.0)
x1=x3;
else
x2=x3;
x4=x3;
}
if (fabs(x3-x4)>eps)
cout<<"\nEl sistema no converge\n"<<endl;

printf("\n");
system("pause");
}

También podría gustarte