domingo, 2 de diciembre de 2012

Métodos Numéricos

Código:

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <iomanip>
#include "j:\menuproyecto.cpp"
using namespace std;
int opcion;

//-----------------------Biseccion---------------------------//
class Biseccion
{
      private:
              float a, b, Xr, *Poli, *fa, fasuma, *fb, fbsuma, *fxr, fxrsuma, fxrfa, fxrfb;
              float Ea, E;
              int n, i, j, k;
      public:
              Biseccion();
              void Leedatos();
              void Imprimedatos();
};

Biseccion::Biseccion()
{
    fasuma = 0;
    fbsuma = 0;
    fxrsuma = 0;
}
void Biseccion::Leedatos()
{
     cout<<"Cuantos valores quieres para n?"<<"\t";
     cin>>n;
     Poli = new float [n+1];
     fa = new float [n+1];
     fb = new float [n+1];
     fxr = new float [n+1];
    
     cout<<endl;
     for(i=n; i>=0; i--)
     {
        cout<<"Ingresa x^ "<<i<<" = ";
        cin>>Poli[i];            
     }
   
    cout<<endl<<endl;
    cout<<"Ingresa el valor de a ";
    cin>>a;
   
    cout<<endl;
    cout<<"Ingresa el valor de b ";
    cin>>b;
   
    cout<<endl<<endl;
    cout<<"Ingresa el error ";
    cin>>E;
   
}

void Biseccion::Imprimedatos()
{
    cout<<endl<<endl;
    
    cout<<"f(x) = ";
    
     for(i=n; i>=0; i--)
     {
    
      if(Poli[i]>=0)
         {
           cout<<" + ";
         }
     cout<<Poli[i]<<"x^ "<<i;
     }
    
    cout<<" = 0";
   
    cout<<endl<<endl<<"\t\t";
    system("pause");
   
    system("cls");
   
    cout<<endl<<endl;
    cout<<"a"<<"\t"<<"b"<<"\t"<<"Xr"<<"\t"<<"f(a)"<<"\t"<<"f(b)"<<"\t"<<"f(xr)"<<"\t"<<"Ea"<<endl;
   
    for(j=0; j<=k; j++)
    {
            
    fasuma = 0;
    fbsuma = 0;
    fxrsuma = 0;
   
             if(Ea<E)
             {
                      break;
             }        
   
    else
    {  
             if(fxrfa<0)
             {
                        b = Xr;
             }
            
             if(fxrfb<0)
             {
                        a = Xr;
             }
           
    for(i=n; i>=0; i--)
    {
             fa[i] = Poli[i]*(pow(a,i)); 
             fasuma += fa[i]; 
    }
   
    for(i=n; i>=0; i--)
    {
             fb[i] = Poli[i]*(pow(b,i)); 
             fbsuma += fb[i]; 
    }

         Xr = (a + b)/2;
        
    for(i=n; i>=0; i--)
    {
             fxr[i] = Poli[i]*(pow(Xr,i)); 
             fxrsuma += fxr[i]; 
    } 
                    
         fxrfa = fxrsuma*fasuma;
        
         fxrfb = fxrsuma*fbsuma;        
getch();
}
k++;
if(fxrfa<0)
{
Ea = ((fabs(Xr-b))/Xr)*100;
}
if(fxrfb<0)
{
Ea = ((fabs(Xr-a))/Xr)*100;  
}
cout<<a<<"\t"<<b<<"\t"<<Xr<<"\t"<<fasuma<<"\t"<<fbsuma<<"\t"<<fxrsuma<<"\t"<<Ea<<endl;
getch();
}
}

//-----------------------Punto Fijo---------------------------//
class Punto
{
 private:
         float Xo, X, F1Xo, FXo, Va, Ap, FX, fx, a, b;
            int n, i;
  
    public:
         void LeeDat();
          float fung(float);
            void Pun();
};
void Punto::LeeDat()
{
 cout<<"\n\n\tAproximacion inicial\n\n\tXo = ";
 cin>>Xo;
 cout<<"\n\n\tNumero de iteraciones  ";
 cin>>n;
 cout<<"\n\n\tLa aproximacion   ";
 cin>>Ap;
}
float Punto::fung(float Xo)
{
 return (exp(Xo-1))-(0.5*Xo);
}
void Punto::Pun()
{
   system("cls");
   X = fung(Xo);
  
   if((Xo*X)>0)
 {
  char opc;
  cout<<"\n\tEs posible que la funcion no tenga raiz, Quieres continuar? ";
  cin>>opc;
 
        if(toupper(opc)=='N')
  {
            system("cls");                 
            cout<<endl<<endl<<"\n\n\n\t\t\t";                 
   cout<<"GRACIAS POR USAR ESTE PROGRAMA"<<endl;
   getch();
  }
 }
 i = 1;
    system("cls");
 cout<<"\n\titeracion"<<"\tXo"<<"\t\tg(Xo)"<<"\t\t|X-Xo|"<<endl;

    while(i<=n)
 {
               
 X = fung(Xo);
 Va = fabs(X-Xo);
    cout<<endl<<"\n\t   "<<i<<"\t\t"<<setprecision(3)<<Xo<<"\t\t"<<setprecision(3)<<X<<"\t\t"<<setprecision(3)<<Va;

    if((Va<Ap))
 {
  cout<<"\n\n\n\t\tEl valor de X = "<<X<<" es la raiz"<<endl;
  getch();
  exit(0);
 }

 Xo = X;
 i++;

    getch();
    }
 cout<<"\nDespues de "<<n<<" iteraciones no se encontro la raiz"<<endl;
    getch();
}

//-----------------------Newton-Raphson---------------------------//
class newton
{
 private:
         float Xo, X, x, F1Xo, F2Xo, FXo, Va, Ap, FX, fx;
         int n, i;
 public:
         void Leedat();
         float fun(float);
         float deri(float);
         float deri2(float);
         void Ecu();
};
void newton::Leedat()
{
 cout<<"\n\n\tValor inicial"<<endl,
 cout<<"\n\n\tXo = ";
 cin>>Xo;
 cout<<endl<<"\n\n\tNumero de iteraciones  ";
 cin>>n;
 cout<<endl<<"\n\n\tLa aproximacion  ";
 cin>>Ap;
 system("cls");
}
float newton::fun(float x)
{
 return cos(x)-(pow(x,2));
}
float newton::deri(float x)
{
 return -(sin(x))-(2*x);
}
float newton::deri2(float x)
{
 return -(cos(x))-2;
}
void newton::Ecu()
{
   fx = fun(Xo);
   F1Xo = deri(Xo);
   X = Xo-((fx)/(F1Xo));
   FX = fun(X);
  
   if((fx*FX)>0)
 {
  char opc;
  cout<<"\n\tEs posible que la funcion no tenga raiz, Quieres continuar? ";
  cin>>opc;
 
        if(toupper(opc)=='N')
  {
            system("cls");                 
            cout<<endl<<endl<<"\n\n\n\t\t\t";                 
   cout<<"GRACIAS POR USAR ESTE PROGRAMA"<<endl;
   getch();
  }
 }
  
 cout<<"\n  iteracion"<<"\tXo"<<"\tX"<<"\tf(Xo)"<<"\tf'(Xo)"<<"\tf(X)"<<"\t|X-Xo|"<<endl;
    i = 1;
   
 while(i<=n)
               
 {
  fx = fun(Xo);
  F1Xo = deri(Xo);
  X = Xo -((fx)/(F1Xo));
  FX = fun(X);
  Va = fabs(X-Xo);
  cout<<endl<<"\n      "<<setprecision(3)<<i<<"\t     "<<setprecision(3)<<Xo<<"\t"<<setprecision(3)<<X<<"\t"<<setprecision(3)<<fx<<"\t"<<setprecision(3)<<F1Xo<<"\t"<<setprecision(3)<<FX<<"\t"<<setprecision(3)<<Va<<endl;
 
        if((fx==0) || (Va<Ap))
  {
   cout<<"\n\n\n\t\tEl valor de X= "<<X<<" es la raiz"<<endl;
   getch();
   exit(0);
  }
 
        Xo = X;
  i++;
    getch();
 }

    cout<<"Despues de "<<n<<" iteraciones no se obtuvo la raiz"<<endl;
 getch();
}

//-----------------------Main del Menu------------------------//
int main (int)
{
    Biseccion B;
    Punto P;
    newton N;
    int n;
   
    cout<<"\n\t\tMENU"<<endl<<endl;
    cout<<"\n 1. Metodo de Biseccion"<<endl;
    cout<<"\n 2. Metodo de Punto Fijo"<<endl;
    cout<<"\n 3. Metodo de Newton-Raphson"<<endl;
    cout<<"\n 4. Salir"<<endl;
    cout<<"\n Elija una opcion ";
    cin>>opcion;
   
    switch(opcion)
    {
                   case 1:
                          system("cls");
                         
                          B.Leedatos();
                          B.Imprimedatos();
   
                          getch(); 
                          break;
                  
                   case 2:
                          cout<<"\n\n\n\n\t\t\tMETODO DE PUNTO FIJO"<<endl<<endl;
                          cout<<"\n\t\t\t";
                          system("pause");
                          system("cls");
       
                          float Zo;
 
                          P.LeeDat();
                          P.fung(Zo);
                          P.Pun();
                         
                          break;
                  
                   case 3: 
                          cout<<"\n\n\n\n\t\t\tMETODO DE NEWTON-RAPHSON"<<endl<<endl;
                          cout<<"\n\t\t\t";
                          system("pause");
                          system("cls");
   
                       float Z;

                          N.Leedat();
                       N.fun(Z);
                       N.deri(Z);
                       N.Ecu();
                      
                          break;
                  
                   case 4:
                          system("cls");
                          cout<<"GRACIAS POR USAR ESTE PROGRAMA";
                          break;  
   }
}

No hay comentarios:

Publicar un comentario