Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Finding Numerically the solution line for an equation

  1. Dec 10, 2012 #1
    I have a function which depends say upon two variables: [itex] f(x,y) [/itex].

    I need to find numerically the solution line [itex] y(x) [/itex] of the equation [itex] f(x,y) = 0 [/itex]. The function itself for arbitrary set of {x,y} may evaluate to some complex number (it involves lots of square roots) but for the solution I am seeking the imaginary part on the solution line should be zero. I understand that one in principal can scan through the whole region of {x,y} to get the curve but I need numerically efficient way to write the code.

    Thanks.
     
  2. jcsd
  3. Dec 11, 2012 #2

    haruspex

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    Is f(x,y) differentiable analytically? I'll assume not, but I will assume it's continuous.
    If you can feed in a feasible range for y(x0) then you could program a binary chop on the values. (Do I need to explain that?) Having found one point on the line, (x0, y0), you could then search for solutions of f(x,y) = 0 where the |(x,y)-(x0, y0)| = some small r. Again, you could do a binary chop on the direction from (x0, y0) to (x,y).
     
  4. Dec 11, 2012 #3
    What is "binary chop"?
     
  5. Dec 11, 2012 #4

    HallsofIvy

    User Avatar
    Staff Emeritus
    Science Advisor

    If [itex]f(x_0, y_0)> 0[/itex] and [itex]f(x_1, y_1)< 0[/itex] then, because f is continuous, there must be a point between [itex](x_0, y_0)[/itex] and [itex](x_1, y_1)[/itex] where f is 0. We have no idea where so checking one point is as good as another- use the midpoint, [itex](x_2, y_2)= ((x_0+ x_1)/2, (y_0+ y_1)/2)[/itex]. Now look at [itex]f(x_2, y_2)[/itex]. If it is 0, we are done! If not, it is either positive or negative. If it is positive, then, since [itex]f(x_1, y_1)< 0[/itex], there must be a solution between them. If it is negative, then, since [itex]f(x_0, y_0)> 0[/itex], there be a solution between them. In either case, define [itex](x_3, y_3)[/itex] to be the mid point of that interval.

    That is, I think, what haruspex is calling a "binary chop". I would call it a "binary search".
     
    Last edited: Dec 12, 2012
  6. Dec 11, 2012 #5
    My concern is that f(x,y) does not evaluate necessarily to real value, it might as well be complex, so it being positive or negative is not well defined.
     
  7. Dec 11, 2012 #6

    haruspex

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    That's not a problem. 0 is 0, and the modulus of f is zero if and only if f is 0.
     
  8. Dec 11, 2012 #7
    But then if you are taking |f(x,y)| how do you implement the comparisons of where it is positive and where negative?
     
  9. Dec 11, 2012 #8

    haruspex

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    Ooops! Yes, you're right, sorry. Let me think some more.
     
  10. Dec 11, 2012 #9

    haruspex

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    OK, instead of doing the binary chop based on the sign of f, you can look for local minima of |f|. This is a bit trickier because you need to track three 'current' points. E.g. if |f(x,y0)| > |f(x,y1)| < |f(x,y2)|, y0 < y1 < y2, then you know there is a min between y0 and y2. So next pick y3 = (y0 + y2). If |f(x,y3)| > |f(x,y1)| then you know there's one between y3 and y2. But it might turn out to be > 0, and there could be a min between y0 and y3.
     
  11. Dec 11, 2012 #10
    but in that case you would just find local minima of the function, not zeros.
     
  12. Dec 11, 2012 #11

    haruspex

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    Since |f| ≥ 0, every zero will be a local minimum. So if you find all the minima you will find all the zeroes.
     
  13. Dec 11, 2012 #12
    But not all local minima are the zeros
     
  14. Dec 12, 2012 #13
    And another problematic thing with what you suggest is that you have to literally scan all x values, the problem is like for single variable case. I was more looking to be able to dispense with scanning x fully.
     
  15. Dec 12, 2012 #14

    haruspex

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    Quite so.
    As I understood it, you want to plot y as a function of x. So you would choose whatever set of x values then for each one try to find that y for which f(x,y)=0. All I can offer is some way to find those corresponding y values.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook