/* PROGRAM FOR NEWTON RAPHSON*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x0,x1,e,delta,c;
int i,n,flag=0;
float f(float x);
float df(float x);
clrscr();
printf("enter the value of x0,e,delta,n:\n");
scanf("%f%f%f%d",&x0,&c,&delta,&n);
for (i=0;i<n;i++)
{
if (df(x0)<delta)
{
flag=1;
break;
}
x1=x0-(f(x0)/df(f(x0)));
if (fabs((x1-x0)/x1)<e)
{
flag=2;
break;
}
x0-x1;
} //end for loop//
if(flag==0)
{
printf("\n solution does not converg in ittration\n");
}
else
if(flag==1)
printf("\n slop is root small.\n");
else
printf("\n root is %f",x1);
getch();
}
float f(float x)
{
return(x*x*x-x*x-1);
}
float df(float x)
{
return(3*x*x-2*x);
}
/*************OUTPUT***************
Enter the value of x0,c,delta,n:
1.5
.1
3
4
Root is 2.015119 */
No comments:
Post a Comment