Answered step by step
Verified Expert Solution
Question
1 Approved Answer
i need a python code that solves this question in general. and solves for bisection, secant and newtons method. because the code that was given
i need a python code that solves this question in general. and solves for bisection, secant and newtons method. because the code that was given by chegg last time was wrong so please edit the code that is below following the criteria of the question
You are designing a spherical tank as shown in FigureQ below to hold water
for a small village in a country. The volume of liquid it can hold can be computed
as
where volume m depth of water in tank m and the tank radius m
If to what depth must the tank be filled so that it holds Use a
computer software program Python to determine your answer. You are
required to compare bisection method, Newton's method, and Secant method
solutions and recommend one of these software programs with justifications.
Employ initial guesses of and
Program should show:
initial guess value input
second guess value input
indication of error if the interval inputs are not correct input values.
Toleranceerror input
All intermediate values of and its final value
import math
# Function to calculate the volume of water in the tank
def volumeh R:
return math.pi h R h
# Bisection method
def bisectionmethodtargetvolume, R tole maxiter:
a
b R
itercount
if volumea R targetvolume or volumeb R targetvolume:
printError: Initial interval does not contain the solution."
return None
while itercount maxiter:
c a b
vc volumec R
if absvc targetvolume tol:
printBisection method converged after", itercount, "iterations."
return c
if vc targetvolume:
b c
else:
a c
itercount
printBisection method did not converge within the maximum number of iterations."
return None
# Newton's method
def newtonmethodtargetvolume, R tole maxiter:
h R # Initial guess
itercount
while itercount maxiter:
f volumeh R targetvolume
if absf tol:
printNewtons method converged after", itercount, "iterations."
return h
df math.pi R h h math.piR h
# Derivative of the volume function
h f df
itercount
printNewtons method did not converge within the maximum number of iterations."
return None
# Secant method
def secantmethodtargetvolume, R tole maxiter:
h
h R
itercount
while itercount maxiter:
f volumeh R targetvolume
f volumeh R targetvolume
hnew h fh hf f
if abshnew h tol:
printSecant method converged after", itercount, "iterations."
return hnew
h h h hnew
itercount
printSecant method did not converge within the maximum number of iterations."
return None
# Main function
def main:
R # Radius of the tank
targetvolume # Target volume of water
printInitial guess values: and", R
# Bisection method
bisectionresult bisectionmethodtargetvolume, R
printBisection method result:", bisectionresult, m
# Newton's method
newtonresult newtonmethodtargetvolume, R
printNewtons method result:", newtonresult, m
# Secant method
secantresult secantmethodtargetvolume, R
printSecant method result:", secantresult, m
if namemain:
main
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started