Extract operations from symbolic expression

조회 수: 13 (최근 30일)
Cedric Kotitschke
Cedric Kotitschke 2023년 1월 28일
편집: Paul 2023년 2월 11일
Hello,
given a symbolic expression, I want to extract the fundamental operations that take place in this symbolic expression. In the end, I want to create a Simulink model that does the same as the symbolic expression but without a simple matlab function block but with basic simulink blocks. For that, I need to know what happens to the inputs and in what order.
For example:
I have the symbolic expression z = cos(x)*y + y^2.
How exactly do I get the necessary information from the sym object? I want to extract that the cosine of x is computed which is then multiplied with y and then this product is added to the square of y.
Are there any built-in function that can help me with this? So far, the only solution I could think of was to parse the string generated by "simscapeEquation(z)"
Thanks so much!
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 1월 28일
If you have
z = cos(x)*y + y^2/cos(x)
then what would the desired output look like? For example are you hoping for a stream of RPN operations such as might be used for an HP29c calculator, with code like
x COS DUP y DUP SWAP2 * SWAP DUP * SWAP2 / +
? This specifies the stream of operations (and is at least partially optimized... I would have to think about whether you could do better by entering y first)
Cedric Kotitschke
Cedric Kotitschke 2023년 1월 28일
I'm not yet sure how the output should look like. I discovered that the Shunting yard algorithm at least brings the operation in the right order so that you can read it from left to right. But yes, a stream of RPN operations would be a good starting point

댓글을 달려면 로그인하십시오.

채택된 답변

Paul
Paul 2023년 1월 28일
Hi Cedric
What's the objection to using a Matlab Function block? If that wasn't a constraint, you could use matlabFunctionBlock
Even if you break down the expression, what would be the next step for implementing all of the individual Simulink blocks to rebuild the expression in Simulink?
  댓글 수: 5
Paul
Paul 2023년 1월 29일
편집: Paul 2023년 2월 11일
Yes, symFunType is what would be needed. Don't know how I didn't see that.
Assuming the original expression can be implemented in Simulink in the first place ....
One feasible apprroach may be to use symvar to find out the inputs to the epxression and create inport blocks for those, and create an outport block for the output
Then recurse as follows:
Check the symFunType of the expression.
Map the result to the a Simulink library block and add it to the model.
Determine if the expression has additional children that need to processed.
All while keeping track of the parent block to to which the children blocks need to be connected.
Don't know if that will really work, but it seems like it might.
Cedric Kotitschke
Cedric Kotitschke 2023년 2월 11일
The children and the symFunType command was exactly what I needed, thank you! I actually made it work now :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by