Answered step by step
Verified Expert Solution
Link Copied!

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 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()
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2008 Comprehensive Concepts And Techniques

Authors: Gary B. Shelly, Corinne Hoisington

1st Edition

1423927168, 978-1423927167

More Books

Students also viewed these Databases questions

Question

Explain the nature of human resource management.

Answered: 1 week ago

Question

Write a note on Quality circles.

Answered: 1 week ago

Question

Describe how to measure the quality of work life.

Answered: 1 week ago