Thread: Newton raphson

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    Lightbulb Newton raphson

    hello everyone, i'm trying to generate a root for a polynomial of order 10 using the Newton Raphson Method...
    my ten order polynomial is in the form: P(x) = a0 + a1x + a2x2 + a3x3+ ... + a10x10
    my first estimate is 1.5 and the iterations should continue until the difference between the two successive estimates is less than the acceptable error margin which is 0.0000001 or if it exceeds 70 iterations.....
    here's my code sofar, though it still has a few issues...

    Code:
    #include<stdio.h>
    
    #include<math.h>
    
    #include<stdlib.h>
    
    #define H 0.00000001
    
    #define exit
    
    
    
    //Initialize variables
    
    float f(float);
    
    float df(float);
    
    void readf();
    
    float *p;
    
    int n=10;
    
    
    
    //Main Function
    
    void main()
    
    void readf()
    
    {
    
    float x=1.5, t, root, It=70;
    
    int count =1;
    
    
    
    p=(float *)malloc((n+1)*sizeof (float));
    
    if (fabs(f(x))&&It==H)
    
    {
    
    root=x;
    
    {
    
    	while(1)
    
       	{
    
       t=x-f(x)/df(x);
    
    
    
    //Output Iterattions
    
    if(count && It==70)
    
    printf("\nIteration %d",count);
    
    else
    
    printf("\nThe iterations are too many");
    
    exit
    
    printf("\nFor x%d -&gt; %f, f(x%d) -&gt; %f\n\n",count,t,count,f(t));
    
    if (count%70==0)
    
    {
    
    printf("\n\nEnter any Key to proceed");
    
    }
    
    if(fabs((x-t)/t)&&It==H)
    
    {
    
    root=t;
    
    break;
    
    }
    
    else
    
    x=t;
    
    count++;
    
    }
    
    }
    
    printf("\n\nThe root of the equation is %f\n",root);
    
    printf("\n\nThe total number of iterations is %d",count);
    
    free(p);
    
    }
    
    int i;
    
    for(i=n;i&&gt;=0;i--)
    
    {
    
    printf("Enter coefficient of a^%d",i);
    
    scanf("%f",p+i);
    
    }
    
    printf("Polynomial equation entered\n\n");
    
    for(i=n;i&&gt;=0;i--)
    
    if(*(p+i)!=0)
    
    {
    
    printf("=0\n");
    
    }
    
    float f(float x);
    
    {
    
    int i
    
    float sum=0;
    
    for(i=n;i&&gt;=0;i--)
    
    sum+=*(p+i)*pow(x,i);
    
    return sum;
    
    }
    
    float df(float x);
    
    {
    
    return ((f(x+H)-f(x))/H);
    
    }

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    1. Indent your code please so that it could be readable..
    2. It's recommended to use int main().
    3. Your main has no body??
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Code:
    #define H 0.00000001
    
    #define exit
    What is this?

    Code:
    //Initialize variables
    float f(float);
    float df(float);
    void readf();
    float *p;
    int n=10;
    What "variables" did you want to initialize here?
    f and df are functions as I can see.
    You cannot have a function inside another function (see your main()).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using newton raphson method
    By sanskaar in forum C Programming
    Replies: 21
    Last Post: 09-27-2010, 12:02 AM
  2. Newton Raphson Method
    By zeb1d1ah in forum C Programming
    Replies: 2
    Last Post: 12-07-2009, 06:26 AM
  3. Newton + Einstein were wrong!
    By Jez in forum A Brief History of Cprogramming.com
    Replies: 64
    Last Post: 12-14-2004, 02:24 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

Tags for this Thread