-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.4_CP6.py
More file actions
31 lines (26 loc) · 938 Bytes
/
Copy path1.4_CP6.py
File metadata and controls
31 lines (26 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# A 10-cm-high cone contains 60cm^3 of ice cream, including a hemispherical scoop on top. Find the radius of the scoop
# to four correct decimal places.
# Cone = (pi * r^2 * h)/3
# Hemisphere = 2/3 * pi * r^3
# f(r) = pi*r^2*10 + 2*pi*r^3 - 180
import numpy as np
# Newtons method
def main(f, Df, x0, epsilon, max_iter):
xn = x0
for n in range(0, max_iter):
fxn = f(xn)
if abs(fxn) < epsilon:
print('Found solution after', n, 'iterations.')
return xn
Dfxn = Df(xn)
if Dfxn == 0:
print('Zero derivative. No solution found.')
return None
xn = xn - fxn / Dfxn
print(xn)
print('Exceeded maximum iterations. No solution found.')
return None
if __name__ == '__main__':
f = lambda r: (np.pi * r ** 2 * 10) + (2 * np.pi * r ** 3) - 180
df = lambda r: (20 * r * np.pi) + (6 * np.pi * r ** 2)
main(f, df, 1, 1e-10, 10)