c program of N-R method. (WITH OUTPUT IMAGE)

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


float f(float x)
{return ((x*x)-12);
}

float f1(float x)
{
return (2*x);
}


void main()
{int i,flag=0;
float a,b,fa,fb,x,fx,f1x,x1,fx1,f1x1,ea,es=.01;
FILE *fp;
clrscr();

fp=fopen("nr-op.txt","w");
fprintf(fp,"\n a          f(a)       b        f(b)     x         f(x)      ea  ");


  for(i=0;i<10000;i++)
 {
     if((f(i)*f(i+1))<0)
     { a=i;
      b=i+1;
      flag=1;
      break;
     }
  }

    if(flag!=1)
    {
      for(i=0;i>-10000;i--)
      {  if(f(i)*f(i-1)<0)
{ a=i;
  b=i-1;
  break;
}
      }
    }


fa=f(a);
fb=f(b);
x=(a+b)/2;
fx=f(x);
fprintf(fp,"\n %f  %f  %f %f %f %f ",a,fa,b,fb,x,fx);

do
{x1=x;
fx1=f(x1);
f1x1=f1(x1);

x=x1-(fx1/f1x1);

ea=fabs((x-x1)/x)*100;

fprintf(fp,"\n \t \t \t \t \t %f %f %f", x,fx,ea);

}
while(ea>es);
fprintf(fp,"\n your ans is %f",x);
getch();
}



After writing this code in c you will get your output in a file name nr-op.txt  that will be created by program in   bin folder. So u will get blank screen on pressing alt+f9  check output in file.





Comments