Challenge: Solving the Optimisation Problem
Opgave
Swipe to start coding
Let's consider a physics-related optimization problem where we need to find the maximum height reached by an object thrown vertically upward with a given initial velocity.
We have the following equation:
h = v * t - 0.5 * g * t**2
that describes the motion of an object.
Our task is to find the time t
when the object reaches its maximum height and then find the maximum height h_max
.
- Calculate the derivatives of the first and second order for the
h
function. - Find critical points of
h
function. - Check if these critical points are points of the maximum of the function
h
.
Løsning
99
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
import sympy as sp
# Define the symbols
t = sp.symbols('t')
# Define the height function h(t)
v = 15.0 # Initial velocity in m/s
g = 9.81 # Acceleration due to gravity in m/s^2
h = v * t - (1/2) * g * t**2
# Calculate the first and second derivatives of h(t)
h_prime = sp.diff(h, t)
h_double_prime = sp.diff(h_prime, t)
# Find the critical points by solving h'(t) = 0
critical_points = sp.solve(h_prime, t)
# Identify the maximum height by evaluating h''(t) at each critical point
maximum_height = -1
for t_critical in critical_points:
h_double_prime_value = h_double_prime.subs({t:t_critical})
if h_double_prime_value < 0:
maximum_height = h.subs({t:t_critical})
# Print the result
if maximum_height != -1:
print(f'The object reaches its maximum height of {maximum_height:.2f} meters at time t = {t_critical:.2f} seconds.')
else:
print('There is no maximum height (object doesn\'t reach the ground).')
Var alt klart?
Tak for dine kommentarer!
Sektion 3. Kapitel 5
99
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
import sympy as sp
# Define the symbols
t = sp.symbols('t')
# Define the height function h(t)
v = 15.0 # Initial velocity in m/s
g = 9.81 # Acceleration due to gravity in m/s^2
h = v * t - (1/2) * g * t**2
# Calculate the first and second derivatives of h(t)
h_prime = sp.___(h, t)
h_double_prime = sp.diff(___, t)
# Find the critical points by solving h'(t) = 0
critical_points = sp.___(h_prime, t)
# Identify the maximum height by evaluating h''(t) at each critical point
maximum_height = -1
for t_critical in critical_points:
h_double_prime_value = h_double_prime.___({t:t_critical})
if h_double_prime_value < 0:
maximum_height = h.subs({t:t_critical})
# Print the result
if maximum_height != -1:
print(f'The object reaches its maximum height of {maximum_height:.2f} meters at time t = {t_critical:.2f} seconds.')
else:
print('There is no maximum height (object doesn\'t reach the ground).')
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat