4CV9 GML P7
4CV9 GML P7
ANALISIS NUMERICO
DOCENTE: CALZADA SERAFIN FELIPE
PRACTICA 7
METODO DE LA SECANTE
2020303263
GARCIA MARTINEZ LESLIE
GRUPO: 4CV9
GML
1
CODIFICACION
//GARCIA MARTINEZ LESLIE - 4CV9
#include <iostream>
#include <graphics.h>
#include<math.h>
#include <cstdlib>
#include <windows.h>
#include <iomanip>
#include <4CV9_GML_P.h>
using namespace std;
int e;
const int ANCHO = 720, ALTO = 720;
class Polinomio
{
private:
int grado,gradop;
double coef[100];
double coefp[100];
double pto,pta,delta;
public:
void pedirDatos();
void pedirCoeficientes();
void derivar();
void mostrarPolinomio();
double retdelta()
{
return delta;
}
GML
2
double retpto()
{
return pto;
}
double retpta()
{
return pta;
}
GML
3
}
}
};
void Polinomio::derivar()
{
int i,g,j;
g=grado;
j=g;
for(i=0;i<j+1;i++,g--)
{
coefp[i]=g*coef[i];
}
gradop=grado-1;
}
void Polinomio::mostrarPolinomio()
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, (FOREGROUND_GREEN |
FOREGROUND_GREEN | FOREGROUND_GREEN));
cout<<endl<<"\t\t-- Polinomio ingresado --"<<endl;
SetConsoleTextAttribute(hConsole, (FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_BLUE));
int i,n,gr;
n=grado+1;
gr=grado;
cout<<"\t\t ";
for(i=0;i<n;i++,gr--)
{
if(gr==0)
{
GML
4
cout<<coef[i]<<endl;
return ;
}
cout<<coef[i]<<"x^"<<gr<<" + ";
}
}
void Polinomio::pedirDatos()
{
void Polinomio::pedirCoeficientes()
{
/*cout<<endl<<"Ingrese los coeficientes del polinomio: "<<endl<<endl;
int n,i;
n=grado+1;
for(i=0;i<n;i++)
{
cout<<"x^"<<i<<": ";
cin>>coef[i];
}*/
cout<<endl<<"\tIngresa el grado del polinomio: ";
cin>>grado;
GML
5
cout<<endl<<"\tIngresa el polinomio"<<endl;
int n,i, xx;
n=grado+1;
xx=grado;
for(i=0;i<n;i++)
{
cout<<"\t -Coeficiente de x^"<<xx<<": ";
cin>>coef[i];
xx--;
}
}
class funcion:public Polinomio{
private:
double c,fc,fp;
public:
void leer(double n){
c=n;
}
void evaluar(){
int i,j,g,h;
fc=0;
fp=0;
g=backgrado(0);
h=backgrado(1);
for(i=0,j=g;i<g+1;i++,j--)
{
fc=(backcoof(i,0)*pow(c,j))+fc;
}
for(i=0,j=h;i<h+1;i++,j--)
GML
6
{
fp=(backcoof(i,1)*pow(c,j))+fp;
}
}
double resultado(int n){
if(n==0)
{
return fc;
}
else
{
return fp;
}
}
};
GML
7
evaluar();
fxa=resultado(0);
leer(xo);
evaluar();
fxo=resultado(0);
xi=xo-((fxo*(xa-xo))/(fxa-fxo));
return xi;
}
double calcuerror()
{
e=(fabs((xi-xo)/xi))*100;
xa=xo;
xo=xi;
return e;
}
};
class newton:public secante
{
private:
double fcxo,fxo,erro,xi,fprima,xo;
public:
double metodo()
{
leer(xo);
evaluar();
fcxo=resultado(0);
fprima=resultado(1);
xi=(xo-(fcxo/fprima));
return xi;
GML
8
}
double errorr()
{
erro=(fabs(xi-xo)/xi)*100;
xo=xi;
return erro;
}
void inciar()
{
xo=retpto();
}
};
GML
9
leer(xd);
evaluar();
fxd=resultado(0);
xi=(xo-(fxos*delta*xo)/(fxd-fxos));
return xi;
}
double geterror()
{
p=(fabs((xi-xo)/xi))*100;
xo=xi;
return p;
}
};
void dibujarLinea( int moverX, int moverY, int dibujarX, int dibujarY, int color, int
textoX, int textoY, char *nombre ) {
moveto ( moverX, moverY );
setcolor ( color );
lineto ( dibujarX, dibujarY );
outtextxy( textoX, textoY, nombre );
}
void dibujarExponente ( int e ){
float x, y;
for ( x = -360 ; x <= 360; x += 0.01 ) {
y = pow ( x , e ) / 100;
putpixel( 360 + x ,360 - y , YELLOW );
}
}
void pintarPlano ( ) {
GML
10
initwindow( ANCHO, ALTO );
dibujarLinea( 0, ALTO / 2, ANCHO, ALTO / 2, WHITE, ANCHO - 20, ALTO / 2
+ 10, "X" );
dibujarLinea( ANCHO / 2, 0, ANCHO / 2, ALTO, WHITE, ANCHO / 2 + 10, 0,
"Y" );
}
int main()
{
PORTADA();
system("PAUSE>NULL");
system("cls");
modificada A;
int i=1,j=1,k=1;
double s,error,tolerancia,error1,raiz,raiz2,error2;
char opcion;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
do{
system("cls");
SetConsoleTextAttribute(hConsole, (FOREGROUND_RED |
FOREGROUND_RED | FOREGROUND_RED));
cout << endl << "\t\t\t ==== PROPORCIONE LOS DATOS ===="
<<endl;
SetConsoleTextAttribute(hConsole, (FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_BLUE));
A.pedirCoeficientes();
A.mostrarPolinomio();
cout<<endl<<"\t\t El polinomio es correcto? [";
SetConsoleTextAttribute(hConsole, (FOREGROUND_GREEN |
FOREGROUND_GREEN | FOREGROUND_GREEN));
GML
11
cout<<"s";
SetConsoleTextAttribute(hConsole, (FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_BLUE));
cout<<"/"; SetConsoleTextAttribute(hConsole,
(FOREGROUND_RED | FOREGROUND_RED | FOREGROUND_RED));
cout<<"n"; SetConsoleTextAttribute(hConsole,
(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE));
cout<<"] ";
cin>>opcion;
}
while((opcion=='n')||(opcion=='N'));
A.pedirDatos();
A.inciar();
A.iniciar1();
A.starter();
A.derivar();
cout<<endl<<"\tIngrese la tolerancia: ";
cin>>tolerancia;
system("pause>null");
GML
12
system("cls");
SetConsoleTextAttribute(hConsole, (FOREGROUND_GREEN |
FOREGROUND_BLUE | FOREGROUND_BLUE));
cout<<endl<<endl<<"\t\t\t\t ========= S E C A N T E
========="<<endl<<endl<<endl;
SetConsoleTextAttribute(hConsole, (FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_BLUE));
SetConsoleTextAttribute(hConsole, (FOREGROUND_BLUE |
FOREGROUND_BLUE | FOREGROUND_BLUE));
cout<<"\t\t\tIt";
SetConsoleTextAttribute(hConsole, (FOREGROUND_GREEN |
FOREGROUND_GREEN | FOREGROUND_GREEN));
cout<<"\t\t\tXi";
SetConsoleTextAttribute(hConsole, (FOREGROUND_RED |
FOREGROUND_RED | FOREGROUND_RED));
cout<<"\t\t\t\tError"<<endl<<endl;
SetConsoleTextAttribute(hConsole, (FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_BLUE));
do{
raiz=A.metodo1();
error1=A.calcuerror();
cout<<"\t\t\t"<<j<<"\t\t"<<setw(20)<<setprecision(15)<<raiz<<"\t"<<"\t"<
<error1<<endl;
j++;
}while(error1>tolerancia);
cout << endl;
system("pause>null");
system("cls");
SetConsoleTextAttribute(hConsole, (FOREGROUND_GREEN |
FOREGROUND_BLUE | FOREGROUND_BLUE));
cout<<endl<<endl<<"\t\t\t ====== S E C A N T E M O D I F I C A D A
======"<<endl<<endl<<endl;
GML
13
SetConsoleTextAttribute(hConsole, (FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_BLUE));
SetConsoleTextAttribute(hConsole, (FOREGROUND_BLUE |
FOREGROUND_BLUE | FOREGROUND_BLUE));
cout<<"\t\t\tIt";
SetConsoleTextAttribute(hConsole, (FOREGROUND_GREEN |
FOREGROUND_GREEN | FOREGROUND_GREEN));
cout<<"\t\t\tXi";
SetConsoleTextAttribute(hConsole, (FOREGROUND_RED |
FOREGROUND_RED | FOREGROUND_RED));
cout<<"\t\t\t\tError"<<endl<<endl;
SetConsoleTextAttribute(hConsole, (FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_BLUE));
do{
raiz2=A.metodo2();
error2=A.geterror();
cout<<"\t\t\t"<<k<<"\t\t"<<setw(20)<<setprecision(15)<<raiz2<<"\t"<<"\t"<
<error2<<endl;
k++;
}while(error2>tolerancia);
SetConsoleTextAttribute(hConsole, (FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_GREEN));
if(i<j)
{
if(i<k)
{
cout<<endl<<"El metodo mas eficiente fue Secante";
return 0;
}
else
{
GML
14
cout<<endl<<"El metodo mas eficiente fue Secante Modificada";
return 0;
}
}
else
if(j<k)
{
cout<<endl<<"El metodo mas eficiente fue Secante";
return 0;
}
else
{
cout<<"El metodo mas eficiente fue Secante Modificada";
return 0;
}
SetConsoleTextAttribute(hConsole, (FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_BLUE));
return 0;
}
GML
15
PRUEBAS DEL CODIGO
GML
16
GML
17
GML
18
GML
19
GML
20
BIBLIOGRAFIA
• METODOS NUMERICOS PARA INGENIEROS, 5ta edición, Steven C
Chapra, Raymong P. Canale. Mexico 2006.
GML
21