Question

In: Computer Science

I'm writing a code for the Secant Method for root finding, but my code makes an...

I'm writing a code for the Secant Method for root finding, but my code makes an infinite code of the correct answer. Below is my code, how do I fix it?

def f(x):
return (x**6)+7*(x**5)-15*(x**4)-70*(x**3)+75*(x**2)+175*x-125

def secant():
p0=float(input('Enter an initial guess for the root '))
p1=float(input('Enter another guess '))
TOL=float(input('Enter a tolerance in decimal form '))
n=15
i=1
while i<=n:
p=p1-f(p1)*(p1-p0)/(f(p1)-f(p0))
if abs(p-p1)<TOL:
print(p)
else:
p0=p1
p1=p
i=i+1
else:
print(p)
return p
  
print(secant())

Solutions

Expert Solution

So the program is mainly not running since you are not getting out of the loop when the tolerance condition is met .

Also the i=i+1 statement should be outside the if-else statements since everytime the tolerance condition is met the program is not going to the else statements and thus your i was not increasing , therefore you were getting a infinite loop.

also the second else statement was a syntax error

all these have been fixed below.

def f(x):
return (x**6)+7*(x**5)-15*(x**4)-70*(x**3)+75*(x**2)+175*x-125

def secant():
p0=float(input('Enter an initial guess for the root '))
p1=float(input('Enter another guess '))
TOL=float(input('Enter a tolerance in decimal form '))
n=15
i=1
while i<=n:
    p=p1-(f(p1)*(p1-p0))/(f(p1)-f(p0))
    i=i+1
    if abs(p-p1)<TOL:
       print(p)
       break #added a break method here so that the loop breaks when tolerance is met
    else:
       p0=p1
       p1=p
       print(p) #no need for another else
return p

print(secant())


Related Solutions

Use the secant Method to find a root for the function: f(x) = x^3 + 2x^2...
Use the secant Method to find a root for the function: f(x) = x^3 + 2x^2 + 10x -20, with x_0 = 2, and x_1 = 1.
The Ostrowski method for finding a single root of ?(?)=0 is given by Initial guess ?0...
The Ostrowski method for finding a single root of ?(?)=0 is given by Initial guess ?0 ??=??−?(??)?′(??), ??+1=??−?(??)?′(??) ?(??)?(??)−2?(??). a) Write MATLAB or OCTAVE coding to implement the Ostrowski method. (Hint: You may use the coding of Newton Method given in Moodle pages) b) Use your coding to find a root of the equation (?−2)2−ln(?)=0 With initial guess ?0=1.0 and ?0 = 3.0. Write or print the results in your Homework sheet.
In class, we have studied the bisection method for finding a root of an equation. Another...
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, ?0 , and then generates successive approximate roots ?1 , ?2 , …. ?i , ?i+i, …. using the iterative formula?′(?௝) Where ?’(xi) is the derivative of function ? evaluated at...
Write MIPS code for finding the (estimated) square-root of a number N, by using the following...
Write MIPS code for finding the (estimated) square-root of a number N, by using the following pseudo-code: sqrt = N; repeat 10 times { sqrt = (sqrt + N/sqrt)/2; } The number N is entered by the user and the answer is displayed on the screen as (for example): The square root of 121 is 11. Write MIPS code for finding the distance between two points [x1,y1] and [x2,y2]. The formula for the distance is: z = ?(x2 − x1)2...
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
Consider the Newton-Raphson method for finding root of a nonlinear function ??+1=??−?(??)?′(??), ?≥0. a) Prove that...
Consider the Newton-Raphson method for finding root of a nonlinear function ??+1=??−?(??)?′(??), ?≥0. a) Prove that if ? is simple zero of ?(?), then the N-R iteration has quadratic convergence. b) Prove that if ? is zero of multiplicity ? , then the N-R iteration has only linear convergence.
Name and sketch two ways in which an open method for root finding can fail to...
Name and sketch two ways in which an open method for root finding can fail to locate a root even when a root exists.
How could a root finding algorithm like the bisection method be used to approximate a value...
How could a root finding algorithm like the bisection method be used to approximate a value such as sqrt(3). In other words how can a root finding algorithm find an x value with a given y value? Write a script to illustrate this usage scenario. Compare the output of your script with the result from a calculator. You must use matlab!! using a while loop.
I'm writing my entrepreneurship project, and it requires me to do a feasibility study, I need...
I'm writing my entrepreneurship project, and it requires me to do a feasibility study, I need help with "organization and staffing" in the feasibility study only. The business idea is basically a coffee shop that allows people to draw freely, provide art classes, a monthly art event and serves pastry, cake, and coffee normally in the store. help me please :(
Invalid entry code in python my code is pasted below. The last elif statement, I'm trying...
Invalid entry code in python my code is pasted below. The last elif statement, I'm trying to get the program to print "invalid entry" if the entry for user_input is invalid. The first user prompt should only allow for numbers 1-10 and "exit" and "quit" import math user_prompt = """Enter number of operation that you want to execute <type exit or quit to end program>: 1 sin(x) 2 cos(x) 3 tan(x) 4 asin(x) 5 acos(x) 6 atan(x) 7 ln(x) 8...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT