Questions about the values of @variables in Catalyst/ModelingToolkit


I have a few seemingly simple questions but just can’t figure out the answer myself. I am using Catalyst to define my model. My first question is how do I find the default values I defined for the variables? for example if I have:
@variables a=1 b=10
How do I find out the value of a and b in julia?
The second question is if I have an expression
f = 10*a+b
can I evaluate the expression f?
I just can’t figure out how to do them.

Thanks in advance for you help!


@variables a=1 b=10 instantiates symbolic variables with default values or initial guesses to be 1 and 10. It doesn’t mean that a is assigned to be 1 and b is assigned to be 10

Thanks. Is there a way to check the default values? And is there a way to assign these value in an expression containing a and b?

Given a ReactionSystem you can call ModelingToolkit.defaults(sys) to get the current defaults. You can use Catalyst.setdefaults!, to add defaults to a system.

That will work. But my question is just to evaluate an expression with these predefined @variables. I don’t understand how the variable definition is done on a lower level, and I didn’t find a function to evaluate a user-defined expression. The question has bothered me for a long time.

@isaacsas @YingboMa

Those values are, as far as I am aware, solely used when creating an ODEProblem or such to specify the initial condition vector, u0, and the parameter value vector, p, – they aren’t used in the symbolic expressions at all.

If you want to substitute concrete values into a symbolic expression you can use Symbolics.substitute like

using ModelingToolkit, Symbolics
@parameters k
@variables t A(t)
ex = A^2 * k
val = substitute(ex, Dict(A => 2.0, k => 3.0))
1 Like

Thanks @isaacsas, this is helpful!

Glad it helped!