Video summary
Introduction to CASADI for Beginners [lecture 2]
Main summary
Key takeaways
Main ideas & lessons (lecture overview)
- The lesson continues an “induction/basics” introduction to CasADi (symbolic modeling/optimization for numerical computation), focusing on:
- Defining CasADi functions with inputs/outputs (scalar, vector, default/omitted arguments).
- Building and evaluating functions by supplying different input shapes.
- Using CasADi to solve geometric nonlinear optimization problems (starting with nonlinear geometry constraints and later framed in a quadratic-programming-like structure).
- Plotting/visualizing solutions (contours, circles, constraint geometry).
- Using CasADi interfaces (higher-level “solver” style expressions).
- Solving ODEs (time-stepping / integrator approach concept).
- Solving systems of nonlinear equations with a root finder.
- Generating C code from CasADi expressions/functions.
Methodology / procedure described
1) Defining a CasADi function with symbolic inputs and outputs
-
Create a function using symbolic variables (conceptual example):
- Inputs:
x,y - Define an output expression such as:
f(x, y) ≈ sin(y) * x(as described in the subtitles)
- Inputs:
-
Construct named function evaluations:
- Define
f(…)so passing values forxandyproduces an output.
- Define
-
Mentioned behavior:
- Even if you define
f(…)and call it with placeholders/repeated symbols (subtitles contain unclear naming), the function still correctly maps symbolic inputs to numeric outputs.
- Even if you define
2) Installing/importing needed CasADi components (Python workflow)
- If the required library/components are not installed:
- Install the needed package (the subtitles likely misrecognize the details).
- Then run the cells to import and inspect the function.
3) Evaluating a function with different input cases (shape handling)
Evaluate the same function using different argument patterns:
-
No explicit argument provided
- Defaults effectively treat unspecified values as zero in the example.
-
A single scalar input
- CasADi may broadcast/scale depending on how the function was set up.
-
A vector input
x = [x1, x2, x3, x4]whileyis absent/zero. -
A vector
xplus scalar/vectorysimultaneously.
Core takeaway:
- The output changes consistently based on the supplied numeric shapes.
- Vectorized inputs produce elementwise results (the example described output behavior like
2*x + y).
4) Formulating and solving a discriminant-style geometry problem
- Define a polynomial/implicit equation involving powers of
xandy, with an equality constraint.- Conceptually described as:
- cubic in
x - quadratic in
y
- cubic in
- Conceptually described as:
- Steps:
- Define symbolic variables for
xandy. - Create the implicit function
f(x, y)representing the constraint. - Use a solver/symbolic elimination concept (subtitles mention a “S formula,” with the context suggesting discriminant-like computation and numerical solution extraction).
- Substitute parameter values and retrieve numerical solutions.
- Optionally extract only specific components (e.g., “if I only want AB…”).
- Define symbolic variables for
5) Visualization: surface/contour plots and overlaying geometry
-
Steps to plot:
- Build a grid for
xandy. - Compute
z(x, y)over the grid. - Use contour plotting at specified levels.
- Overlay shapes (example: a circle with a chosen center and radius).
- Add aesthetics:
- colorbar
- labels
- saving the image
- Build a grid for
-
Purpose:
- Interpret the optimizer’s result as aligning with the circle/contour intersection.
6) Packing multiple equal circles in a square: maximize radius R
-
Problem statement:
- Place five circles inside a 10×10 square.
- Maximize the common radius
R. - Circles must not intersect (tangency allowed).
-
Modeling/constraints:
- Circle centers as decision variables:
xi, yifor each circlei
- Boundary constraints (each circle stays inside the square):
- Left wall:
xi - R ≥ 0 - Right wall:
10 - (xi + R) ≥ 0 - Bottom wall:
yi - R ≥ 0 - Top wall:
10 - (yi + R) ≥ 0
- Left wall:
- Non-intersection constraints between circle pairs:
- Distance between centers ≥
Ri + Rj - With equal radii:
distance ≥ 2R
- Distance between centers ≥
- Circle centers as decision variables:
-
Optimization setup:
- Decision variables include:
- all
xi, yi(for five circles) andR
- all
- Objective:
- maximize
R(implemented as minimizing-R, per the subtitles)
- maximize
- Decision variables include:
-
Solve and extract:
- Call a nonlinear solver (subtitles mention an
nlpsol-like optimization solver). - Retrieve
Rand the circle centers.
- Call a nonlinear solver (subtitles mention an
-
Plotting:
- Reshape center vector into 5 pairs.
- Draw each circle and overlay them on the square.
- Show that tightening constraint tolerance/iterations makes circles become tangent.
7) “Quadratic programming” style formulation (structured modeling)
- The lecture claims the problem can be expressed in a solver-consistent way resembling quadratic programming.
- High-level idea:
- Use variables such as
xandy(and radii), write constraints explicitly as algebraic expressions/inequalities, and solve with a CasADi optimization formulation.
- Use variables such as
- Emphasis:
- Symbolic, high-level expressions can be converted into solver-ready structures.
8) Level/interface (high-level CasADi interface) example
-
Introduce a higher-level interface using matrix-like expressions:
- Objective shown conceptually as a quadratic form:
x^T * H * x + ...
- Constraints packaged as:
- equality:
g(x) = 0 - inequalities via bounds
- equality:
- Objective shown conceptually as a quadratic form:
-
Steps:
- Convert expressions into matrix form (conceptually).
- Provide variable lower/upper bounds.
- Provide constraint structure and corresponding bounds.
- Call the solver.
- Interpret outputs (e.g., extract
x_outand objective/feasibility results).
9) Solving differential equations (ODEs) via time discretization
-
Concept:
- Choose a time interval (example:
0to10) and a step sizeDT. - For each time step:
- integrate/update the state from the current condition
- accumulate/reshape the solution over time
- Choose a time interval (example:
-
Verification:
- Compare the computed solution against a reference/“actual” solution.
- Plot and verify agreement.
10) Root finding for a nonlinear system
-
Define two nonlinear equations (example structure):
g1(x, y)described like:tan(x) + (2*y^2)/25g2(x, y)described like:sin(x) - (1/2)*y
-
Steps:
- Stack equations into a vector:
g(x, y) = [g1, g2]
- Use CasADi’s root finder:
- provide symbolic function
g - provide an initial guess for
[x, y]
- provide symbolic function
- Solve for
(x, y)such that both equations are zero.
- Stack equations into a vector:
-
Output:
- solution values for the unknowns satisfying the system.
11) Generating C code from CasADi functions
- Steps:
- Define a CasADi function
f(x, y)symbolically (subtitles describe a trig-based expression). - Use CasADi code generation to produce C source files.
- Compile/use the generated code (e.g., check the output folder and inspect generated files).
- If mistakes occur, correct and regenerate.
- Define a CasADi function
Speakers / sources featured
- Primary speaker: The lecturer (referred to as “Welcome everyone” / teaching throughout). No name is provided in the subtitles.
- Source/Channel referenced: The lecture series/channel is referenced generally (no specific channel name is stated in the subtitles).