Handling multiple-line equations

조회 수: 9 (최근 30일)
Ahmed Hassan
Ahmed Hassan 2015년 12월 11일
댓글: Eranga De Silva 2020년 2월 28일
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
Ahmed Hassan
Ahmed Hassan 2015년 12월 11일
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 2020년 2월 28일
To convert Mathematica code to Matlab, look this video : https://www.youtube.com/watch?v=c7q3Lch9W-s

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

답변 (1개)

Matt J
Matt J 2015년 12월 11일
편집: Matt J 2015년 12월 11일
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
Walter Roberson
Walter Roberson 2015년 12월 11일
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 2015년 12월 12일
Thanks all for replying, I have also this package that transforms your equations into Matlab syntax http://library.wolfram.com/infocenter/MathSource/577/

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

Community Treasure Hunt

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

Start Hunting!

Translated by