report_II---I[1] (4)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

QUESTION 21: Develop a script to find out cube root values by using

Scipy.

To find cube root values using SciPy, you can use the optimize module, which provides
several optimization and root-finding functions. Below is a script that demonstrates how to
calculate the cube root of a number using SciPy's newton method.

Here's the Python script:

python
Copy code
import scipy.optimize as opt

def cube_root_function(x, a):


return x**3 - a

def find_cube_root(a):
# Initial guess for the root
initial_guess = a / 3.0
# Find the cube root using the Newton method
root = opt.newton(cube_root_function, initial_guess, args=(a,))
return root

# Example usage
numbers = [8, 27, 64, 125, 1000]
cube_roots = {num: find_cube_root(num) for num in numbers}

for num, root in cube_roots.items():


print(f"The cube root of {num} is approximately {root}")

In this script:

1. The cube_root_function defines the equation x3−a=0x^3 - a = 0x3−a=0, where a is


the number whose cube root we want to find.
2. The find_cube_root function uses the opt.newton method to find the root of the
cube_root_function. An initial guess is provided to start the iteration.
3. The script then calculates the cube roots of a list of numbers and prints the results.

T.Rohan(22311A19A3)

B.Banny(22311A19A4)
1

You might also like