1

From a simulation software, I obtain in a matlab file my algebro-differential equations in the following way :

C_p_e = C_state/C_c;

C_p_f = I_state/I_i;

R_p_e = R_r*C_p_f;

I_p_e = (Se_p_e-R_p_e)-C_p_e;

For theses expressions, I would like to do two operations : 1) Transform it into equations 2) Conduct substitutions so as to change the names of the variables with nicer names.

For this objectives, I would like to automatisation the creation of the four equations eq1, eq2, eq3, eq4. For the moment, to create these equations, I only do copy/ paste from matlab but i have models with far more equations.

How can I create equations labeled eq1, ...eqn from this list of expressions (or more assignments in matlab) from Matlab in Mathematica?

Thank you for your help

Bendesarts
  • 1,099
  • 5
  • 12
  • What format is the file? Can you import the equations as strings? – march Aug 25 '15 at 15:23
  • I have .m file from Matlab. Here i only make copy/ paste from this file. – Bendesarts Aug 25 '15 at 17:16
  • Thanks a lot for your very useful help. It seems that the code is very closed to one that I wanted. However, I have a problem at the end with 'list = StringSplit[comm2, ";\n"] Scan[(eqn[#] = list[[#]]) &, Range@Length@list]' It seems that these lines are not working. Is it possible for me to send you my code ? Otherwise, may you tell me how I can easily show you the status of my code. Thanks a lot for your help – Bendesarts Aug 25 '15 at 19:58
  • It's not working because I forgot an essential part, which is to do ToExpression. See edited answer (fix is at end). – march Aug 25 '15 at 20:07
  • Thank you your code works perfectly. It remains me only the step of variables substitutions to make the equations nicer. But, my code seems not working. May you have a look to my lastest code ? The 3 first commands are identic and after list = StringSplit[comm2, ";"] list = Replace[ list, {cC[p, e] -> fs[t], cC[p, f] -> v[t], rR[p, e] -> fd[t], iI[p, e] -> fm[t], cC[c] = 1/k, iI[i] = m, rR[r] -> c, sSe[p, e] -> fe[t], iI[state] -> Int[fm[t], t], cC[state] -> Int[v[t], t]}, [Infinity]] Scan[(eqn[#] = ToExpression@list[[#]]) &, Range@Length@list] – Bendesarts Aug 25 '15 at 20:37
  • See above It doesn't work but you should manage to understand my need @march – Bendesarts Aug 25 '15 at 20:40
  • list is still a List of Strings. You can't use Replace on Strings. Either use StringReplace on each element in list or do the replacement after you do the Scan. As it is, I think if you go through and understand what all the command in the answer are doing, you will be able to fix the problem on your own. – march Aug 25 '15 at 20:51
  • I hope I will enough clear on theses last comments. Don't hesitate to let me know. – Bendesarts Aug 25 '15 at 20:52
  • You right I should manage to modify the code now.Thank you. Otherwise, i don't understand this part of the code : "\n\n" :> "\n". What \n means ? I ask Mathematica by pressing F1 but it didn't receive an answer. And why have you chosen a rule delayed ? Thank you in advance for your help. @march – Bendesarts Aug 26 '15 at 08:44
  • RuleDelayed is just habit sometimes. It doesn't matter there. "\n" is the character that stands for "end-of-line". So any time a new line begins, MMA sees it as "\n". I'm just replacing two of them (there is an extra space between lines) with one. – march Aug 26 '15 at 15:07
  • comm1 = StringReplace[commands, {"\n\n" :> "\n", "=" -> "=="}] I try to adapt my code but it no longer works comm2 = StringReplace[ comm1, {"C_p_e " -> "fs[t]", "C_p_f " -> "v[t]", "R_p_e" -> "fd[t]", "I_p_e" -> "fm[t]", "C_c" = "1/k", "I_i" = "m", "R_r" -> "c", "Se_p_e" -> "fe[t]", "I_state" -> "Int[fm[t],t]",

    "C_state" -> "Int[v[t],t]"}] Do you have an idea why the 2nd line doesn't work ? @march

    – Bendesarts Aug 26 '15 at 15:50
  • I went to the chat and left some messages there that should help out. Otherwise, I have to move on to other things. – march Aug 26 '15 at 16:31

1 Answers1

1

I can't guarantee that the Import will work properly without having a sample ".m" file, but somewhere in my dim past, I've used MatLab, and I recall that ".m" files are simple text files. Therefore, we can do the following.

Suppose that the file in which you have stored

C_p_e = C_state/C_c;

C_p_f = I_state/I_i;

R_p_e = R_r*C_p_f;

I_p_e = (Se_p_e-R_p_e)-C_p_e;

is called temp.m. Then, we import the file as a String using

commands = Import["temp.m", "String"];

We then want to change the variable names that have underscores with the corresponding notation in MMA (which does not allow underscores in variable names). Here's one possibility. We first replace all capital letters by lower-case ones so as not to conflict with built-in MMA commands (for instance, both C and I are built-in protected symbols in MMA). Since you are using lower-case and upper-case versions of the same letter, I will do, for instance, cC in place of C:

comm1 = StringReplace[commands, {"\n\n" :> "\n", a_?UpperCaseQ :> ToLowerCase[a] <> a}]
(* cC_p_e:=cC_state/cC_c;
   cC_p_f:=iI_state/iI_i;
   rR_p_e:=rR_r*cC_p_f;
   iI_p_e:=(sSe_p_e-rR_p_e)-cC_p_e; *)

(I have also gotten rid of the extra lines in between expressions for convenience.)

Next, we turn the underscores into brackets (in some sense):

comm2 = StringReplace[comm1
  , {"_" ~~ pat1 : LetterCharacter .. ~~ "_" ~~ pat2 : LetterCharacter .. :> "[" <> pat1 <> "," <> pat2 <> "]"
  , "_" ~~ pat : LetterCharacter .. :> "[" <> pat <> "]"}]
(* cC[p,e]=cC[state]/cC[c];
   cC[p,f]=iI[state]/iI[i];
   rR[p,e]=rR[r]*cC[p,f];
   iI[p,e]=(sSe[p,e]-rR[p,e])-cC[p,e]; *)

Finally, we evaluate this using

comm2 // ToExpression;

You will find, upon testing ?cC that cC[p,e] has been set to cC[state]/cC[c] and cC[p,f] has been set to iI[state]/iI[i].


Now, if you want to make MMA equations, modify the above with the following.

(1) Add "=" -> "==" to the first list of StringReplace replacements.

(2) After the rest has been executed, do

list = StringSplit[comm2, ";"];
Scan[(eqn[#] = ToExpression @ list[[#]]) &, Range@Length@list];

This will assign the equation cC[p,f]==iI[state]/iI[i] to eqn[2], for instance.

march
  • 23,399
  • 2
  • 44
  • 100
  • Thank you for your help. comm1 = StringReplace[commands, {"\n\n" :> "\n", "=" -> "=="}] – Bendesarts May 07 '16 at 07:39
  • comm1 = StringReplace[commands, {"\n\n" :> "\n", "=" -> "=="}] Can you explain me what the delayed rule "\n\n" :> "\n" is doing ? – Bendesarts May 07 '16 at 07:40