Question

4.10 Team Activity 4 Create a program that will approximate the roots of an equation (i.e....

4.10 Team Activity 4 Create a program that will approximate the roots of an equation (i.e. solution to the equation f(x) = 0) using Newton's Method. Newton's Method is an iterative process that approximates the solution using the equation: alt text For this activity, you will approximate the solution for f(x) = x^2. (The derivative of this function is f'(x) = 2x). Your program should accept two user inputs: the number of iterations to perform, i (integer) the starting guess , x0 (float) then output the resulting values of x for each iteration (including the starting guess). For example, if the user wants to perform 3 iterations with a starting guess of x0=1, the result should look like: x 0 = 1.0 x 1 = 0.5 x 2 = 0.25 x 3 = 0.125 Note: The results of this problem have varying numbers of decimal places. Therefore, your print statement should either: (a) use commas to separate strings and variables OR (b) use variable replacement fields with the format() function. If you use replacement fields, we recommend { } in place of {:f} or {:d} which will output the wrong number of decimal places (see zybooks section 2.13 String formatting)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code

#method to calculate the roots using Newton's method
def calculate_roots(num_iterations, starting_guess):
    #initializing x to starting_guess
    x=starting_guess
    #printing starting values
    print('x 0 = {}'.format(x))
    #looping from i=1 to num_iterations
    for i in range(1,num_iterations+1):
        #finding new x (next term) as x^2 divided by 2x
        x=x-(x**2)/(2*x)
        #printing these values after each iteration
        print('x {} = {}'.format(i,x))
    #returning final value of x
    return x


#asking, reading number of iterations and starting guess
i=int(input("Please enter the number of iterations: "))
x0=float(input("Please enter the starting guess: "))
#calculating roots and storing the result
result=calculate_roots(i,x0)
#printing final result, remove if you don't need this.
print('Final result is {}'.format(result))

#output

Please enter the number of iterations: 3
Please enter the starting guess: 1
x 0 = 1.0
x 1 = 0.5
x 2 = 0.25
x 3 = 0.125
Final result is 0.125

Add a comment
Know the answer?
Add Answer to:
4.10 Team Activity 4 Create a program that will approximate the roots of an equation (i.e....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any a...

    Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any arbitrary function f given an initial guess xo, an absolute error tolerance e and a maximum number of iterations max iter. Follow mynewton.m template posted in homework 2 folder on TritonED for guidance. You are not required to use the template. The function should return the approximated root ^n and the number of steps n taken to reach the solution. Use function mynewton.m to perform...

  • QUESTION 1 = = (a) Apart from x = 0 the equation f(x) 22 – 4...

    QUESTION 1 = = (a) Apart from x = 0 the equation f(x) 22 – 4 sin r 0 has another root in (1, 2.5). Perform three iterations of the bisection method to approximate the root. State the accuracy of the root after the three iterations. (b) Perform three iterations of Newton's method for the function in (a) above, using x(0) = 1.5 as the initial solution. Compare the error from the Newton's approximation with that incurred for the same...

  • DO THIS IN MATLAB PLEASE DO THIS IN MATLAB Create a script file that performs the...

    DO THIS IN MATLAB PLEASE DO THIS IN MATLAB Create a script file that performs the following calculations. Your script will use the functions you create. Label all graphs appropriately. For this project, do not have your homemade functions fprintf anything, instead have all your fprintf commands within your script. Attach your published script file along with .m files for your functions. Exercise 1. A fundamental iterative method for finding the roots of equations of one-variable is known as Newton's...

  • QUESTION 1 (a) Apart from = 0 the equation f(t) = 12 - 4sin r =...

    QUESTION 1 (a) Apart from = 0 the equation f(t) = 12 - 4sin r = 0 has another root in (1, 2.5). Perform three (10) iterations of the bisection method to approximate the root. State the accuracy of the root after the three iterations. (b) Perform three iterations of Newton's method for the function in (a) above, using x(0) = 1.5 as the initial (10) solution. Compare the error from the Newton's approximation with that incurred for the same...

  • this is numerical analysis QUESTION 1 (a) Apart from 1 = 0 the equation f(1) =...

    this is numerical analysis QUESTION 1 (a) Apart from 1 = 0 the equation f(1) = x2 - 4 sin r = 0 has another root in (1, 2.5). Perform three (10) iterations of the bisection method to approximate the root. State the accuracy of the root after the three iterations. (b) Perform three iterations of Newton's method for the function in (a) above, using x) = 1.5 as the initial (10) solution. Compare the error from the Newton's approximation...

  • Not in C++, only C code please In class, we have studied the bisection method for...

    Not in C++, only C code please In class, we have studied the bisection method for finding a root of an equation. Another method for finding a root, Newton's method, usually converges to a solution even faster than the bisection method, if it converges at all. Newton's method starts with an initial guess for a root, xo, and then generates successive approximate roots X1, X2, .... Xj, Xj+1, .... using the iterative formula: f(x;) X;+1 = x; - f'(x;) Where...

  • Consider the following function with a real variable, x: ?(?) = ?3 - 3?2 + 6?...

    Consider the following function with a real variable, x: ?(?) = ?3 - 3?2 + 6? + 10 a. Write a Python function for the derivative of f(x) that takes x and returns the derivative of f(x). Take the derivative of f(x) analytically with respect to x before writing the function. b. Write a Python code that approximately finds the real root, x0, of f(x) such that f(x0)~0 using the Newton-Raphson method. The code is expected to get an initial...

  • in matlab -Consider the equation f(x) = x-2-sin x = 0 on the interval x E [0.1,4 π] Use a plot to approximately locate the roots of f. To which roots do the fol- owing initial guesses converge wh...

    in matlab -Consider the equation f(x) = x-2-sin x = 0 on the interval x E [0.1,4 π] Use a plot to approximately locate the roots of f. To which roots do the fol- owing initial guesses converge when using Function 4.3.1? Is the root obtained the one that is closest to that guess? )xo = 1.5, (b) x0 = 2, (c) x.-3.2, (d) xo = 4, (e) xo = 5, (f) xo = 27. Function 4.3.1 (newton) Newton's method...

  • II. Using Newton’s method, write a MATLAB program to find the fixed point of the following...

    II. Using Newton’s method, write a MATLAB program to find the fixed point of the following function: ?(?) = √? + ?? accurate to at least 8 decimal places. (HINT: finding the fixed point of f(x) is the same as finding the zero of g(x) = f(x) − x. ) The output of this program should display in a single table (i) the solution for the fixed point, (ii) the initial guess, (iii) the number of iterations it took to...

  • Please write in Language c using only the files stdio.h and math.h Suppose you wish to...

    Please write in Language c using only the files stdio.h and math.h Suppose you wish to find the root r of a function f(x), that is, the value r where f(r)=0. One method is to make an initial guess, x0, compute the line tangent to f at x0, and find where the tangent line intercepts the x-axis, x1. Then, use x1 as the second guess and repeat this procedure n times until f(xn) approximately equals 0 and report xn as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT