Video summary

Introduction to CASADI for Beginners [lecture 2]

Main summary

Key takeaways

Educational

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)
  • Construct named function evaluations:

    • Define f(…) so passing values for x and y produces an output.
  • 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.

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] while y is absent/zero.

  • A vector x plus scalar/vector y simultaneously.

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 x and y, with an equality constraint.
    • Conceptually described as:
      • cubic in x
      • quadratic in y
  • Steps:
    1. Define symbolic variables for x and y.
    2. Create the implicit function f(x, y) representing the constraint.
    3. Use a solver/symbolic elimination concept (subtitles mention a “S formula,” with the context suggesting discriminant-like computation and numerical solution extraction).
    4. Substitute parameter values and retrieve numerical solutions.
    5. Optionally extract only specific components (e.g., “if I only want AB…”).

5) Visualization: surface/contour plots and overlaying geometry

  • Steps to plot:

    1. Build a grid for x and y.
    2. Compute z(x, y) over the grid.
    3. Use contour plotting at specified levels.
    4. Overlay shapes (example: a circle with a chosen center and radius).
    5. Add aesthetics:
      • colorbar
      • labels
      • saving the image
  • 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, yi for each circle i
    • 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
    • Non-intersection constraints between circle pairs:
      • Distance between centers ≥ Ri + Rj
      • With equal radii: distance ≥ 2R
  • Optimization setup:

    • Decision variables include:
      • all xi, yi (for five circles) and R
    • Objective:
      • maximize R (implemented as minimizing -R, per the subtitles)
  • Solve and extract:

    • Call a nonlinear solver (subtitles mention an nlpsol-like optimization solver).
    • Retrieve R and the circle centers.
  • 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 x and y (and radii), write constraints explicitly as algebraic expressions/inequalities, and solve with a CasADi optimization formulation.
  • 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
  • Steps:

    1. Convert expressions into matrix form (conceptually).
    2. Provide variable lower/upper bounds.
    3. Provide constraint structure and corresponding bounds.
    4. Call the solver.
    5. Interpret outputs (e.g., extract x_out and objective/feasibility results).

9) Solving differential equations (ODEs) via time discretization

  • Concept:

    • Choose a time interval (example: 0 to 10) and a step size DT.
    • For each time step:
      • integrate/update the state from the current condition
      • accumulate/reshape the solution over time
  • 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)/25
    • g2(x, y) described like: sin(x) - (1/2)*y
  • Steps:

    1. Stack equations into a vector:
      • g(x, y) = [g1, g2]
    2. Use CasADi’s root finder:
      • provide symbolic function g
      • provide an initial guess for [x, y]
    3. Solve for (x, y) such that both equations are zero.
  • Output:

    • solution values for the unknowns satisfying the system.

11) Generating C code from CasADi functions

  • Steps:
    1. Define a CasADi function f(x, y) symbolically (subtitles describe a trig-based expression).
    2. Use CasADi code generation to produce C source files.
    3. Compile/use the generated code (e.g., check the output folder and inspect generated files).
    4. If mistakes occur, correct and regenerate.

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

Original video