# Thread: Need help with math equation.

1. ## Need help with math equation.

Hi. I really need someone to help me with this.

I have to create a program that solve this equation using Newton's method.

cosx - 0.9 + x*x/3 = 0

This is the code that i've witten. I keep getting error everytime i try to compile the code. Did i wrote the equation correctly for the cos and sine part?

Also, for the 'no of iteration' part, how should i do to make the output becomes 1,2,3,4,.... instead of 1.00000,2.00000,3.00000,...

Code:
```#include<stdio.h>
#include<math.h>

double f( double x ) {
return cos(x)-0.9+x*x/3;
}

double diff( double x ) {
return -sin(x)+2*x/3;
}

double newton( double initial ) {
double e = 0.000001 ;//
double x1, x2;
double a;
x2 = initial;
a = 0;

while( 1 ) {
x1 = x2 - f(x2)/diff(x2) ;    // The formula
if( fabs(x1-x2) < e ) {
return x1;
} else {
x2 = x1;
a = a+1;
printf( "New value of x is %f.Ends of iteration %f\n", x1, a);
}
printf( "Total no of iteration is %f\n", a);
}
}

int main() {
double x0 = 1.0 ;     // initial value of x
printf( "Final answer is x =%f\n", newton(x0));
}```

2. What is the error message and which line is indicated?

3. I'm using online c compiler btw. These are what written.

sh-4.2# gcc -o main *.c
/tmp/ccV7wHzd.o: In function `f':
main.c.text+0x1b): undefined reference to `cos'
/tmp/ccV7wHzd.o: In function `diff':
main.c.text+0x92): undefined reference to `sin'
collect2: error: ld returned 1 exit status
sh-4.2#

4. Looks like you need to link in the math library. It looks like gcc, so somewhere you probably need to add -lm. I'm not sure which site you're using, but there should be a help page describing how to do this.
Originally Posted by syahidfz
Did i wrote the equation correctly for the cos and sine part?
Why do you think it's wrong? Please describe in detail what is wrong if you are having problems with the equations. For example, what output do you get for a given initial value and what should the correct output be?
Originally Posted by syahidfz
Also, for the 'no of iteration' part, how should i do to make the output becomes 1,2,3,4,.... instead of 1.00000,2.00000,3.00000,...
Don't use a float for counting iterations

5. I'm using this site :Compile and Execute C Online, (An Online C Compiler) . Do you know anywhere else i can go to get my code compiled?

Originally Posted by anduril462
Looks like you need to link in the math library. It looks like gcc, so somewhere you probably need to add -lm. I'm not sure which site you're using, but there should be a help page describing how to do this.

I'm completely newbie on c language so i don't really know, and since i'm getting error mentioning about cos and sine everytime i try to compiled the code, so i thought maybe i wrote the equation wrongly. Btw, the answer to that equation is not given.

Originally Posted by anduril462
Why do you think it's wrong? Please describe in detail what is wrong if you are having problems with the equations. For example, what output do you get for a given initial value and what should the correct output be?
I try using int before but the numbers become 0.00001,0.00002,0.00003,....
Originally Posted by anduril462
Don't use a float for counting iterations

6. Originally Posted by syahidfz
I'm using this site :Compile and Execute C Online, (An Online C Compiler) . Do you know anywhere else i can go to get my code compiled?
There are several good, free compilers for whatever platform you're using. GCC for Linux is pretty standard. MinGW is the Windows Equivalent. Windows also has Pelles C.
The one you are using looks like a pretty good one though. Just a guess, but go to Project->Compile Options. Add -lm to the end of the Compilation Command field.
Originally Posted by syahidfz
I try using int before but the numbers become 0.00001,0.00002,0.00003,....
Did you try changing your printf strings to use the int format specifier %d where appropriate?

7. I try adding the -lm just like u said and it works fine now. Thank you so much for your help. Really appreciate it.

Originally Posted by anduril462
There are several good, free compilers for whatever platform you're using. GCC for Linux is pretty standard. MinGW is the Windows Equivalent. Windows also has Pelles C.
The one you are using looks like a pretty good one though. Just a guess, but go to Project->Compile Options. Add -lm to the end of the Compilation Command field.

Did you try changing your printf strings to use the int format specifier %d where appropriate?