Message Boards Message Boards


Export transfer function to Matlab: force string with negative exponents

Posted 2 months ago
1 Reply
1 Total Likes

I calculated some transfer functions in Mathematica analytically and would now like to import them into Matlab for controller optimization. The problem is now, that these transfer functions contain numerous delay times, which are expressed as "exp(-s*Tdel)". I use the following command to generate a string from the transfer functions:

StringReplace[ToString[Gref, InputForm], {"E^" -> "exp", "*^" -> "e"}]

Then I copy the string into a Matlab script. This works fine as long as I don't have any time delays, however, if I add the time delays I face the problem that Matlab can't handle negative delay times (Tdel being negative). Mathematica on the other hand tries everything to get rid of the negative exponents and constantly changes these expressions from ... * exp(-s Tdel) to 1/exp(s Tdel). Is there a proper way to get my equations from Mathematica into Matlab?

I tried replacing s wiith (-y) and replacing it back in Matlab, however, some of the time delays stayed negative, ending up positive then. I'm looking for a way to force Mathematica, to explicitly use only negative (or only positive) exponents.


First, you state you "would now like to import them into Matlab for controller optimization". Why can't you do your controller design in Mathematica? It has all the functionality of Matlab. Typically you would convert the TransferFunction to a StateSpaceModel and use any of these controller design functions. Then you can use these connection functions to join your controller and system together. You can simulate results. Lastly, you can export the controller directly to Wolfram SystemModeler and simulate the results on a WSM model of a system. This is far greater functionality than Matlab.

HOWEVER, if you really want to go to Matlab with strings, I would suggest you use SystemModelDelay instead of the Exp[] form. The notation is easier and the conversion to what you want is more direct because Mathematica does not try to do simplification of the Exp[] terms during InputForm.

Instead of

TransferFunctionModel[{{{Exp[-3 s] (s + 1)}}, s^2 - 2 s + 1}, s]

do this instead for your derivations:

foo = TransferFunctionModel[{{{SystemsModelDelay[3] - 1}}, s + 3}, s]
bar = StringReplace[ 
  ToString[foo // InputForm] , {"SystemsModelDelay[" ~~ Shortest[x__] ~~ "]" :> 
    "exp(-s * " <> x <> ")"}]

to get the string

TransferFunctionModel[{{{-1 + exp(-s * 3]}}, 3 + s}, s)

If you REALLY want to use Exp for your derivation, then you need to do an Mathematica replacement before doing your string replacement:

bar2 = StringReplace[ 
  ToString[(foo2 /. Exp[s * x_] -> SystemsModelDelay[-x]) // 
    InputForm] , {"SystemsModelDelay[" ~~ Shortest[x__] ~~ "]" :> 
    "exp(-s * " <> x <> ")"}]

The reason for this replacement is that the internal representation of Exp[-3 * s] is actually Power[E,Times[-3,s]] which when multiplied by anything else gets simplified to 1/Exp[3 s] in InputForm. The way around this is to replace the Exp[-3 * s] with SystemsModelDelay. You need to change sign on the constant because SystemModelDelay takes a positive number for what would normally be a negative exponent. An alternative to using SystemsModelDelay and changing the sign on the constant is to make your own temporary function to replace the time delays and replace that new function in the string:

bar2 = StringReplace[ 
  ToString[(foo2 /. Exp[s * x_] :> myExp[x]) // 
    InputForm] , {"myExp[" ~~ Shortest[x__] ~~ "]" :> 
    "exp(" <> x <> " * s)"}]



Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract