Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6337997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:21:31+00:00 2026-05-24T19:21:31+00:00

I have this code for solving Newton’s method and in the last iteration, the

  • 0

I have this code for solving Newton’s method and in the last iteration, the output values have come wrong. Those values should not be negative as I checked it manually on paper. As far as I know the code is right but I cannot figure out why is it displaying negative values and also the final u value should ideally be a positive value in between 0 and 1. Here is the code:

import copy
import math

tlist = [0.0, 0.07, 0.13, 0.15, 0.2, 0.22] # list of start time for the phonemes

w = 1.0

def time() :
    t_u = 0.0
    for i in range(len(tlist)- 1) :
        t_u = t_u + 0.04 # regular time interval
        print t_u
        print tlist[i], ' ' , tlist[i+1], ' ', tlist[i -1]
        if t_u >= tlist[i] and t_u <= tlist[i + 1] :
            poly = poly_coeff(tlist[i], tlist[i + 1], t_u)
            Newton(poly) 
        else :
            poly = poly_coeff(tlist[i - 1], tlist[i], t_u)
            Newton(poly) 

def poly_coeff(start_time, end_time, t_u) :
    """The equation is k6 * u^3 + k5 * u^2 + k4 * u + k0 = 0. Computing the coefficients for this polynomial."""
    """Substituting the required values we get the coefficients."""
    t0 = start_time
    t3 = end_time
    t1 = t2 = (t0 + t3) / 2
    w0 = w1 = w2 = w3 = w
    k0 = w0 * (t_u - t0)
    k1 = w1 * (t_u - t1)
    k2 = w2 * (t_u - t2)
    k3 = w3 * (t_u - t3)
    k4 = 3 * (k1 - k0)
    k5 = 3 * (k2 - 2 * k1 + k0)
    k6 = k3 - 3 * k2 + 3 * k1 -k0 

    print k0, k1, k2, k3, k4, k5, k6
    return [[k6,3], [k5,2], [k4,1], [k0,0]]

def poly_differentiate(poly):
    """ Differentiate polynomial. """
    newlist = copy.deepcopy(poly)

    for term in newlist:
        term[0] *= term[1]
        term[1] -= 1

    return newlist

def poly_substitute(poly, x):
    """ Apply value to polynomial. """
    sum = 0.0 

    for term in poly:
        sum += term[0] * (x ** term[1])
    return sum

def Newton(poly):
    """ Returns a root of the polynomial"""
    x = 0.5  # initial guess value 
    epsilon = 0.000000000001
    poly_diff = poly_differentiate(poly) 

    while True:
        x_n = x - (float(poly_substitute(poly, x)) / float(poly_substitute(poly_diff, x)))

        if abs(x_n - x) < epsilon :
            break
        x = x_n
        print x_n
    print "u: ", x_n
    return x_n

if __name__ == "__main__" :
    time()

The output for the last iteration is the following,

where k6 = -0.02, k5 = 0.03, k4 = -0.03 and k0 = 0.0

0.2
0.2   0.22   0.15
0.0 -0.01 -0.01 -0.02 -0.03 0.03 -0.02
-0.166666666667
-0.0244444444444
-0.000587577730193
-3.45112269878e-07
-1.19102451449e-13
u: -1.42121180685e-26

The initial guess value is 0.5 so if it is substituted in the polynomial then the output is -0.005.

Then again the same initial value is substituted in the differentiated polynomial. The result of that is -0.015.

Now these values are substituted in the Newton’s equation then the answer should come as 0.166666667. But the actual answer is a negative value.

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:21:32+00:00Added an answer on May 24, 2026 at 7:21 pm

    Ah, I see now.

    Just as you say,

    float(poly_substitute(poly, x))
    

    evaluates to -0.015. Then,

    float(poly_substitute(poly_diff, x))
    

    evaluates to -0.01. Thus, substituting for these values and for x,

    x_n = 0.5 - ( (-0.015) / (-0.01) )
    x_n = 0.5 - 0.6666666...
    x_n = -0.166666...
    

    Your manual math was what was at fault, not the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code <?php session_start(); if (isset($_GET[cmd])) $cmd = $_GET[cmd]; else die(You should
I have this code in jQuery, that I want to reimplement with the prototype
I have this code: chars = #some list try: indx = chars.index(chars) except ValueError:
I have this code that performs an ajax call and loads the results into
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
I have this code: CCalcArchive::CCalcArchive() : m_calcMap() { } m_calcMap is defined as this:
I have this code: myVariable which I want to change into trace(myVariable: + myVariable);
I have this code while($row = mysql_fetch_row($result)) { echo '<tr>'; $pk = $row[0]['ARTICLE_NO']; foreach($row
I have this code, which works fine, but I would like to be able
I have this code :- using (System.Security.Cryptography.SHA256 sha2 = new System.Security.Cryptography.SHA256Managed()) { .. }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.