1. ## 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. 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.

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. And one more thing i forgot.

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

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. 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. thanks for the support

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

6. Originally posted by Dev
And one more thing i forgot.

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

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. 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. 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. i used the old fashioned, change all the %f -> %lf so the code actually makes some sense