vendredi 21 octobre 2016

if statements or while loop conditions?

So for an assignment, I'm supposed to write a function that evaluates two other functions that I previously defined in the program. How, the intricacies of this next function I must define is confusing me how I should set it up. Basically, I have determined that I have to write a while loop, but I can't decide whether to have three different conditions or if statements to evaluate this function. The thing is, the loop must end after one of the three conditions is met; I just don't know how to lay it out currently. These are the three conditions (where only one must be met in order for the loop/function to terminate):

  1. the absolute value of the polynomial at the current estimate is less than epsilon, in
    which case the method is successful.
  2. the absolute value of the derivative of the polynomial at the current estimate is less
    than epsilon (this could lead to division by 0), in which case the method failed, or
  3. the number of revisions of the estimate has exceeded timeout (the estimate is not converging on a solution). This case is also a failure.

This is currently what I have as my code (and it hasn't been running obviously):

def newtonsMethod(poly, x_, epislon, timeout):
    """Calculating root of polynomial using newton's Method."""
    estimate = 0
    epislon = 1e-20
    while (abs(evaluatePoly(poly, x_)) < epislon and \
abs(evaluatePoly(findDerivative(poly), x_)) and estimate > timeout):
        estimate = x_ - (evaluatePoly(poly) / evaluatePoly(findDerivative(poly)))
         x_ = estimate
         print(x_)

How should I go about this? The function name is a requirement of the assignment so it cannot be changed. Also, I am a complete beginner at this stuff (just started last month) and I'm only basically required to have knowledge of data structures, loops, and if statements (and functions). So please keep your responses as simple/dumby proof as possible.

Aucun commentaire:

Enregistrer un commentaire