1

I wrote a symbolic Matlab code which manipulates matrices and vectors for a given problem. The calculations are performed very fast. In order to see the results though I try to print a symbolic expression into a .txt file. Before that I need to convert the sym to a string with char() so then i can fprintf the variable. The char() function is making the code extremely slow and need a lot of memory.

Thanks

Alex
  • 63
  • 1
  • 1
  • 8
  • How big is the matrix? – Eugene Sh. Feb 25 '15 at 18:33
  • Have you profiled this? Do you know if it's the writing that's slow (`fprintf`) or the conversion from `sym` to `char`? And yes, please specify what you mean by large. How big is your `J11` char array for a real case. Lastly, `J11=Jacobian(1,1);` isn't really valid code. A simple working example would be better. – horchler Feb 25 '15 at 19:11
  • The matrix is callied Jacobian and its 6x6. Each element is a product of symbolic manipulations calculated in a larger code and actually is the result of the Jacobian of a vector F(6) which I cannot print to show you cause each component is a very large function of symbolic variables. The J11 symbolic variable is the (1,1) component of the matrix called Jacobian and yes its a valid code. I did not check if the char(J11) is the slowest part, I will do that and come back. Thanks for your comments – Alex Feb 25 '15 at 19:28
  • 1
    @Alex: I don't mean the size of the symbolic `J11`, but the size of the char array once `J11` is converted to a string. That's the size of the data that gets written to file Type `whos` in your command window. – horchler Feb 25 '15 at 19:31
  • @Horchler: Your question helped me to understand that what makes the code slow is the conversion from sym to char with the char() function. Actually, its not only slow, it freezes the pc. Do you have any ideas? Do not hesitate to ask me more and I will test it. Thanks!!! – Alex Feb 26 '15 at 02:41
  • That is what I suspected. Conversion can use a lot of memory and is rather inefficient. What `version` of Matlab are you using? It would be helpful if you edited your original question to include runnable code (all variables/inputs defined) for what you're doing and that replicates the actual issue, including how you calculate the Jacobian. – horchler Feb 26 '15 at 03:06
  • The version is R2013a student version. I edited my question. – Alex Feb 26 '15 at 22:24
  • @Alex: You've now removed all actual code. Symbolic calculation is all about string manipulation and conversion back and forth from strings to Matlab's `sym` and `symfun` classes. To provide a helpful answer that is actually faster depends on the precise order of functions that are being called and the desired outputs. The idea is minimize the number of conversions and directly call lower-level functions and hard code things that don't change. See [this answer](http://stackoverflow.com/a/28398643/2278029) of mine for some suggestions. – horchler Mar 01 '15 at 18:02
  • @Horchler: Thanks a lot. Your answer to the other post was very helpful to understand the problem and make things more efficient. – Alex Mar 05 '15 at 15:40

0 Answers0