Search & Find out

Thursday, December 3, 2009

Backword, Fordward & Newton Raphson Methods

 This Program includes Backword, Fordward & Newton Raphson Methods with it's GRAPH


#include "stdio.h"
#include "conio.h"
#include "math.h"

#include "graphics.h"


void main()
{
     int power,i=0,m,n,n1,l,gd,gm,xaxis,yaxis,p=0;
     double x1,x2,x0,fx1,fx2,fx0,c[100],n2,fx,gx1[100],gx2[100],f1[100],f2[100];
     char a[50],b[100],e;
     clrscr();

     printf("Enter your lucky number (0 to 14) :  ");
     scanf("%d",&i);

     textcolor(i);
     textbackground(WHITE);
     clrscr();

     printf("Enter your name:\t");
     scanf("%s",a);
     clrscr();


teja:
     printf("\n\nHi, %s Welcome to the world of computational mathematics\n\n",a);
     printf("Which method would you like to perform\n\n 1) Bisection Method\t2)False Position Method\t3)Newton Lapson Method\n\n");
     scanf("%d",&m);

     if(m!=1 && m!=2 && m!=3)
      {
    clrscr();
    printf("\n\tInvalid option, try again\n");
    goto teja;

      }
      else
       {
    printf("\n\nyour option is %d (y | n)\n\n",m);
    e=getche();
    if(e=='y'||e=='Y')
    {
    }
    else
    {
      clrscr();
      printf("\nSo, choose your option again\n\n");
      goto teja;
    }
      }


     clrscr();
     printf("\n\nEnter the maximum power\t");
     scanf("%d",&power);

     for(i=power;i>=0;i--)
     {

       if(i==0)
       {
    printf("Enter a constant number\t");
       }
       else
       {
    printf("Enter the coefficient of the power %d \t",i);
       }

       scanf("%lf",&c[i]);
     }
      printf("\n\n");

     for(i=power;i>=0;i--)
     {
       if(i==0)
       {
    printf("(%lf)  = 0\n",c[i]);
       }
       else
       {
    printf("(%lf) x^%d + ",c[i],i);
       }
     }
       if(m==3)
       {
     goto tej;
       }
       printf("\nEnter the value of x1 & x2\n\n");
       scanf("%lf%lf",&x1,&x2);
       clrscr();

tej:

       printf("\nhey...! %s do you want to perform function till \n\n1)No. of ittrations\t2)Accuracy\n\n",a);
       scanf("%d",&n);

     if(n==1)
     {
      printf("\nSo, enter the no. of ittrations\t");
      scanf("%d",&n1);
     }
     else if(n==2)
     {
      printf("\nSo, enter the Accuracy till\t");
      scanf("%lf",&n2);
     }
     else
     {
      clrscr();
      printf("\n\tInvalid option try again\n");
      goto tej;
     }
     clrscr();

     if(m==3)
     {
     goto m3;
     }


     for(  l=1,fx0=1;  (n==1?l<=n1:fabs(fx0)>=n2);   l++  )

    {
        for(fx1=0,i=power;i>=0;i--)
        {
          fx=c[i] * pow(x1,i);
          fx1=fx+fx1;
        }
        for(fx2=0,i=power;i>=0;i--)
        {
          fx=c[i] * pow(x2,i);
          fx2=fx+fx2;
        }

        if(m==1)
        {
          x0=(x1+x2)/2;
        }
        else
        {
          x0=(x1-((x2-x1)/(fx2-fx1))*fx1);
        }

        for(fx0=0,i=power;i>=0;i--)
        {
          fx=c[i]*pow(x0,i);
          fx0=fx+fx0;
        }

       if(l==1)
       {
         printf("\nIttration  x1       x2          x0          fx1          fx2          fx0\n");
       }

       printf("\n%d    %lf    %lf    %lf    %lf    %lf    %lf\n",l,x1,x2,x0,fx1,fx2,fx0);

        if((fx1>0&&fx2>0)||(fx1<0&&fx2<0))
        {

          printf("\nThe function won't be perform further,because the sign of fx1 & fx2 are same.\n");
          getch();
          goto exit;
        }

       gx1[l]=x1;gx2[l]=x2;f1[l]=fx1;f2[l]=fx2;
       p=p+1;

       x1=((fx0>0&&fx1>0)||(fx0<0&&fx1<0)?x0:x1);
       x2=((fx0>0&&fx2>0)||(fx0<0&&fx2<0)?x0:x2);

      }

     getch();
     clrscr();

     printf("\nDo you want to see the graph of the function: (y | n)\n\n");
     e=getche();
      if(e=='y'||e=='Y')
      {
    goto graph;
      }
      else
      {
    goto repeat;
      }


graph:
      clrscr();
      detectgraph(&gd,&gm);
      initgraph(&gd,&gm,"c:\\tc\\bgi");
      xaxis=getmaxx();
      yaxis=getmaxy();

      line(0,yaxis/2,xaxis,yaxis/2);
      line(xaxis/2,0,xaxis/2,yaxis);
      getch();

    for(l=1;l<=9;l++)
      {
    gx1[l]=gx1[l]*10;
    gx2[l]=gx2[l]*10;
    f1[l]=f1[l]*10;
    f2[l]=f2[l]*10;

    line(gx1[l]+xaxis/2,f1[l]+yaxis/2,gx2[l]+xaxis/2,f2[l]+yaxis/2);

      }

    getch();
    clrscr();

repeat:

     printf("\n\n hi, %s thanks for spending your valuable time.\n\n\t do you want to continue (y | n)\n");
     e=getche();

      if(e=='y'||e=='Y')
      {
    clrscr();
    goto teja;
      }
      else if(e=='n'||e=='N')
      {
    goto exit;
      }
      else
      {
    printf("Please press or enter a valid option");
      }


m3:
    printf("\nEnter the value of x\n");
    scanf("%lf",&x1);

    m=power-1;

      for(  l=1,x2=1;  (n==1?l<=n1:fabs(x2)>=n2);   l++  )
      {
     for(fx1=0,i=power;i>=0;i--)
        {
          fx=c[i]*pow(x1,i);
          fx1=fx+fx1;
        }

      for(fx2=0,i=power;i>0;i--)
      {
       fx=(c[i]*i) * pow(x1,m);
       fx2=fx+fx2;
       m--;

      }

      x2=(x1-(fx1/fx2));

      if(l==1)
      {
        printf("\tItt. \tx1   \tfx1    \t\tf'x1     \tx2 \n");
      }
     printf("\t%d    %lf    %lf    %lf    %lf \n",l,x1,fx1,fx2,x2);

     x1=x2;

       }

     getch();

exit:

}



Download the executable (.exe)  file for this program..
download

0 comments:

Post a Comment

 

My Blog List

Term of Use