Thread: Need help with math equation.

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    4

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the error message and which line is indicated?
    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
    Feb 2015
    Posts
    4
    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. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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.
    Quote Originally Posted by syahidfz View Post
    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?
    Quote Originally Posted by syahidfz View Post
    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. #5
    Registered User
    Join Date
    Feb 2015
    Posts
    4
    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?

    Quote Originally Posted by anduril462 View Post
    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.

    Quote Originally Posted by anduril462 View Post
    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,....
    Quote Originally Posted by anduril462 View Post
    Don't use a float for counting iterations

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by syahidfz View Post
    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.
    Quote Originally Posted by syahidfz View Post
    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. #7
    Registered User
    Join Date
    Feb 2015
    Posts
    4
    I try adding the -lm just like u said and it works fine now. Thank you so much for your help. Really appreciate it.

    Quote Originally Posted by anduril462 View Post
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math equation tokenization
    By Epy in forum Tech Board
    Replies: 1
    Last Post: 12-15-2011, 10:05 AM
  2. Math equation help!!
    By alex33zebras in forum C Programming
    Replies: 2
    Last Post: 09-27-2011, 03:31 PM
  3. How to do a math equation in c Programming
    By jabber5050 in forum C Programming
    Replies: 6
    Last Post: 09-08-2011, 01:08 PM
  4. C++ math equation
    By XodoX in forum C++ Programming
    Replies: 22
    Last Post: 03-02-2009, 12:02 AM
  5. A math equation
    By dizolve in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-17-2002, 12:56 PM