From 26db17c307c7e556dd3793ed04a63f47267c8b23 Mon Sep 17 00:00:00 2001 From: Peter Stahlecker Date: Tue, 14 Apr 2026 15:53:23 +0200 Subject: [PATCH 1/3] add variable bounds on the torque --- .../plot_pendulum_swing_up_fixed_duration.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/examples-gallery/beginner/plot_pendulum_swing_up_fixed_duration.py b/examples-gallery/beginner/plot_pendulum_swing_up_fixed_duration.py index 40bdc89d..73f84fd5 100644 --- a/examples-gallery/beginner/plot_pendulum_swing_up_fixed_duration.py +++ b/examples-gallery/beginner/plot_pendulum_swing_up_fixed_duration.py @@ -7,6 +7,7 @@ --------- - Show the use of opty with likely the simplest example possible. +- Show how to use adjustable bounds. Introduction @@ -84,8 +85,16 @@ ) # %% -# Limit the torque to a maximum magnitude. -bounds = {T(t): (-2.0, 2.0)} +# Limit the torque using variable bounds. +third = int(num_nodes/3) +low_T = np.array([-3 for _ in range(third)] + + [-1 for _ in range(third)] + + [-3 for _ in range(num_nodes - 2 * third)]) +hi_T = np.array([3 for _ in range(third)] + + [1 for _ in range(third)] + + [3 for _ in range(num_nodes - 2 * third)]) + +bounds = {T(t): (low_T, hi_T)} # %% # Create an optimization problem. @@ -111,7 +120,7 @@ # %% # Plot the optimal state and input trajectories. -_ = prob.plot_trajectories(solution) +_ = prob.plot_trajectories(solution, show_bounds=True) # %% # Plot the constraint violations. From 5bd53be52bc60802fac4fdb2f3bf585ff65e5982 Mon Sep 17 00:00:00 2001 From: Peter Stahlecker Date: Tue, 14 Apr 2026 22:13:44 +0200 Subject: [PATCH 2/3] change adjustable bounds to variable bounds --- .../beginner/plot_pendulum_swing_up_fixed_duration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples-gallery/beginner/plot_pendulum_swing_up_fixed_duration.py b/examples-gallery/beginner/plot_pendulum_swing_up_fixed_duration.py index 73f84fd5..62f0a502 100644 --- a/examples-gallery/beginner/plot_pendulum_swing_up_fixed_duration.py +++ b/examples-gallery/beginner/plot_pendulum_swing_up_fixed_duration.py @@ -7,7 +7,7 @@ --------- - Show the use of opty with likely the simplest example possible. -- Show how to use adjustable bounds. +- Show how to use variable bounds. Introduction From 11e1824809d746b99dc2fcf45396ee27edc4f47c Mon Sep 17 00:00:00 2001 From: Peter Stahlecker Date: Tue, 14 Apr 2026 22:27:43 +0200 Subject: [PATCH 3/3] improved code as per suggestion --- .../beginner/plot_pendulum_swing_up_fixed_duration.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/examples-gallery/beginner/plot_pendulum_swing_up_fixed_duration.py b/examples-gallery/beginner/plot_pendulum_swing_up_fixed_duration.py index 62f0a502..b441e054 100644 --- a/examples-gallery/beginner/plot_pendulum_swing_up_fixed_duration.py +++ b/examples-gallery/beginner/plot_pendulum_swing_up_fixed_duration.py @@ -87,12 +87,10 @@ # %% # Limit the torque using variable bounds. third = int(num_nodes/3) -low_T = np.array([-3 for _ in range(third)] + - [-1 for _ in range(third)] + - [-3 for _ in range(num_nodes - 2 * third)]) -hi_T = np.array([3 for _ in range(third)] + - [1 for _ in range(third)] + - [3 for _ in range(num_nodes - 2 * third)]) +low_T = np.full(num_nodes, -3.0) +low_T[third: 2*third] = -1.0 +hi_T = np.full(num_nodes, 3.0) +hi_T[third: 2*third] = 1.0 bounds = {T(t): (low_T, hi_T)}