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.
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.
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...
I'm writing about environmental health. And these are my thesis, (1)physical factors that affect health, (2)...
I'm writing about environmental health. And these are my thesis, (1)physical factors that affect health, (2) biological factors that affect health, (3) chemical factors that affect halth. You should please help me to attach citations. It should be about two pages
XML and XSL I'm trying to style my XML code but it's not implementing the XSL...
XML and XSL I'm trying to style my XML code but it's not implementing the XSL file when I run it even though the file is referenced. Any help? XML code: <?xml version="1.0"?> <?xml-stylesheet href="textbooks.xsl" type="text/xsl" ?> <textbooks> <textbook> <title> Introduction to Design and Analysis of Algorithms</title> <authors> <author> <firstName>Anany</firstName> <lastName>Levitin</lastName> </author> </authors> <publisher> <name>Ed, Pearson</name> <website>https://www.pearson.com</website> </publisher> <Year-of-Publication>2011</Year-of-Publication> <ISBN>978-0132316811</ISBN> <book-specific-website></book-specific-website> <edition>3rd edition</edition> <cover-type>Paperback</cover-type> </textbook> <textbook> <title>Software Engineering: A Practitioner’s Approach</title> <authors> <author> <firstName>Roger</firstName> <lastName>Pressman</lastName> </author> </authors> <publisher> <name>Ed, McGraw-Hill</name>...
In MATLAB write a function secant.m to apply the secant method. function [x,i] = secant(f, x0,...
In MATLAB write a function secant.m to apply the secant method. function [x,i] = secant(f, x0, x1, tol, maxiters) [x,i] = secant(f, x0, x1, tol, maxiters) performs the secant method with f(x), starting at x_0 = x0 and x_1 = x1, and continuing until either |x_i+1 - x_i| <= tol, or maxiters iterations have been taken. The number of iterations, i, is also returned. An error is raised if the first input is not a function handle. A warning is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT