Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Solving the Optimisation Problem | Mathematical Analysis
Mathematics for Data Analysis and Modeling

book
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.

  1. Calculate the derivatives of the first and second order for the h function.
  2. Find critical points of h function.
  3. Check if these critical points are points of the maximum of the function h.

Løsning

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?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 5
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

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt