Thread: anyone help me please

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    7

    Post anyone help me please

    hi im a retard and i cant get this to work any of you super chaps can help would be appreciated my code is
    #include<math.h>
    #include<stdio.h>

    double func(double x);
    double deriv(double x);

    double func(double x)
    {
    return pow(x,3)+5*pow(x,2)+x+sin(2*x);
    }

    double deriv(double x)
    {
    return 3*pow(x,2)+10*x+1+2*cos(2*x);
    }

    main()
    {
    double delta,x,y;
    y=0;
    printf("Insert Initial Guess\n");
    scanf("%f",&x);
    printf("The value of function =%f\n",func(x));
    while(y!=x)
    {
    y=x;
    delta = (-func(x)/deriv(x));
    x = x + delta;
    }
    printf("The function equals %8.4f at x=%f\n",func(x));
    }

    im trying to find the roots of the function but it doesn't work, ill love you if you can help.

  2. #2
    Registered User Dev's Avatar
    Join Date
    Mar 2003
    Posts
    59
    I don't understand why you are using variable y.

    The loop seems to be wrong. In Newton Raphson method the one you are using you have to continue until you get value of function near to zero and not the root equal to zero.

    For your question i think 0 is the only answer.

    So change your loop to something like

    while(func(x)!=0)
    {
    ---
    }

    And it should work fine .

    And one big problem is that while scanning and printing values you are using %f modifier while your values are all double.


    Change %f to %lf.

  3. #3
    Registered User Dev's Avatar
    Join Date
    Mar 2003
    Posts
    59
    And one more thing i forgot.

    Instead of using this method in which you know function and its derivative.

    Use standard method say start with any initial guess.

    Find value of function if its equal to zero then its the root otherwise increase that guess by some amount.

    It's not the best way but doesn't involve derivatives atleast.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Daaaaamn! Newton would roll over in his grave if he saw this code!!

    Just kidding.

    It looks like you were thinking of testing for convergence by "if (delta == 0)", which you tried to express as "while (y !=x)". But has Dev pointed out, y isn't doing anything for you.

    Here's Newtons method
    Code:
    FOR n = 1 to m DO
       x = x - f(x)/f'(x)
    ENDFOR
    Here, m is an arbitrary value. Normally, you'll want to run until you've reached an appropriate amount of convergance. In C, this can be until you've exhausted the precision of a float or double (ie. delta == 0).

    gg

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    thanks for the support

    im new to this, and im a physics student with this leet programming course to attempt, thanks thou

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    Originally posted by Dev
    And one more thing i forgot.

    Instead of using this method in which you know function and its derivative.

    Use standard method say start with any initial guess.

    Find value of function if its equal to zero then its the root otherwise increase that guess by some amount.

    It's not the best way but doesn't involve derivatives atleast.
    would love to but i gotta use this silly newton-raphson method

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    right, i fixed it and it works incase anyone cares its now
    Code:
    #include<math.h>
    #include<stdio.h>
    
    double func(double x);
    double deriv(double x);
    
    double func(double x)
    {
      return pow(x,3)+5*pow(x,2)+x+sin(2*x); 
    }
    
    double deriv(double x)
    {
      return 3*pow(x,2)+10*x+1+2*cos(2*x);
    }
    
    main()
    {
    	double delta,x,y;
    	y,delta=0;
    	printf("Insert Initial Guess\n");
                    scanf("%lf",&x);
    	do
    		{
    			y=x;
    			delta = (-func(x)/deriv(x));
    			x = x + delta;
    		}
    	while(y!=x);				
            printf("The root is at %lf\n",x);
    				
    }

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    It looks like you were thinking of testing for convergence by "if (delta == 0)", which you tried to express as "while (y !=x)". But has Dev pointed out, y isn't doing anything for you.
    I thought your code looked right, Dev messed me up

    gg

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    i used the old fashioned, change all the %f -> %lf so the code actually makes some sense

Popular pages Recent additions subscribe to a feed