Title: `solve_flat_ocp` and scalar `timepts` · Issue #1110 · python-control/python-control · GitHub
Open Graph Title: `solve_flat_ocp` and scalar `timepts` · Issue #1110 · python-control/python-control
X Title: `solve_flat_ocp` and scalar `timepts` · Issue #1110 · python-control/python-control
Description: flowing from gh-1101, which complains about the code in question, solve_flat_ocp argument timepts is advertised in the docstring as being allowed to be scalar: timepts : float or 1D array_like The list of points for evaluating cost and c...
Open Graph Description: flowing from gh-1101, which complains about the code in question, solve_flat_ocp argument timepts is advertised in the docstring as being allowed to be scalar: timepts : float or 1D array_like The ...
X Description: flowing from gh-1101, which complains about the code in question, solve_flat_ocp argument timepts is advertised in the docstring as being allowed to be scalar: timepts : float or 1D array_like The ...
Opengraph URL: https://github.com/python-control/python-control/issues/1110
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`solve_flat_ocp` and scalar `timepts`","articleBody":"flowing from gh-1101, which complains about the code in question,\n\n`solve_flat_ocp` argument `timepts` is advertised in the docstring as being allowed to be scalar:\n\n```\n timepts : float or 1D array_like\n The list of points for evaluating cost and constraints, as well as\n the time horizon. If given as a float, indicates the final time for\n the trajectory (corresponding to xf)\n```\n\nbut this doesn't work:\n\n```python\nimport numpy as np\nimport control as ct\nimport control.flatsys as fs\n\nf = fs.LinearFlatSystem(ct.ss(ct.tf([1],[1,1])))\n\ndef terminal_cost(x, u):\n return (x-5).dot(x-5)+u.dot(u)\n\ntraj1 = fs.solve_flat_ocp(f, [0, 1], x0=[23],\n terminal_cost=terminal_cost)\n\ntraj2 = fs.solve_flat_ocp(f, 1, x0=[23],\n terminal_cost=terminal_cost)\n\nteval = np.linspace(0,1,101)\n\nr1 = traj1.response(teval)\nr2 = traj2.response(teval)\n\nassert np.max(abs(r1.x-r2.x)) == 0\nassert np.max(abs(r1.u-r2.u)) == 0\nassert np.max(abs(r1.y-r2.y)) == 0\n```\n\nproduces with master @ ebff1259\n\n```\nTraceback (most recent call last):\n File \"/home/rory/projects/pycontrol/pyflakes/flatbug2.py\", line 13, in \u003cmodule\u003e\n traj2 = fs.solve_flat_ocp(f, 1, x0=[23],\n terminal_cost=terminal_cost)\n File \"/home/rory/src/python-control/control/flatsys/flatsys.py\", line 725, in solve_flat_ocp\n T0 = timepts[0] if len(timepts) \u003e 1 else T0\n ^^\nUnboundLocalError: cannot access local variable 'T0' where it is not associated with a value\n```\n\nI think the fix is as below. Diff also removes `Tf`, which is not used in the function. With this diff the test script runs with error.\n\n```diff\n@@ -721,8 +721,7 @@ def solve_flat_ocp(\n \n # Process final time\n timepts = np.atleast_1d(timepts)\n- Tf = timepts[-1]\n- T0 = timepts[0] if len(timepts) \u003e 1 else T0\n+ T0 = timepts[0] if len(timepts) \u003e 1 else 0\n \n # Process keyword arguments\n if trajectory_constraints is None:\n```\n","author":{"url":"https://github.com/roryyorke","@type":"Person","name":"roryyorke"},"datePublished":"2025-02-01T11:27:49.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/1110/python-control/issues/1110"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e720a045-2294-1fb2-0ee0-0dcc4e3d4b69 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B40E:3BF324:FF7443:16ADF18:697A0667 |
| html-safe-nonce | b9ba479157489d34102673865da72ac5dd0437c17c6c9839c1093333afa597e7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNDBFOjNCRjMyNDpGRjc0NDM6MTZBREYxODo2OTdBMDY2NyIsInZpc2l0b3JfaWQiOiIzODI1NzUyOTY5NzY4OTk0NDA3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | b86058f648feb9d90972c2dff11e692dec1063a8987a544f98fdc411f1ceef70 |
| hovercard-subject-tag | issue:2825156415 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python-control/python-control/1110/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3e1d6bb5ae72722cecf2e459b6b46ecada62a65e2a220cfcac213e715fb1d73f/python-control/python-control/issues/1110 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3e1d6bb5ae72722cecf2e459b6b46ecada62a65e2a220cfcac213e715fb1d73f/python-control/python-control/issues/1110 |
| og:image:alt | flowing from gh-1101, which complains about the code in question, solve_flat_ocp argument timepts is advertised in the docstring as being allowed to be scalar: timepts : float or 1D array_like The ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | roryyorke |
| hostname | github.com |
| expected-hostname | github.com |
| None | 24021c77440f544078f9cbe80d78a2fb16ccff65fca92deda9da3c51d1cdc5fc |
| turbo-cache-control | no-preview |
| go-import | github.com/python-control/python-control git https://github.com/python-control/python-control.git |
| octolytics-dimension-user_id | 2285872 |
| octolytics-dimension-user_login | python-control |
| octolytics-dimension-repository_id | 22791752 |
| octolytics-dimension-repository_nwo | python-control/python-control |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 22791752 |
| octolytics-dimension-repository_network_root_nwo | python-control/python-control |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | bd34d6cb576e14ec8da51f144ef82b621f68256d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width