Ayuda Newton Raphson

Edgar c++
21 de Septiembre del 2004
Que partes del codigo que hice estan mal o que , lo compila (Dev c++) y ya compiñado corre pero en ocaciones se traba y ya no me da la raiz, quiero opiniones para poder mejorar mi programa
porfa se los agradecere demaciado.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void newton1();
main()
{
float a,b,c,d,e,f,g,h,i,x,df,ff,tol;
int iter,j=0;
char resp;
do{
printf ("nMetodo de Newton Raphson");
printf ("nPara resolver ecuaciones con el siguiente formato:nAx^8+Bx^7+Cx^6+Dx^5+Ex^4+Fx^3+Gx^2+Hx+I=0");
printf ("nValor de A:"); scanf (" %f",&a);
printf ("Valor de B:"); scanf (" %f",&b);
printf ("Valor de C:"); scanf (" %f",&c);
printf ("Valor de D:"); scanf (" %f",&d);
printf ("Valor de E:"); scanf (" %f",&e);
printf ("Valor de F:"); scanf (" %f",&f);
printf ("Valor de G:"); scanf (" %f",&g);
printf ("Valor de H:"); scanf (" %f",&h);
printf ("Valor de I:"); scanf (" %f",&i);
printf ("Valor del punto inicial:"); scanf (" %f",&x);
printf("Dame el numero de iteraciones:");scanf(" %i",&iter);
printf ("Dame la toleracia:");scanf(" %f",&tol);

do{
ff=(a*pow(x,8))+(b*pow(x,7))+(c*pow(x,6))+(d*pow(x,5))+(e*pow(x,4))+(f*pow(x,3))+(g*pow(x,2))+(h*x)+(i);
df=(8*a*pow(x,7))+(7*b*pow(x,6))+(6*c*pow(x,5))+(5*d*pow(x,4))+(4*e*pow(x,3))+(3*f*pow(x,2))+(2*g*x)+(h);

if (df!=0)
if (df!=0)
{
x=(x-(ff/df));
j ;}
else
{
printf("ERROR");}
}
while ((j<iter)&&(fabs(f)>tol)&&(f!=0));
if (ff==0)
{
printf("nLa raiz exacta es:%.5f", x);
}
else{
printf("nLa raiz aproximada es %.5f", x);
}
getch();
printf("nDeseas otra oparacion s/n");
resp=getche();
}while ((resp=='s')||(resp=='S'));
return 0;
}

ckarla21
21 de Septiembre del 2004
yo tengo este codigo en C, espero te ayude a solucionar tu duda:

/*Practica #2 "Biseccion y Newton-Raphson

biseccion: f(x)=e^x+2^-x+2Cos(x)-6=0
[1,2], No.=15

newton: f(x)=x-0.8-0.2Sen(x)=0
x0=0 y No.=15
*/

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




float f1(float x)
{
return (x- 0.8-(0.2*sin(x)));
}

float f2(float x)
{
return (1-(-0.2*cos(x)));
}

void newton()
{
clrscr();
int iter=15,i,y=2;
float x0=0,x,fx,derivada;
for(i=1;i<=iter;i++)
{
fx=f1(x0);
// derivada=f2(x0);
x=x0-(fx/f2(x0));
gotoxy(2,1);printf("iter");
gotoxy(17,1);printf("x");
gotoxy(32,1);printf("f(x)");

gotoxy(1,y+1);printf("%d",i);
gotoxy(16,y+1);printf("%f",x);
gotoxy(30,y+1);printf("%f",fx);
x0=x;
y=y+1;

}
getch();
}

float funcion1(float x)
{
float res;
res=exp(x)+pow(2,-x)+2*cos(x)-6;
//res=pow(x,3)+2*pow(x,2)+10*x-20;
return res;

}
void biseccion()
{clrscr();
int iter=15,i; float a=1,b=2,y=2;
float x,fx;
for(i=1;i<=iter;i=i+1)
{
if(i<=iter)
{
x=a+(b-a)/2;
gotoxy(2,1);printf("iter");
gotoxy(17,1);printf("a");
gotoxy(32,1);printf("b");
gotoxy(47,1);printf("x");
gotoxy(62,1);printf("f(x)");

gotoxy(1,y+1);printf("%d",i);
gotoxy(16,y+1);printf("%f",a);
gotoxy(31,y+1);printf("%f",b);
gotoxy(46,y+1);printf("%f",x);
fx=funcion1(x);
gotoxy(61,y+1);printf("%f",fx);
if(fx>0)
b=x;
else
a=x;
y=y+1;
}

}
getch();
}

void main()
{ int op;
do{
clrscr();
gotoxy(30,3);printf("PRACTICA #2 ");
gotoxy(15,7);printf("METODO DE BISECCION ...........1");
gotoxy(15,10);printf("METODO DE NEWTON-RAPHSON ......2");
gotoxy(15,13);printf("SALIR .........................3");
gotoxy(15,16);printf("OPCION: "); scanf("%d",&op);

if(op==1) biseccion();
if(op==2) newton();
}while(op<3);

}