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

  • Home
  • SEARCH
  • 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 6213431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:41:45+00:00 2026-05-24T06:41:45+00:00

I am trying to write a data calculated from this function in a file.

  • 0

I am trying to write a data calculated from this function in a file. But the function is called number of times. Say there are 9 numbers in another file and this function will calculate the root for each of those 9 numbers. These 9 roots from this function should be written in the same file. But the way I have done it here will write calculated root in the file but the next one will replace this in the file. There are other mathematical functions that are carried out for each of those 9 numbers before this function is called therefore the functions are called again and again separately.Is it possible to write them all in the same file? Thank you.

def Newton(poly, start):
    """ Newton's method for finding the roots of a polynomial."""
    x = start 
    poly_diff = poly_differentiate(poly)
    n = 1
    counter = 0
    r_i = 0

    cFile = open("curve.dat", "w")   
    while True:
        if (n >= 0) and (n < 1):
            break

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

        if x_n == x:
            break

        x = x_n # this is the u value corresponding to the given time

        n -= 1
        counter += 1
        x = str(x)
        cFile.write('\n' + x + '\n')

    if r_i:
        print "t(u) = ", (x, counter)

    else:
        print "t(u) = ", x


    cFile.close 

After following the suggestions I got I changed the code to the following:

def Newton(poly, start):
    """ Newton's method for finding the roots of a polynomial."""
    x = start 
    poly_diff = poly_differentiate(poly)
    n = 1
    counter = 0

    while True:
        if (n >= 0) and (n < 1):
            break

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

        if x_n == x:
            break

        x = x_n # this is the u value corresponding to the given time

        n -= 1
        counter += 1
        yield x 

    Bezier(x)

def Bezier(u_value) :
    """ Calculating sampling points using rational bezier curve equation"""
    u = u_value

    p_u = math.pow(1 - u, 3) * 0.7 + 3 * u * math.pow(1 - u, 2) * 0.23 \
        + 3 * (1 - u) * math.pow(u, 2) * 0.1 + math.pow(u, 3) * 0.52

    p_u = p_u * w

    d = math.pow(1 - u, 3) * w + 3 * u * w * math.pow(1 - u, 2) + 3 * (1 - u) *\ 
        w * math.pow(u, 2) + math.pow(u, 3) * w

    p_u = p_u / d

    yield p_u

    plist = list (p_u)
    print plist

I followed the same thing in the Bezier() function but plist is not created as it doesn’t print anything. Please help. 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-24T06:41:46+00:00Added an answer on May 24, 2026 at 6:41 am

    Your function does two things: It calculates the roots of a polynomial, and it writes the result to an output file. Functions should ideally do one thing.

    So, try breaking this up into a function that receives a polynomial and returns a list containing the roots, and then just write that list to a file in one step.

    The simplest way to modify your function would be to replace the lines

    x = str(x)
    cFile.write('\n' + x + '\n')
    

    with

    yield x
    

    Then you can call your function like this:

    roots = list(Newton(polynomial, start))
    

    To understand this, read about generators. To write the resulting list to a file, you can use this code:

    with open("curve.dat", "w") as output_file:
        output_file.write("\n".join(str(x) for x in roots)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a query that extracts and transforms data from a table
I am trying to use the following code to write data into an excel
I'm trying to write a C# program, where when a user enters some data
I'm trying to write a regex function that will identify and replace a single
I'm trying to write some PHP to upload a file to a folder on
I am trying to write a query to create a 'table' of data as
I am trying to write a simple program for this interview question: Write a
I'm trying write a query to find records which don't have a matching record
Trying to write a PowerShell cmdlet that will mute the sound at start, unless
I'm trying to write a blog post which includes a code segment inside a

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.