I'm stuck with this proplem. Variable used before it's defined.
이전 댓글 표시
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;
addnode = (const1 + const2)*const3;
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?
댓글 수: 6
Walter Roberson
2018년 6월 21일
"The problem is that at some points it write"
What is it that writes that?
OCDER
2018년 6월 21일
You might need to take one step back... Instead of asking how to suspend execution of code until const3 is defined, show us this directed graph, and how you are generating the .m file from this graph.
You have something like this??
diagram =
[*] ---->[+]----> const1
| | ----> const2
|
|----> const3
generateMCode(diagram) %do you have a function to create functions?
%generated ode function
function a = myFunction(varargin)
const1 = ...;
const2 = ...;
const3 = ...;
a = const3 * (const1 + const2);
Image Analyst
2018년 6월 21일
Can you put answers down in the Answers section (not up here in the comments section)?
OCDER
2018년 6월 21일
There is no answer yet. We're waiting to see what the automatic code generation function is that is creating the wrong myFunction code with the out-of-order variable declarations.
Walter Roberson
2018년 6월 22일
Please do not close questions that have an answer.
답변 (1개)
Steven Lord
2018년 6월 21일
0 개 추천
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.
카테고리
도움말 센터 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!