C/Visual C - Newton- Raphson en c++

 
Vista:

Newton- Raphson en c++

Publicado por guinetar17 (1 intervención) el 20/10/2006 21:02:19
NECESITO HACER UN EJECUTABLE EN C++ DE ESTE PROBLEMA, EL DETALLE ES QUE MATEMATICAMENTE LO ENTIENDO DE MANERA POBRE Y SE OCUPAR C++ A NIVEL MEDIO BAJO. ALGUIEN HA RESULTO UN EJERCICIO SIMILAR???, PORFAVOR AYUDA URGENTE, ACA VA EL EJERCICIO

Utilizando el método de Newton- Raphson hallar la solución de la ecuación
x5 -5x3 + 2 x +1 = 0 en el intervalo (0,1) con un error no mayor a 0,0005.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Newton- Raphson en c++

Publicado por JuanC (57 intervenciones) el 24/10/2006 17:39:13
/*********************************************************************
METODOS PARA CALCULAR LAS RAICES DE UNA FUNCION: f(x)=0
programado por:
JOSE LUIS DE LA CRUZ LAZARO UNI-FIEE 30 Nov 1999
[email protected]
Pagina Web: http://www.geocities.com/joseluisdl/jldl.htm
*********************************************************************/
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <ctype.h>

//"menu.h"
//RUTINA PARA CREAR UN MENU VIRTUAL EN EL MODO TEXTO
#ifndef __STDLIB_H
#include <stdlib.h>
#endif

#ifndef __CONIO_H
#include <conio.h>
#endif

#ifndef __STDIO_H
#include <stdio.h>
#endif

#ifndef __BIOS_H
#include <bios.h>
#endif

int MENU (char *vec[],int x,int y,int dim,int puntero,int col);
///
void CUADRO(int x1,int y1,int ancho,int largo,int col);

Imprimir_Raiz( int iteracion, double raiz)
{
cout<<"\nIteraci¢n "<<iteracion<<"\tRaiz = "<<raiz;
}

//definicion de la funcion f(x)
double f( double x)
{
//f(x)=xtan(x)-x^2-0.168=0
return x*tan(x)-x*x-0.168;
}

//definicion de la funcion g(x)
//para obtener g(x) se despeja convenientemente f(x)
//de forma que quede x=g(x) donde g'(x)<1 ( derivada de g(x) <1 )
//esta funcion se utiliza en el metodo del punto fijo
double g( double x)
{
//x=g(x)=(x^2+0.168)/tan(x);
return (x*x+0.168)/tan(x);
}

double fderivada(double x)
{
/*
derivamos por definicion
lim ( f(x+h)-f(x) )/h
h->0
*/
double h=1e-6;
return (f(x+h)-f(x))/h;

}

//Metodo de Newton-Raphson
double Newton_Raphson( double x , double error=1e-6)
{
double x_anterior;

int iteracion=1;

do
{
x_anterior=x;
x=x-f(x)/fderivada(x);
Imprimir_Raiz( iteracion++, x );
}while( fabs(x-x_anterior)>error );

return x;

}

//Metodo de la bisecci¢n
double Biseccion( double a, double b, double error=1e-6)
{
double m;
int iteracion=1;
do
{
m=(a+b)/2;
if( f(a)*f(m)<0 )b=m;
else a=m;
Imprimir_Raiz( iteracion++, (a+b)/2 );
}while( fabs(a-b)>error );

return (a+b)/2;
}

//Metodo de la falsa posici¢n
double Falsa_Posicion( double a, double b, double error=1e-6)
{
double x1,x2,fa,fx2;
int iteracion=1;
x2=a;
do
{
x1=x2;
x2=a-(b-a)*f(a)/(f(b)-f(a));
fa=f(a);
fx2=f(x2);
if((fa*fx2)>0) a=x2; else b=x2;
Imprimir_Raiz( iteracion++, x2 );
}while( fabs(x2-x1)>error );
}

//Metodo de la secante
double Secante( double x1, double x2, double error=1e-6)
{
double fx1,fx2,x;
int iteracion=1;
do
{
fx1=f(x1);fx2=f(x2);
x=x2-fx2*(x2-x1)/(fx2-fx1);
x1=x2;x2=x;
Imprimir_Raiz( iteracion++, x2 );
}while ( fabs(x2-x1)>error );
}

double Punto_Fijo( double x, double error=1e-6)
{
double x1;
int iteracion=1;
do
{
x1=x;
x=g(x);
Imprimir_Raiz(iteracion++,x);
}while( fabs(x1-x)>error );
}

void main()
{
char *Calculo_de_Raices[8]={
"Cambiar par metros",
"Visualizar par metros",
"M‚todo de Bisecci¢n",
"M‚todo de la Falsa Posici¢n",
"M‚todo de la Secante",
"M‚todo de Newton-Raphson",
"M‚todo del Punto Fijo",
"Salir"}; //inicializacion del menu

char opc; //definicion de variables

double a,b,Xo,error=0;//par metros para los m‚todos

clrscr();
while(opc!=-1)
{
clrscr();
CUADRO(12,6,50,15,LIGHTRED);
gotoxy( 14,8 );
textcolor(LIGHTCYAN);
cprintf("METODOS PARA CALCULAR LAS RAICES DE LA FUNCION");
gotoxy( 25,9 );cprintf("f(x)=xtan(x)-x^2-0.168=0");
gotoxy( 1,23 );cprintf("Utilice las flechas ARRIBA y ABAJO para desplazar el cursor sobre las opciones");

opc=MENU(Calculo_de_Raices,20,11,8,-1,15);//se crea el menu de opciones

switch(opc)
{
case 0:
textcolor(WHITE);
clrscr();
cout<<"\n\nIngresar el intervalo [A,B] donde se desea buscar la raiz\n"
<<"Ingrese A (N£mero Real):";
cin>>a;
cout<<"Ingrese B (N£mero Real):";
cin>>b;
cout<<"\n\nPara M‚todo de Newton y Punto fijo\nIngrese valor inicial de la raiz (N£mero Real): ";
cin>>Xo;
cout<<"\n\nIngrese el error de aproximaci¢n ( \"N£mero peque¤o\" 0<error<=0.1 ): ";
cin>>error;
break;

case 1:
if( error==0 )break;
clrscr();
cout<<"\n\n";
textcolor(YELLOW);
cprintf("Intervalo [a,b] donde se desea buscar la raiz = [ %g , %g ]",a,b);
cout<<"\n\n";
textcolor(LIGHTCYAN);
cprintf("Para M‚todo de Newton y Punto fijo");
cout<<"\n";
cprintf("Valor inicial de x = %g",Xo);
cout<<"\n\n";
textcolor(LIGHTRED);
cprintf("Error de aproximaci¢n (en valor decimal) = %g ",error);
getch();
break;

case -1:
case 7:
clrscr();
gotoxy(25,12);
cprintf("Esta seguro que desea salir S/N: ");
opc=toupper(getche());
if( opc=='S')
{
opc=-1;
error=1;
}
break;

}

if( opc>1 && error!=0 )
{
clrscr();
cprintf(Calculo_de_Raices[opc]);
cout<<"\n\nTabla de Raices:\n";
switch(opc)
{
case 2: Biseccion( a, b, error );break;
case 3: Falsa_Posicion( a, b, error );break;
case 4: Secante( a, b, error );break;
case 5: Newton_Raphson( Xo, error );break;
case 6: Punto_Fijo( Xo, error );break;
}
getch();
}

if( error==0 )
{
textcolor(LIGHTCYAN);
clrscr();
gotoxy(20,12);
cprintf("No se ha ingresado los par metros");
gotoxy(17,13);
cprintf("Por favor seleccione la opci¢n ");
textcolor(LIGHTGREEN);
cprintf("Cambiar par metros");
getch();
}

}

}

int MENU (char *vec[],int x,int y,int dim,int puntero,int col)
{
/*Esta funcion resive unvector tipo caracter
definido de la siguiente manera
char *nomvec[dim]={"OPC1","OPC2","OPC3"..... ,"OPCn"};
necesita las siguientes librerias:
#include <conio.h>
#include <stdio.h>
#include <bios.h>
ejemplo de como se envia
opc=MENU(10,10,nomvec,dim);
puntero= opcion por defecto donde aparecera el puntero
*/
textcolor(col);
int con=0,con_ant=0,sal=0;
if (dim >=1 && x>=2 && y+(dim-1)<=24)
{
for(int k=0;k<dim;k++)
{
gotoxy(x,y+k);cprintf("%s",vec[k]);
}
if(puntero!=-1 && puntero<dim){con=puntero;}

while(sal != 1)
{
gotoxy(x-1,y+con);cprintf(">");

textcolor(col);
gotoxy(x,y+con_ant);cprintf("%s",vec[con_ant]);
textcolor(LIGHTGREEN);
gotoxy(x,y+con);cprintf("%s",vec[con]);
con_ant=con;

while(bioskey(1)==0);
gotoxy(x-1,y+con);cprintf(" ");
switch(bioskey(0))
{
case 0x11b :sal=1;con=-1;break;//ESC
case 0x1c0d:sal=1;break;//ENTER
case 0x4800:
con--;
if(con < 0)con=(dim-1);

break;//Fle. Arriba
case 0x5000:con++;if(con > (dim-1))con=0;break;//Fle. Abajo
}
if( kbhit() ) getch();
}

return(con);
}
else
{
printf("El menu no tiene la dimension correcta o se salio de la pantalla");
getche();
return(con=-1);
}
}

///
void CUADRO(int x1,int y1,int ancho,int largo,int col)
{
/*necesita
#include <conio.h>
#include <stdio.h>
*/
if(x1 >= 0 && y1>=0 && (x1+ancho)<=70 && (y1+largo)<=25)
{
textcolor(col);
for(int i=x1+1;i<=x1+ancho-1;i++)
{
gotoxy(i,y1);cprintf("Í");
gotoxy(i,y1+largo);cprintf("Í");
}
for(int k=y1+1;k<=y1+largo-1;k++)
{
gotoxy(x1,k);cprintf("º");
gotoxy(x1+ancho,k);cprintf("º");
}
gotoxy(x1,y1);cprintf("É");
gotoxy(x1,y1+largo);cprintf("È");
gotoxy(x1+ancho,y1+largo);cprintf("¼");
gotoxy(x1+ancho,y1);cprintf("»");
}
else
{
gotoxy(x1,y1);cprintf("Cuadro fuera de pantalla");getch();
}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar