Thread: How do I solve this logarithmic equation?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    1

    How do I solve this logarithmic equation?

    Hello everyone, my homework is writing a prog. solving
    "log y=-4.4132+0.0302x" (y is equal to x) equation by iteration. What program will do is beginning with x=0 then finding a y value. After putting this y value in place of x in the equation, again finding a new y value and this continues until we find y=x.

    y is approx. 3.86*10 to the power (-5).

    I am not good at C. I dont expect you to do the entire hw. but if you help me showing a solution way I will be glad.Thanks..

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by unicorn View Post
    Hello everyone, my homework is writing a prog. solving
    "log y=-4.4132+0.0302x" (y is equal to x) equation by iteration. What program will do is beginning with x=0 then finding a y value. After putting this y value in place of x in the equation, again finding a new y value and this continues until we find y=x.

    y is approx. 3.86*10 to the power (-5).

    I am not good at C. I dont expect you to do the entire hw. but if you help me showing a solution way I will be glad.Thanks..
    That's alright, I'm so rusty with math, I think a log is something for the fireplace.

    A very simple and straight forward approach would be something like: (in rough code)

    Code:
     
    double step = *VERY* tiny incremental amount or step. 
    double y = incredibly small value. It MUST be less than y + step's real value.
    
    do {
       y += step;
       log y =  -4.4132+0.0302y
      
    }while (log y !=  -4.4132+0.0302y);
    All the above is good, but it may not ever resolve on y's value. Especially if the step value is not oh-so-tiny.

    A more elaborate idea would be to have two loops, in separate functions. StepUp, would be the above loop, StepDown would be just like that do loop, EXCEPT the stop condition would be like:

    Code:
    }while (log y <= -4.4132+0.0302y);
    
    and for StepDown:
    y = incredibly large value. MUST be greater than y - step's real value.
    y -= step;  /* <--note the minus just before the = sign here */
    
    }while (log y >= -4.4132+0.0302y);
    Now you call these functions, and then give you an approximation for y, but not exact. Now you call those same functions back, but with much tighter granularity for step, for both functions.

    Repeat calls to these functions, alternating back and forth, until y is resolved. Step would be come smaller and smaller after every run through of the two functions, and y's starting value would be raised by StepUp, and lowered by StepDown, each time through them, as well.

  3. #3
    Registered User alreadyinuse's Avatar
    Join Date
    Dec 2007
    Posts
    12
    you could use newton method to find zeroes to an equation like :

    log y +A +B y = 0

    http://en.wikipedia.org/wiki/Newton's_method
    EDIT: read also the "counter example" section to see when this method does not work.
    Last edited by alreadyinuse; 01-16-2008 at 06:59 AM.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I do not really see a reason why when asked a help to implement one specific method of solving the equation everyone starts to give advices how to use other methods for this purpose?

    I think the OP should slightly change the equation to bring it to form

    y = f(x);

    when write a function
    double f(double x)

    that calculates the above
    and then write a loop
    Code:
    do
    {
       x = y ;
       y = f(x);
    }while(abs(y-x) > delta);
    properly initializing y and delta before loop.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User alreadyinuse's Avatar
    Join Date
    Dec 2007
    Posts
    12
    Quote Originally Posted by vart View Post
    I do not really see a reason why when asked a help to implement one specific method of solving the equation everyone starts to give advices how to use other methods for this purpose?
    because that method will never solve the equation.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by vart View Post
    I do not really see a reason why when asked a help to implement one specific method of solving the equation everyone starts to give advices how to use other methods for this purpose?

    I think the OP should slightly change the equation to bring it to form

    y = f(x);

    when write a function
    double f(double x)

    that calculates the above
    and then write a loop
    Code:
    do
    {
       x = y ;
       y = f(x);
    }while(abs(y-x) > delta);
    properly initializing y and delta before loop.
    What good would properly initializing y do, when your first command in the do loop, is to reset it's value to the value of x?

    To converge on an answer, your logic should show something that converges. Perhaps that was implied, but it is not explicitly shown.

    Can you elaborate on that, Vart, keeping in mind that I wouldn't know "proper form" from a Cocker Spaniel, here?

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by alreadyinuse View Post
    because that method will never solve the equation.
    http://en.wikipedia.org/wiki/Iterative_method

    You should go back to school
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Adak View Post
    What good would properly initializing y do, when your first command in the do loop, is to reset it's value to the value of x?
    Maybe it is because I set the value of x to be equal to y?


    Quote Originally Posted by Adak View Post
    Can you elaborate on that, Vart, keeping in mind that I wouldn't know "proper form" from a Cocker Spaniel, here?
    Elaborate what?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by vart View Post
    Maybe it is because I set the value of x to be equal to y?



    Elaborate what?
    Well the x, y thing is solved with a few less cobwebs from the Sandman, and perhaps a bit of caffeine.

    I'll read your link, it probably shows how your loop converges on an answer.

    Unfortunately, the link has nothing really useful unless you're into calculus.
    Last edited by Adak; 01-17-2008 at 04:29 AM.

  10. #10
    Registered User alreadyinuse's Avatar
    Join Date
    Dec 2007
    Posts
    12
    Quote Originally Posted by vart View Post
    I know what an iterative method is, otherwise I would have not suggest newton method.

    The problem is that to use your method you have to be sure that the solution is an attractive fixed point. example:

    y=f(x)= -3 + 5x
    if I want to solve y=f(y) I cannot use your method:
    x=0 y= -3
    x=-3 y=-18
    and so on.

    Now, unicorn is trying to solve:
    log y=-4.4132+0.0302x
    that on a Real field should be equal to:
    y=f(x)=exp(-4.4132+0.0302x)
    with y=f(y).
    If I solved it well there are 3 fixed points, but only 2 are stable/attractive:
    a=0.0121 b=339 c=infinity

    with your method you can find "a" and (in theory) "c" but you cannot find "b" because "b" is an unstable fixed point.

    Generally speaking that method is really weak as you cannot see at a glance all the properties of the function.

    And yes, I will go back to school....

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by alreadyinuse View Post
    with your method you can find "a" and (in theory) "c" but you cannot find "b" because "b" is an unstable fixed point.
    So what's your point? It is not my method - it is method OP needs to implement. Even if it has some limitations.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. solve linear equation
    By unix7777 in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2008, 11:47 PM
  2. Replies: 15
    Last Post: 11-04-2006, 04:06 AM
  3. IDEA: Equation solver
    By Magos in forum Contests Board
    Replies: 2
    Last Post: 01-07-2003, 11:46 AM
  4. Equation solving
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-24-2002, 02:13 PM
  5. Quadratic Equation Program
    By Ambizzy in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 09:21 PM