domingo, 2 de diciembre de 2012

Euler/Runge Kutta

Código:

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<math.h>
//Se debe ingresar X0=0; Y0=1; Xf=1; N=10
/**********************Se ingresa la función****************************/
float func(float x, float y){
  //return 0.5*(1+x)*pow(y,2);
  return x-y;
}
/**********************Reportar los Datos******************************/
void reportar(float x, float y, int i)
{cout<<setiosflags(ios::showpoint | ios::fixed);
  cout<<setiosflags(ios::right);
  cout.precision(4);
  cout<<setw(10)<<i<<setw(15)<<x<<setw(15)<<y<<endl;
}
/**********************Reportar los Datos******************************/
int menu()
{int opc;
  do
  {//clrscr();
    cout<<setw(50)<<"SELECCIONE UNA  OPCIÓN\n";
    cout<<setw(50)<<"-----------------\n"<<endl;
    cout<<"1.Metodo de Euler"<<endl;
    cout<<"2.Metodo de Runge -Kutta"<<endl;
    cout<<"3.Salir"<<endl;
    cout<<"\nSeleccione Opcion: ";cin>>opc;
  }while(opc<1 || opc>3);
  return opc;
}
/**********************Método de Euler******************************/
void Euler(){
  float x0,y0,xf,yf,h;
  int n,i;
  //clrscr();
  cout<<setw(50)<<"Metodo de Integracion de Euler"<<endl;
  cout<<setw(50)<<"------------------------------"<<endl<<endl;
  cout<<"Ingrese el valor de x0: ";
  cin>>x0;
  cout<<"Ingrese el valor de y0: ";
  cin>>y0;
  cout<<"ingrese el valor de xf: ";
  cin>>xf;
  do{
    cout<<"Ingrese el numero de subintervalos a emplear: ";
    cin>>n;
  }while(n<=0);
  h=(xf-x0)/n;
  cout<<endl;
  cout<<setw(10)<<"I"<<setw(15)<<"Xi"<<setw(15)<<"Yi"<<endl;
  cout<<setw(10)<<"-"<<setw(15)<<"--"<<setw(15)<<"--"<<endl;
  for(i=1;i<=n;i++)
  { y0=y0+h*func(x0,y0);
    x0=x0+h;
    reportar(x0,y0,i);
  }
  cout<<"\nEl valor de Yf: "<<y0<<endl;
  getch();
}
/**********************Metodo de Runge Kutta******************************/
void Kutta(){
  float x0,y0,xf,yf,h,k1,k2,k3,k4;
  int n,i;
  //clrscr();
  cout<<setw(50)<<"Método de Runge - Kutta"<<endl;
  cout<<setw(50)<<"-----------------------"<<endl<<endl;
  cout<<"Ingrese el valor de x0: ";
  cin>>x0;
  cout<<"Ingrese el valor de y0: ";
  cin>>y0;
  cout<<"ingrese el valor de xf: ";
  cin>>xf;
  do{
    cout<<"Ingrese el número de subintervalos a emplear: ";
    cin>>n;
  }while(n<=0);
  h=(xf-x0)/n;
  cout<<endl;
  cout<<setw(10)<<"I"<<setw(15)<<"Xi"<<setw(15)<<"Yi"<<endl;
  cout<<setw(10)<<"-"<<setw(15)<<"--"<<setw(15)<<"--"<<endl;
  for(i=1;i<=n;i++)
  { k1=func(x0,y0);
    k2=func(x0+h/2,y0+h*k1/2);
    k3=func(x0+h/2,y0+h*k2/2);
    k4=func(x0+h,y0+h*k3);
    y0=y0+(k1+2*k2+2*k3+k4)*h/6;
    x0=x0+h;
    reportar(x0,y0,i);
  }
  cout<<"El valor de Yf: "<<y0<<endl;
  getch();
}
/**********************Terminar******************************/
void terminar()
{cout<<"\t\t\t\tSalir del Programa\n";
cout<<"\t\t\t\t------------------\n\n";
cout<<"Gracias por usar el programa"<<endl<<endl;
}
/**********************Función Principal******************************/
int main (void)
{int opc;
do
{//clrscr();
  opc=menu();
 // clrscr();
  switch(opc)
  {case 1: Euler(); break;
    case 2: Kutta();break;
    case 3: terminar();break;
  }
  getch();
}
while(opc!=3);
getch();
return 0;
}

Euler

Código:


#include<stdio.h>
#include<iostream.h>
#include<conio.h>

double f(double x0,double y0)
{
 double y;
     //y=y0-2*x0/y0;
       y=x0+y0;
           return y;
}
void imp(double x0,double y0,double h,int N)
{
 int n,flag;
   double x1,y1,yp,yc;
     n=1,flag=0;
 cout<<"El resultado es:"<<endl;
   cout<<" Xn       Yn    "<<endl;
  do{
   if(flag)
   {
  n+=1;
   x0=x1;
    y0=y1;
   }
    x1=x0+h;
      yp=y0+h*f(x0,y0);
        yc=y0+h*f(x1,yp);
          y1=(yp+yc)/2;
    printf(" %.1f    %.4f\n",x1,y1);
         flag=1;
  }while(n!=N);
}

int main(int argc, char* argv[])
{
 int N;
 double x0,y0,h;
  cout<<"Ingrese x0=";
    cin>>x0;
  cout<<"Ingrese y0=";
    cin>>y0;
  cout<<"Ingrese h=";
     cin>>h;
cout<<"Ingrese N=";
     cin>>N;  
  imp(x0,y0,h,N);
  getch();
 return 0;
}

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;  
   }
}

Lagrange

Compilador: Borland C++
Código:


# include <stdio.h>
# include <conio.h>
#include <iostream.h>
# include <malloc.h>
# include <stdio.h>
void lagrange(float x, double X[], double y[], int Lit)
{
 double r=0, num=1, den=1;
 for(int i=0; i<Lit;i++){ //para el total de polinomios
   for(int j=0; j<Lit;j++){ //para cada polinomio
       if (i!=j){  num*=(x - X[j]); den*=(X[i] - X[j] ); }
   }
   num*=y[i];
   printf("Iteracion %d valor %lf\n", i, num/den);
   getch();
   r+=num/den;
   num=den=1;
 }
 cout<<"\nf("<<x<<")= "<< r;
}
void main()
{
 int m,i, medio;
 double *X,*Y,x;
 clrscr();
 printf("Ingresa n puntos deseados:\n\t\t");
 scanf("%d",&m);
 X=(double*)malloc(sizeof(double)*m);
 printf("Ingresa los valores de X de la tabla:\n");
 for(int i=0; i<m; i++)    scanf("%lf", &X[i]);
 Y=(double*)malloc(sizeof(double)*m);
 printf("\nIngresa los valores de Y de la tabla:\n");
 for(i=0; i<m; i++)        scanf("%lf", &Y[i]);
//   cout<<"\nLa ecuacion de la recta de ajuste es: \ty="<<endl;
   printf("\nEscribe el valor X que se desea interpolar:\n");
   scanf("%lf",&x);
 lagrange(x, X, Y, m);
 getch();
}

Gauss-Seidel

Compilador: Borland C++
Código:

/*Gauss Seidel
*/
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
#include <math.h>
#define ITERA 50   //despues de ITERA, el proceso es cancelado
#define Maxec 6  //numero maximo de ecuaciones
enum Boolean{False,True};
void lectura(double a[Maxec][Maxec+1],double x0[Maxec],double b[Maxec],int n)
{
int i,j;
clrscr();
cout<<"Por favor introduzca la matriz de coeficientes del sistema";
gotoxy(3+n*5,5);cout<<"= b";
gotoxy(3+(n/2)*5,5);cout<<"Ax";
for(i=0;i<n;i++)
   {
    for(j=0; j<n; j++)
 {
  gotoxy(3+j*5,7+i);
  scanf("%lf",&a[i][j]);
 }
    gotoxy(3+j*5,7+i);cout<<"= ";
    scanf("%lf",&b[i]);
   }
for(i=1; i<=n; i++)
   {
    printf("x0(%d) = ",i);
    scanf("%lf",&x0[i]);
   }
}
//---------------------------------------------------------------------------
void enter(void)
{
int c;
do{
   if (kbhit())
     c=getch();
  }while(c!=13);
}
//---------------------------------------------------------------------------
void main(void)
{
double  x[Maxec]={0},x0[Maxec]={0},b[Maxec]={0};
double a[Maxec][Maxec+1]={0},s,eps;
int y,i,j,k,n;
Boolean valver=False;
char c,cad[25]={0};
clrscr();
gotoxy(30,1);cout<<"M‚todo de Gauss Seidel";
gotoxy(1,5);cout<<"N£mero de ecuaciones ";
do{
  cin>>n;
  if (n>6)
    cout<<"El sistema debe tener menos de 6 ecuaciones por cuestiones \nde impresion en pantalla\nGracias";
    cout<<"\n\nN£mero de ecuaciones ";
  }while(n>6);
lectura(a,x,b,n);
clrscr();
textcolor(14);
gotoxy(10,12);
cprintf("Si la solucion no converge, presione ESC para terminar");
textcolor(10);
gotoxy(1,24);
cprintf("Presione ENTER para continuar...");
textcolor(7);
enter();
k=0;
y=4;
eps=1e-5;
clrscr();
textcolor(10);
gotoxy(30,1);cout<<"M‚todo de Gauss Seidel";
textcolor(14);
gotoxy(45,24);cprintf("Presione una tecla para continuar...");
textcolor(15);
gotoxy(4,2);printf("k       x1        x2        x3         x4         x5       x6");
do{
   for(i=0;i<n;i++)
     x[i]=x0[i];
   for(i=0; i<n; i++)
     {
      s=0;
      for(j=0; j<n; j++)
  if((i-j)!=0)
    s=s+a[i][j]*x0[j];
      x0[i]=(b[i]-s)/a[i][i];
     }
   for(i=0; i<n; i++)
      {
       sprintf(cad,"%.4f",x[i]);
       x[i]=atof(cad);
       sprintf(cad,"%.4f",x0[i]);
       x0[i]=atof(cad);
      }
   for(i=0; i<n; i++)
      {
       if(fabs(x0[i]-x[i]) > eps)
   valver=False;
       else
   valver=True;
      }
   k++;
   if (k>ITERA)
     c=27;
   gotoxy(2,y);cout<<k;
   for(i=0; i<n; i++)
      {
       gotoxy(8+i*11,y);printf("%5.4f",x0[i]);
      }
   y++;
   c=getch();
   if(y>22)
     {
      y=4;
      clrscr();
      textcolor(10);
      gotoxy(30,1);cout<<"M‚todo de Gauss Seidel";
      textcolor(14);
      gotoxy(45,24);cprintf("Presione una tecla para continuar...");
      textcolor(15);
      gotoxy(4,2);printf("k       x1        x2        x3         x4         x5       x6");
     }
   if (k>ITERA)
     c=27;
  }while(!(valver==True||c==27));
textcolor(15);
cout<<"\n";
cprintf("DESPUES DE %d ITERACIONES",k);
cout<<"\n";
cprintf("LA SOLUCION DEL SISTEMA ES : \n");   printf("\n");
for(i=0; i<n; i++)
   cprintf(" x(%d) = %10.4f",i+1,x0[i]),cout<<"\n";
getch();
}

Interpolación de Newton

Compilador: Borland C++
Código:

//METODO DE INTERPOLACION DE NEWTON
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<iostream.h>
#include<iomanip.h>
void uno(float a);
void dos(float a,float b);
void tres(float a, float b, float c);
void cuatro(float a,float b, float c, float d);
void cinco(float a, float b, float c, float d, float e);
float bs(float f1,float f0,float x1, float x0);
float x[6],fx[6];
void main()
{
 int opcion;
 clrscr();
 cout<<"\nINTERPOLACION DE NEWTON";
 cout<<"\nCUANTOS PUNTOS";
 cin>>opcion;
 if(opcion<=5)
 {
  for(int d=0;d<opcion;d++)
  {
   cout<<"\nX"<<d<<": ";cin>>x[d];
   cout<<"\nf(x)"<<d<<": ";cin>>fx[d];
   }
 switch(opcion)
 {
  case 1:{uno(fx[0]);}break;
  case 2:{dos(x[0],x[1]);}break;
  case 3:{tres(x[0],x[1],x[2]);}break;
  case 4:{cuatro(x[0],x[1],x[2],x[3]);}break;
  case 5:{cinco(x[0],x[1],x[2],x[3],x[4]);}break;
 }
 getch();
}
}
void uno(float a)
{
cout<<"\n";
cout<<"Y="<<a;
getch();
}
void dos(float a, float b)
{
cout<<"\n"<<setiosflags(ios::showpos);
cout<<setiosflags(ios::fixed);
cout<<"Y=";
cout<<bs(a,b,fx[0],fx[1])<<"X"<<bs(a,b,fx[0],fx[1])*(-a)+fx[0];

getch();
}
void tres(float a, float b, float c)
{
float t1,t2,t3;
t1=bs(a,b,fx[0],fx[1]);
t2=bs(b,c,fx[1],fx[2]);
t3=bs(a,c,t1,t2);
cout<<"\n"<<setiosflags(ios::showpos);
cout<<setiosflags(ios::fixed);

cout<<"Y="<<t3<<"X^2 "<<((-a-b)*t3)+t1<<"X"<<-(t1*a)+(fx[0])+(t3*a*b);
getch();

}
void cuatro(float a, float b, float c, float d)
{
float t1,t2,t3,t4,t5,t6;
t1=bs(a,b,fx[0],fx[1]);
t2=bs(b,c,fx[1],fx[2]);
t3=bs(c,d,fx[2],fx[3]);
t4=bs(a,c,t1,t2);
t5=bs(b,d,t2,t3);
t6=bs(a,d,t4,t5);
cout<<setiosflags(ios::showpos);
cout<<setiosflags(ios::fixed);
cout<<"\nY="<<t6<<"X^3 "<<(((-a-b-c)*t6)+t4)<<"X^2"<<((t6*((a*b)+(a*c)+(b*c)))+(t4*(-a-b))+t1)<<"X"<<((t6*(-a*b*c))+((a*b)*t4)-(t1*a)+fx[0]);
getch();
}
void cinco(float a, float b, float c, float d, float e)
{
//long
float t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
t1=bs(a,b,fx[0],fx[1]);
t2=bs(b,c,fx[1],fx[2]);
t3=bs(c,d,fx[2],fx[3]);
t4=bs(d,e,fx[3],fx[4]);
t5=bs(a,c,t1,t2);
t6=bs(b,d,t2,t3);
t7=bs(c,e,t3,t4);
t8=bs(a,d,t5,t6);
t9=bs(b,e,t6,t7);
t10=bs(a,e,t8,t9);

cout<<setiosflags(ios::showpos);
cout<<setiosflags(ios::fixed);
cout<<"\n"<<fx[0]<<t1<<"(x"<<-a<<")"<<t5<<"(x"<<-a<<")"<<"(x"<<-b<<")"<<t8<<"(x"<<-a<<")"<<"(x"<<-b<<")"<<"(x"<<-c<<")"<<t10<<"(x"<<-a<<")"<<"(x"<<-b<<")"<<"(x"<<-c<<")"<<"(x"<<-d<<")";

getch();
}


float bs(float x0,float x1,float f0, float f1)
{
       return( (f1-f0)/(x1-x0));
}

Gauss-Jordan

Compilador: Borland C++
Código:

#include <conio.h>
#include <math.h>
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>

class gauss
{
 private:
  int n,k,Piv,f,c,a[4][4];
      float cero;
   public:
      gauss();
  int Lee_Datos(float a[4][4]);
  void Pivoteo(int n, int i, float a[4][4]);
  void Mueve_Ceros(int n, int i, float a[4][4]);
  void Imprime_matriz(int n, float a[4][4]);
      void Comprobacion(int n,  float a[4][4]);
};
gauss::gauss(){}
int gauss::Lee_Datos(float a[4][4])
{
 int n;
 int k,j;
 cout<<"\n  Número de Ecuaciones =";
 cin>>n;
 cout<<"\n";
 cout<<"\n Sistema de Ecuaciones Lineales:";
 cout<<"\n";
 for ( k=0; k < n; k++)
 {
  for (j=0; j< n+1; j++)
  {
   cout<<"\n["<<(k+1)<<","<<(j+1)<<"] = ";
   cin>>a[k][j];
  }
  cout<<endl;
 }
 return n;
}
void gauss::Pivoteo(int n, int i, float a[][4])
{
 int j;
 float Piv;
 Piv = a[i][i];
 for (j=i; j < n+1; j++)
  a[i][j] = a[i][j]/ Piv;
}
void gauss::Mueve_Ceros(int n, int i, float a[][4])
{
 int k,j;
 float Cero;
 for (k=0; k < n; k++)
 {
  if (k != i)
  {
   Cero = a[k][i];
   for (j=i; j<n+1; j++)
    a[k][j] = a[k][j] - (Cero * a[i][j]);
  }
 }
}

void gauss::Imprime_matriz(int n, float a[][4])
{
 int f,c;
 for (f=0; f< n; f++)
 {
  cout<<"\n";
  for (c=0; c< n+1; c++)
  {
   if (c == n)
    cout<<a[f][c];
   else
    cout<<"\t"<<a[f][c]<<"X"<<(c+1)<<"\t";
  }
 }
}
 void gauss:: Comprobacion(int n,  float a[4][4])
 {
  cout<<"Comprobacion"<<endl;
   cout<<"I1\t"<<a[0][n]<<"\n";
   cout<<"I2\t"<<a[1][n]<<"\n";
   cout<<"I3\t"<<a[2][n]<<"\n";
}
int main()
{
  int     N;
  int     f;
  int     i;
  float   A[4][4];

  gauss obj;
  inicio:
  cout<<"\n\tMétodo de eliminación de Gauss-Jordan";
  N=obj.Lee_Datos(A);
  for (i= 0; i < N; i++)
   {
    cout<<"\nRenglon"<<(i+1)<<"\n";
    obj.Imprime_matriz(N,A);
    cout<<"\n";
    obj.Pivoteo(N,i,A);
    obj.Imprime_matriz(N,A);
    cout<<"\n";
    obj.Mueve_Ceros(N,i,A);
    obj.Imprime_matriz(N,A);
    cout<<"\n";
   }
  cout<<"\n Resultado del sistema:\n";
  for (f =0; f < N; f++)
  cout<<" X "<<(f+1)<<" = \t"<<A[f][N]<<endl;
  obj.Comprobacion(N,A);
  getch();
  clrscr();
  goto inicio;
 return 0;
}

Bisección

Compilador: Borland C++
Código:

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
class Biseccion
{
 private:
    float LimA;
      float LimB;
      float LimC;
  float Es;
      float Ea;
      float Raiz;

   public:
    void IngrVal();
      void ResBis();
      float Funcion(float Var);
      void CalcularEa();
};
void Biseccion::IngrVal()
{
 cout<<endl<<" MÉTODO DE BISECCIÓN"<<endl<<endl;
   cout<<"Busca la raiz cuadrada de un numero por el metodo de biseccion"<<endl<<endl;
   do
   {
   cout<<"Ingrese el número del que quiere calcular la raiz"<<endl;
   cin>>Raiz;
   if(Raiz<=0)
   {
   cout<<"\n\n El programa no calcula raices negativas ni 0\n";
   }
   }while(Raiz<=0);
 cout<<"Ingresa el limite Xa"<<endl;
   cin>>LimA;
   cout<<"Ingresa el limite Xb"<<endl;
   cin>>LimB;
   cout<<"Ingresa el error preestablecido Es(%)"<<endl;
   cin>>Es;
}
void Biseccion::CalcularEa()
{
 Ea=((LimA-LimB)/LimA)*(100);
   Ea=fabs(Ea);
}
float Biseccion::Funcion(float Var)
{
   float RFun;
   RFun = (pow(Var,2)-Raiz);
   return RFun;
}

void Biseccion::ResBis()
{
 int i=0;
    float Res;
   gotoxy(5,15);
 cout<<"Xa"<<endl;
   gotoxy(15,15);
   cout<<"Xb"<<endl;
   gotoxy(25, 15);
   cout<<"Xr"<<endl;
   gotoxy(35,15);
   cout<<"f(Xa)-f(Xb)"<<endl;
   gotoxy(48,15);
   cout<<" Ea"<<endl<<endl;
  do
  {
   LimC=((LimA+LimB)/2);
   Res=Funcion(LimA)*Funcion(LimC);
   CalcularEa();
   gotoxy(5,17+i);
 cout<<LimA<<endl;
   gotoxy(15,17+i);
   cout<<LimB<<endl;
   gotoxy(25, 17+i);
   cout<<LimC<<endl;
   gotoxy(35,17+i);
   cout<<Res<<endl;
   gotoxy(48,17+i);
   cout<<Ea<<endl<<endl;
   i++;
   if(Res>0)
 {
    LimA=LimC;
 }
   else
   {
    LimB=LimC;
    }

  }while(Ea>Es);
  cout<<"La raiz es: "<<LimC<<endl;
}

int main()
{
 Biseccion a;
   a.IngrVal();
 a.ResBis();
 getch();
   return 0;
}

Aproximaciones

Codigo de aproximaciones, se corre en Borland C++

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
void main()
{
  float B,R;
  cout<<endl<<" MÉTODO DE APROXIMACIONES SUCESIVAS   "<<endl<<endl;

  do
   {
   cout<<"Ingrese el número de X0"<<endl;
   cin>>B;
   if(B<=0)
   {
   cout<<"\n\n El programa no calcula raices negativas ni 0\n";
   }
   //}while(B<=0);
   R=6*(pow(B,2))-(14*B)+4;
   R=fabs(R);
   cout<< " R = " <<R;
   if(R>1)
   cout<<"\n\n no es posible evaluar la función indique otro valor de X0 ";
   }while(B<=0);
   getch();
}