Question: 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 Figure(Q1) below to hold water
for a small village in a country. The volume of liquid it can hold can be computed
as
V=h2[3R-h]3
where V volume (m3),h depth of water in tank (m), and R the tank radius (m).
If R=3m, to what depth must the tank be filled so that it holds 30m3? 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 0 and R
Program should show:
initial guess value input
second guess value input
indication of error if the interval inputs are not correct input values.
Tolerance/error input
All intermediate values of h and its final value
import math
# Function to calculate the volume of water in the tank
def volume(h, R):
return math.pi * h *2*(3* R - h)/3
# Bisection method
def bisection_method(target_volume, R, tol=1e-6, max_iter=100):
a =0
b = R
iter_count =0
if volume(a, R)> target_volume or volume(b, R) target_volume:
print("Error: Initial interval does not contain the solution.")
return None
while iter_count max_iter:
c =(a + b)/2
vc = volume(c, R)
if abs(vc - target_volume) tol:
print("Bisection method converged after", iter_count, "iterations.")
return c
if vc > target_volume:
b = c
else:
a = c
iter_count +=1
print("Bisection method did not converge within the maximum number of iterations.")
return None
# Newton's method
def newton_method(target_volume, R, tol=1e-6, max_iter=100):
h = R /2 # Initial guess
iter_count =0
while iter_count max_iter:
f = volume(h, R)- target_volume
if abs(f) tol:
print("Newton's method converged after", iter_count, "iterations.")
return h
df =2* math.pi *(R - h)* h + math.pi*(3*R -2*h)
# Derivative of the volume function
h -= f / df
iter_count +=1
print("Newton's method did not converge within the maximum number of iterations.")
return None
# Secant method
def secant_method(target_volume, R, tol=1e-6, max_iter=100):
h0=0
h1= R
iter_count =0
while iter_count max_iter:
f0= volume(h0, R)- target_volume
f1= volume(h1, R)- target_volume
h_new = h1- f1*(h1- h0)/(f1- f0)
if abs(h_new - h1) tol:
print("Secant method converged after", iter_count, "iterations.")
return h_new
h0, h1= h1, h_new
iter_count +=1
print("Secant method did not converge within the maximum number of iterations.")
return None
# Main function
def main():
R =3 # Radius of the tank
target_volume =30 # Target volume of water
print("Initial guess values: 0 and", R)
# Bisection method
bisection_result = bisection_method(target_volume, R)
print("Bisection method result:", bisection_result, "m")
# Newton's method
newton_result = newton_method(target_volume, R)
print("Newton's method result:", newton_result, "m")
# Secant method
secant_result = secant_method(target_volume, R)
print("Secant method result:", secant_result, "m")
if __name__=="__main__":
main()
 i need a python code that solves this question in general.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!