Handling multiple-line equations

9 views (last 30 days)
Ahmed Hassan
Ahmed Hassan on 11 Dec 2015
Commented: Eranga De Silva on 28 Feb 2020
Hello, I have some equations of motion derived in Mathematica and I'm transferring them to Matlab to solve them, so each equation is really long (more than 100 lines in the mfile), when I just did copy&paste it broke down to multiple lines so now I have to put these 3 dots "..." after each line manually which really very annoying for 5 equations (more than 100 lines each). So my question is, Is there any other automatic way or so to type these dots or to handle these very long equations by any other means?
Thanks
  4 Comments
Ahmed Hassan
Ahmed Hassan on 11 Dec 2015
No, I don't know how to make Mathematica generate Matlab code, I just made some replacements for the syntax to be compatible with Matlab and then took it (copy&paste).
Eranga De Silva
Eranga De Silva on 28 Feb 2020
To convert Mathematica code to Matlab, look this video : https://www.youtube.com/watch?v=c7q3Lch9W-s

Sign in to comment.

Answers (1)

Matt J
Matt J on 11 Dec 2015
Edited: Matt J on 11 Dec 2015
I agree with what Fredius said. It is a bad idea to work with equations hundreds of lines long, when they probably consist of numerous repetitions of the same expressions. This will be especially inefficient if you are going to try to solve them numerically (which presumably you are, else why migrate from a symbolic package like Mathematica to MATLAB). The better way to evaluate expressions with recurrences of intermediate quantities is to break them into multiple statements, e.g.,
expr1 = (x+1)^2+sin(x+1)^2 +sin(x+1)
would be coded as
tmp1=(x+1);
tmp2=sin(tmp1);
expr1= tmp1^2+tmp2^2+tmp2;
so that the intermediate calculations can be re-used.
Nevertheless, adding the ellipses can be automated as in the example below if you have the lines of your equations as a cell array, C, of strings. You can obtain such a cell array from a text file using the textscan() command.
>> C={'Line 1:blabla','Line 5: xxxxyyyyyy'};
C=char(cellfun(@(z) [z,' ...'], C,'uni',0))
C =
Line 1:blabla ...
Line 5: xxxxyyyyyy ...
It is then a simple matter of copy/pasting the result as displayed in the command window into a new file.
  2 Comments
Walter Roberson
Walter Roberson on 11 Dec 2015
filecontent = fileread('YourFileWithEquations.txt');
newcontent = regexprep(filecontent, '$', '...', 'lineanchors');
and then you could write newcontent to a file.
Or just use a decent editor like vi, which can make the change in a small number of keystrokes:
:%s/$/...
Ahmed Hassan
Ahmed Hassan on 12 Dec 2015
Thanks all for replying, I have also this package that transforms your equations into Matlab syntax http://library.wolfram.com/infocenter/MathSource/577/

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!