# I'm stuck with this proplem. Variable used before it's defined.

2 views (last 30 days)
Akbar on 21 Jun 2018
Commented: Walter Roberson on 22 Jun 2018
I have a differential equation(ODE) represented as a directed graph. The ODE is not known, only its graph representation is given. ODE function is to be derived from the directed graph object. Nodes of the graph are operations like: sum, product, constant, gain... And the edges represent the variables. There is only one starting node (with one output only) whose output corresponds to "x" (or state variable of ODE). And there is one end node (with one input only) whose input corresponds to "dx" (or the first derivative of "x"). So I am trying to get equation by traversing the graph starting from the leaves (i mean terminal nodes) which also include start node. Lets say in the first iteration i scan 10 leave nodes and write the variables in m-file. Ex.: if a leave is just a constant (say 5.4), then i create a variable const1 = 5.4; After scanning some of the leaves i delete them, then i end up having the next set of leave nodes. I continue until I have no nodes left.
Shortly, the m-file i am creating is a differential equation function file. The problem is that at some points during the iteration the m-file has error like this:
function dx = myFunction(~,x,temperature,pressure)
const1 = temperature;
const2 = pressure;
const3 = 0.03;
...
dx = ...
Here you see that const3 is defined after it has been called. I know that const3 has to be defined first, but im not sure how to do that. I get a list of terminal nodes, scan them and write the equations. Then delete scanned nodes and proceed to next set of leave nodes... Is there a way to somehow suspend the execution of line containing "addnode" untile "const3" is defined?
Walter Roberson on 22 Jun 2018

Steven Lord on 21 Jun 2018
What you're describing sounds a lot like a Simulink model. Perhaps you should represent your differential equation graph that way? See this documentation page for a simple example.

### Categories

Find more on General Applications in Help Center and File Exchange

R2017b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!