Thread: using newton raphson method

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    9

    ovr

    hey friends....
    can u help me with this program, sorry i cant create a new thread. so posting here..
    write a c program to find all the real roots of a polyomial of degree 'n' using newton raphson method. the polynomial cannot be defined as a macro in the program nor the initial interval.
    3 different functions are to be used.
    read polynomial function to read the program.
    find interval used to find the interval and
    bisection method to find the roots..
    plzz. anyone help me... its very urgent..
    please friends.
    Last edited by laserlight; 09-26-2010 at 11:59 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I have moved your post to a new thread. Since it is so urgent, you better get started on it soon and then ask us for help on what you have already tried.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    thanks.. yeah.. i am trying it. but i cant create the polynomial of nthe degree where user will input the value of 'n'.. i am almost a beginner..
    it will be good if u help me with this.plzz

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    this is not running properly..
    please correct the codes...and help me in creating the interval...please


    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    #define MAX 50
    #define eps 1e-6
    void ReadPolynomial(int);
    void BisectionMethod(int,double,double);
    int arr[MAX];
    double a,b;
    void main()
    {
    int deg;
    clrscr();
    printf("\nEnter degree of equation : ");
    scanf("%d",&deg);
    ReadPolynomial(deg);
    BisectionMethod(deg,a,b);
    getch();
    }
    void ReadPolynomial(int deg)
    {
    int i,j,terms;
    terms = deg+1;
    for(i=0;i<terms;i++)
    {
    printf("\nEnter coefficient of x raised to the power %d : ",i);
    scanf("%d",&arr[i]);
    }
    }

    void BisectionMethod(int deg,double a,double b)
    {
    double x1,dx,fa,fx1,f;
    int nstep,i,j,terms;
    terms=deg+1;
    dx=b-a;
    nstep=0;
    f=1.0;
    fa=fx1=0.0;
    printf("\n\n\nBISECTION METHOD : \nn\ta\t\tb\t\tx1\n");
    while(fabs(dx)>eps)
    {
    x1=(a+b)/2;
    for(i=0;i<terms;i++)
    {
    for(j=0;j<i;j++)
    f=f*a;
    f=f*arr[i];
    fa+=f;
    }
    for(i=0;i<terms;i++)
    {
    for(j=0;j<i;j++)
    f=f*x1;
    f=f*arr[i];
    fx1+=f;
    }
    if((fa*fx1)<0)
    {
    b=x1;
    dx=b-a;
    }
    else
    {
    a=x1;
    dx=b-a;
    }
    nstep++;
    printf("\n%d\t%lf\t%lf\t%lf",nstep,a,b,x1);
    }
    printf("\nFinal Root : %lf", x1);
    }

  5. #5
    Registered User
    Join Date
    Sep 2010
    Location
    Halesowen, England
    Posts
    30
    Please use the [code][/code] tags around your source, it makes it easier for everyone to read.
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    #define MAX 50
    #define eps 1e-6
    void ReadPolynomial(int);
    void BisectionMethod(int,double,double);
    int arr[MAX];
    double a,b;
    void main()
    {
      int deg;
      clrscr();
      printf("\nEnter degree of equation : ");
      scanf("%d",&deg);
      ReadPolynomial(deg);
      BisectionMethod(deg,a,b);
      getch();
    }
    void ReadPolynomial(int deg)
    {
     int i,j,terms;
     terms = deg+1;
     for(i=0;i<terms;i++)
     {
      printf("\nEnter coefficient of x raised to the power %d : ",i);
      scanf("%d",&arr[i]);
     }
    }
    
    void BisectionMethod(int deg,double a,double b)
    {
      double x1,dx,fa,fx1,f;
      int nstep,i,j,terms;
      terms=deg+1;
      dx=b-a;
      nstep=0;
      f=1.0;
      fa=fx1=0.0;
      printf("\n\n\nBISECTION METHOD : \nn\ta\t\tb\t\tx1\n");
      while(fabs(dx)>eps)
    	{
    	 x1=(a+b)/2;
    	 for(i=0;i<terms;i++)
    	 {
    	  for(j=0;j<i;j++)
    		f=f*a;
    	  f=f*arr[i];
    	  fa+=f;
    	 }
    	 for(i=0;i<terms;i++)
    	 {
    	  for(j=0;j<i;j++)
    		f=f*x1;
    	  f=f*arr[i];
    	  fx1+=f;
    	 }
    	  if((fa*fx1)<0)
    	  {
    		b=x1;
    		dx=b-a;
    	  }
    	 else
         {
          a=x1;
          dx=b-a;
         }
    	 nstep++;
    	printf("\n%d\t%lf\t%lf\t%lf",nstep,a,b,x1);
      }
     printf("\nFinal Root : %lf", x1);
    }

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    ok... can u please help!!!??

  7. #7
    Registered User
    Join Date
    Sep 2010
    Location
    Halesowen, England
    Posts
    30
    At a quick glance, I can't see what's wrong with it.
    Why isn't it working? What incorrect outputs are you getting? What should the correct outputs be?

  8. #8
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Question

    Quote Originally Posted by Jamdog View Post
    Please use the [code][/code] tags around your source, it makes it easier for everyone to read.
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    #define MAX 50
    #define eps 1e-6
    void ReadPolynomial(int);
    void BisectionMethod(int,double,double);
    int arr[MAX];
    double a,b;
    void main()
    {
      int deg;
      clrscr();
      printf("\nEnter degree of equation : ");
      scanf("%d",&deg);
      ReadPolynomial(deg);
      BisectionMethod(deg,a,b);
      getch();
    }
    void ReadPolynomial(int deg)
    {
     int i,j,terms;
     terms = deg+1;
     for(i=0;i<terms;i++)
     {
      printf("\nEnter coefficient of x raised to the power %d : ",i);
      scanf("%d",&arr[i]);
     }
    }
    
    void BisectionMethod(int deg,double a,double b)
    {
      double x1,dx,fa,fx1,f;
      int nstep,i,j,terms;
      terms=deg+1;
      dx=b-a;
      nstep=0;
      f=1.0;
      fa=fx1=0.0;
      printf("\n\n\nBISECTION METHOD : \nn\ta\t\tb\t\tx1\n");
      while(fabs(dx)>eps)
    	{
    	 x1=(a+b)/2;
    	 for(i=0;i<terms;i++)
    	 {
    	  for(j=0;j<i;j++)
    		f=f*a;
    	  f=f*arr[i];
    	  fa+=f;
    	 }
    	 for(i=0;i<terms;i++)
    	 {
    	  for(j=0;j<i;j++)
    		f=f*x1;
    	  f=f*arr[i];
    	  fx1+=f;
    	 }
    	  if((fa*fx1)<0)
    	  {
    		b=x1;
    		dx=b-a;
    	  }
    	 else
         {
          a=x1;
          dx=b-a;
         }
    	 nstep++;
    	printf("\n%d\t%lf\t%lf\t%lf",nstep,a,b,x1);
      }
     printf("\nFinal Root : %lf", x1);
    }
    I hope you don't mind my using Jamdog's version since it's a pleasure to look at.

    I've gone through the trouble to verify that double a and double b are both zero
    in your function BisectionMethod. Checking back, although a & b are both global variables,
    where are they initialized?

    Also frustrating me, my compiler should have squawked about this... warnings are on
    high.... reason why I had to manually search for this.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    okk... but can you please correct the mistakes..!!??

  10. #10
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Angry

    Quote Originally Posted by sanskaar View Post
    okk... but can you please correct the mistakes..!!??
    I SPENT over an HOUR finding your mistake.

    NOW you want me to correct it?

  11. #11
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Pntr just told you what is wrong, and even marked it in your code, so you could understand it at a glance and correct it fast. (Whether or not these are the only mistakes here, I cannot tell)

    If you can't do that, then I have to wonder if you even wrote that code to begin with.

  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    noo.... just tell me what should i do??
    i have made changes in a and b but still cannot find the correct answer...!!
    please....!!

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What did you do to correct double a and b? They were global (above main), AND they were also being passed as parameters to your functions.

    Which generally means the global will be "covered up", and the local copy will be changed, only.

    Have you made double a and double b into local variables, declared in main() ? That would be my recommendation.

    I believe you need to post your changed code, so we can take a look at it, and a toast to Char*Pntr for his work in finding the errors, so far. << Well Done! >> Char*Pntr and Jamdog, also!

    USE CODE TAGS, THIS TIME!!
    Last edited by Adak; 09-26-2010 at 09:47 PM.

  14. #14
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    yes, i got the answer now.... and found my mistakes..
    sorry...!!
    n thanx Char*Pntr... .
    but still one problem...i have to calculate the interval in which the roots lie of the equation lie by passing a function..
    can you all please give some idea of how to do that in this same program code...??

  15. #15
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    i made the changes... and this program is working fine.
    but i am providing the intervals myself in the function bisection method. i dont have to do that i have to pass a function within the main function to calculate the interval,and frankly speaking i have no idea how to do that. please give me some ideas....
    here is the changed program code..
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    #define MAX 50
    #define eps 1e-6
    void ReadPolynomial(int);
    void BisectionMethod(int,double,double);
    int arr[MAX];
    void main()
    {
      int deg;
      double a,b;
      clrscr();
      printf("\nEnter degree of equation : ");
      scanf("%d",&deg);
      ReadPolynomial(deg);
      BisectionMethod(deg,a,b);
      getch();
    }
    void ReadPolynomial(int deg)
    {
     int i,j,terms;
     terms = deg+1;
     for(i=0;i<terms;i++)
     {
      printf("\nEnter coefficient of x raised to the power %d : ",i);
      scanf("%d",&arr[i]);
     }
    }
    
    void BisectionMethod(int deg,double a,double b)
    {
      double x1,dx,fa,fx1,f;
      int nstep,i,j,terms;
      a=0.0, b=1.0;
      terms=deg+1;
      dx=b-a;
      nstep=0;
      f=1.0;
      fa=fx1=0.0;
      printf("\n\n\nBISECTION METHOD : \nn\ta\t\tb\t\tx1\n");
      while(fabs(dx)>eps)
    	{
    	 x1=(a+b)/2;
    	 for(i=0;i<terms;i++)
    	 {
    	  for(j=0;j<i;j++)
    		f=f*a;
    	  f=f*arr[i];
    	  fa+=f;
    	 }
    	 for(i=0;i<terms;i++)
    	 {
    	  for(j=0;j<i;j++)
    		f=f*x1;
    	  f=f*arr[i];
    	  fx1+=f;
    	 }
    	  if((fa*fx1)<0)
    	  {
    		b=x1;
    		dx=b-a;
    	  }
    	 else
    	  {
    		a=x1;
    		dx=b-a;
    	  }
    	 nstep++;
    	printf("\n%d\t%lf\t%lf\t%lf",nstep,a,b,x1);
      }
     printf("\nFinal Root : %lf", x1);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newton Raphson Method
    By zeb1d1ah in forum C Programming
    Replies: 2
    Last Post: 12-07-2009, 06:26 AM
  2. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  3. Replies: 2
    Last Post: 01-22-2008, 04:22 PM
  4. Newton Raphson method code
    By taebin in forum C Programming
    Replies: 2
    Last Post: 10-17-2004, 02:44 AM
  5. Newton Raphson method code
    By taebin in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2004, 03:07 PM