#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(x*x*x-5*x+1);
}
float df(float x)
{
return(3*x*x-5);
}
void main()
{
int maxitr,itr;
float x0,x1,h,err;
clrscr();
printf("\nEnter values of x0,err,and maxitr:");
scanf("%f%f%d",&x0,&err,&maxitr);
itr=1;
begin:
h=f(x0)/df(x0);
x1=x0-h;
printf("\n the root of equation %d=\t%f\n",itr,x1);
if(fabs(h)<err)
{
printf("\n the root of equation after %d=\t%f\n",itr,x1);
}
else
{
x0=x1;
itr++;
if(itr<maxitr)
goto begin;
else
printf("\n the number of iteration are not sufficient");
}
getch();
}
#include<conio.h>
#include<math.h>
float f(float x)
{
return(x*x*x-5*x+1);
}
float df(float x)
{
return(3*x*x-5);
}
void main()
{
int maxitr,itr;
float x0,x1,h,err;
clrscr();
printf("\nEnter values of x0,err,and maxitr:");
scanf("%f%f%d",&x0,&err,&maxitr);
itr=1;
begin:
h=f(x0)/df(x0);
x1=x0-h;
printf("\n the root of equation %d=\t%f\n",itr,x1);
if(fabs(h)<err)
{
printf("\n the root of equation after %d=\t%f\n",itr,x1);
}
else
{
x0=x1;
itr++;
if(itr<maxitr)
goto begin;
else
printf("\n the number of iteration are not sufficient");
}
getch();
}
No comments:
Post a Comment