It is sometimes not possible. For instance if all 3 corners have the same height, then you know that the peak must be in the middle, but have no way of figuring out how high that peak is.

That said, you have 4 measurements, and you need to figure out 4 variables - the x-coordinate of the peak, the y-coordinate of the peak, the height of the peak, and the slope of the mountain. That means that normally (barring degenerate cases, such as the peak being in the middle) there should be a unique answer. (Or a finite number of them - however a glance at this situation suggests that it will normally be unique.) But that doesn't make it easy to find.

I would have to resort in this case to numerical approximations. You can use brute force guessing to come up with a good combination. Or you can try to be clever.

If I wanted an easy answer to code, I'd use brute force. If I wanted to be clever, I'd use Newton's method.

To refresh your memory, Newton's method works like this. For x near x0, f(x) is approximately

f(x0) + f'(x0)*(x - x0)
Now suppose that we want k = f(x). Then if x is close to x0 then we expect the above approximation to be close to k. Which means that x - x0 is going to be close to:
x0 + (f'(x0))**(-1) * (k - f(x0))
So Newton's method starts with finding one approximation x0, using the above to find a hopefully better approximation x1, which is used to improve to x2 and so on.

If you start off a long ways away from an approximation, you can still use Newton's method. What you do is check how good an approximation you have. If in a single step your approximation gets worse, then that is because you are travelling farther than the tangent line approximation is good for. But you know that if you just go a short distance in that direction, you should get better. So try travelling in that direction half the distance, 1/4, 1/8 and so on until you find a fraction that is an actual improvement. After improving a few times, then you should get close enough to a real answer that Newton's method just works for you.

Now I see you objecting that you can do this for functions of one variable, and here you have functions of many. Well while it seems more complex, this works for functions of many variables as well! All that you have to remember is that x is a vector and f'(x) is a matrix.

Which matrix? It is a mess of partial derivatives. I'm too lazy to type in what mess of which partial derivatives, but it can be found written in lots of places. Also it is trivial to figure out the terms by remembering two facts. The first is that the tangent approximation has to hold in every output dimensions for changes in any one input dimension. The second is the rule for matrix multiplication. When you write those two down, it is easy to see that only one set of partial derivatives can possibly work.

To figure out the answer, you'll also need to invert lots of matrices. If you don't want to use PDL, then remember that the fastest way to do invert M is to write [M | I] (where I is the identity matrix) and then apply row operations (multiply row by scalar, swap rows, add multiple of one row to another) to turn that into [I | N]. When you have done so, then N is the inverse of M. (This requires an average of O(n**3) operations. The well-known formula is exponential in the dimension.)

Of course Cramer's rule may be faster for you to program, and it isn't THAT much slower in 4 dimensions...


In reply to Re: OT:Math problem: Grids and conical sections. by tilly
in thread OT:Math problem: Grids and conical sections. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.