Handling multiple-line equations

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 Kommentare

Eng. Fredius Magige
Eng. Fredius Magige am 11 Dez. 2015
I think there are no simple way to copy or simplification of ... The only advice from mine is to observe the relationship among the equations which would allow you to generate subfunctions. It is normally and common to generate a capable and appropriate mathematical models, in which I believe your 100 some of them might have closely interrelated. Thanks
Walter Roberson
Walter Roberson am 11 Dez. 2015
What do they currently look like? Are you having Mathematica generate MATLAB code?
Ahmed Hassan
Ahmed Hassan am 11 Dez. 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 am 28 Feb. 2020
To convert Mathematica code to Matlab, look this video : https://www.youtube.com/watch?v=c7q3Lch9W-s

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Matt J
Matt J am 11 Dez. 2015
Bearbeitet: Matt J am 11 Dez. 2015

0 Stimmen

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 Kommentare

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 am 12 Dez. 2015
Thanks all for replying, I have also this package that transforms your equations into Matlab syntax http://library.wolfram.com/infocenter/MathSource/577/

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 11 Dez. 2015

Kommentiert:

am 28 Feb. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by