-
-
Notifications
You must be signed in to change notification settings - Fork 228
Add comprehensive documentation for array variables (fixes #956) #3839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This commit adds clear documentation for creating and using array (vector) variables in ModelingToolkit, addressing the long-standing issue #956. Changes: - Added new tutorial "Working with Array Variables" covering: - Basic array variable syntax `@variables x[1:n](t)` - Array parameters - Building systems with array variables (mass-spring example) - Initial conditions and parameter values for arrays - Multi-dimensional arrays - Common patterns and tips (comprehensions, defaults, variable-sized) - Troubleshooting section addressing the specific error from issue #956 - Added the new tutorial to the documentation navigation in pages.jl - Updated ode_modeling.md to: - Add a brief section introducing array variables - Update outdated text saying vector variables are "work in progress" - Link to the comprehensive array variables tutorial The documentation now clearly explains the syntax that was previously only documented in Symbolics.jl docstrings, and provides practical examples to help users avoid common pitfalls like passing vector initial conditions to scalar variables. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
# Define the system | ||
n = 3 # number of masses | ||
|
||
@variables x[1:n](t) v[1:n](t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this gives warnings about deprecated syntax
push!(eqs, m[1] * D(v[1]) ~ -k[1] * x[1] + k[2] * (x[2] - x[1]) - c[1] * v[1]) | ||
|
||
# Middle masses | ||
for i in 2:(n-1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶
for i in 2:(n-1) | |
for i in 2:(n - 1) |
# Middle masses | ||
for i in 2:(n-1) | ||
push!(eqs, D(x[i]) ~ v[i]) | ||
push!(eqs, m[i] * D(v[i]) ~ k[i] * (x[i-1] - x[i]) + k[i+1] * (x[i+1] - x[i]) - c[i] * v[i]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶
push!(eqs, m[i] * D(v[i]) ~ k[i] * (x[i-1] - x[i]) + k[i+1] * (x[i+1] - x[i]) - c[i] * v[i]) | |
push!(eqs, | |
m[i] * D(v[i]) ~ | |
k[i] * (x[i - 1] - x[i]) + k[i + 1] * (x[i + 1] - x[i]) - c[i] * v[i]) |
|
||
# Last mass | ||
push!(eqs, D(x[n]) ~ v[n]) | ||
push!(eqs, m[n] * D(v[n]) ~ k[n] * (x[n-1] - x[n]) - c[n] * v[n]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶
push!(eqs, m[n] * D(v[n]) ~ k[n] * (x[n-1] - x[n]) - c[n] * v[n]) | |
push!(eqs, m[n] * D(v[n]) ~ k[n] * (x[n - 1] - x[n]) - c[n] * v[n]) |
|
||
# Plot the solution | ||
using Plots | ||
plot(sol, idxs=[x[1], x[2], x[3]], labels=["x₁" "x₂" "x₃"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶
plot(sol, idxs=[x[1], x[2], x[3]], labels=["x₁" "x₂" "x₃"]) | |
plot(sol, idxs = [x[1], x[2], x[3]], labels = ["x₁" "x₂" "x₃"]) |
1. Use array variables: `@variables x[1:2](t)` | ||
2. Provide scalar initial conditions: `[x => 1.0]` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶
1. Use array variables: `@variables x[1:2](t)` | |
2. Provide scalar initial conditions: `[x => 1.0]` | |
1. Use array variables: `@variables x[1:2](t)` | |
2. Provide scalar initial conditions: `[x => 1.0]` |
- Static arrays (`@SVector`, `@SMatrix`) for small, fixed-size arrays | ||
- Sparse arrays for systems with sparse connectivity | ||
- Symbolic simplification with `mtkcompile` to eliminate redundant calculations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶
- Static arrays (`@SVector`, `@SMatrix`) for small, fixed-size arrays | |
- Sparse arrays for systems with sparse connectivity | |
- Symbolic simplification with `mtkcompile` to eliminate redundant calculations | |
- Static arrays (`@SVector`, `@SMatrix`) for small, fixed-size arrays | |
- Sparse arrays for systems with sparse connectivity | |
- Symbolic simplification with `mtkcompile` to eliminate redundant calculations |
- Use `@variables x[1:n](t)` syntax to create array variables | ||
- Access elements with standard Julia indexing: `x[i]` | ||
- Provide initial conditions and parameters for each array element | ||
- Use array comprehensions for concise equation definition | ||
- Multi-dimensional arrays are supported | ||
- For variable-sized arrays, use `@mtkmodel` with structural parameters |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶
- Use `@variables x[1:n](t)` syntax to create array variables | |
- Access elements with standard Julia indexing: `x[i]` | |
- Provide initial conditions and parameters for each array element | |
- Use array comprehensions for concise equation definition | |
- Multi-dimensional arrays are supported | |
- For variable-sized arrays, use `@mtkmodel` with structural parameters | |
- Use `@variables x[1:n](t)` syntax to create array variables | |
- Access elements with standard Julia indexing: `x[i]` | |
- Provide initial conditions and parameters for each array element | |
- Use array comprehensions for concise equation definition | |
- Multi-dimensional arrays are supported | |
- For variable-sized arrays, use `@mtkmodel` with structural parameters |
- Vector-valued parameters and variables are possible. A cleaner, more | ||
consistent treatment of these is still a work in progress, however. Once finished, | ||
this introductory tutorial will also cover this feature. | ||
- Vector-valued parameters and variables are supported using array syntax like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶
- Vector-valued parameters and variables are supported using array syntax like | |
- Vector-valued parameters and variables are supported using array syntax like |
Summary
This PR adds comprehensive documentation for array (vector) variables in ModelingToolkit, addressing the long-standing issue #956 from February 2024.
Problem
Users were struggling to discover how to create symbolic vectors/arrays in ModelingToolkit. The syntax
@variables x[1:n](t)
was not documented in MTK's documentation, only in Symbolics.jl docstrings. Additionally, users who tried to pass vector initial conditions to scalar variables received confusing error messages.Solution
Added a comprehensive tutorial "Working with Array Variables" that covers:
📚 Tutorial Contents
Basic Syntax
@variables x[1:3](t)
creatingx[1](t), x[2](t), x[3](t)
@parameters k[1:3]
Practical Examples
Advanced Topics
@mtkmodel
Troubleshooting
📝 Additional Changes
ode_modeling.md
to mention array variablesExample from the Tutorial
Testing
The tutorial includes complete, runnable examples that demonstrate:
Closes #956
🤖 Generated with Claude Code