A simple algebraic which I failed to do

3 visualizzazioni (ultimi 30 giorni)
Hello friends!
Unfortunately, I have to tell you that I am unable to do a simple math I learned at intermediate school in matlab. Consider the following expression
syms dt D0 D1 D2 y y0
f=-(4*D0 - dt*D2 + 4*y*D0^2 - 4*y0*D0^2 + y^2*D2 + y0^2*D2 + 4*y*D1 - 4*y0*D1 - 2*dt*D0*D1 - 2*y*y0*D2 + 2*y^2*D0*D1 + 2*y0^2*D0*D1 - 4*y*y0*D0*D1)/(48*dt^(3/2));
I want to re-write f as in a way that each term has one single coefficient as bellow:
f = (-0.08333*D0 + 0.02083*dt*D2 - 0.08333*y*D0^2 + 0.08333*y0*D0^2 - 0.02083*y^2*D2 - 0.02083*y0^2*D2 - 0.08333*y*D1 + 0.08333*y0*D1 + 0.04167*dt*D0*D1 + 0.04167*y*y0*D2 - 0.04167*y^2*D0*D1 - 0.04167*y0^2*D0*D1 + 0.08333*y*y0*D0*D1)/(dt^(3/2));
where each coefficient is approximated using the vpa command, for instance the first coefficient is vpa(-4/48*4,4)=0.08333.
However, if I directly apply the vpa command it does not do the job correctly as you can see in bellow
vpa(f,4) = -(0.02083*(4.0*D0 - 1.0*D2*dt + 4.0*D1*y - 4.0*D1*y0 + 4.0*D0^2*y - 4.0*D0^2*y0 + D2*y^2 + D2*y0^2 - 2.0*D0*D1*dt - 2.0*D2*y*y0 + 2.0*D0*D1*y^2 + 2.0*D0*D1*y0^2 - 4.0*D0*D1*y*y0))/dt^(3/2)
which is never what I want.
Any idea?
Thanks in advance!
Babak
  2 Commenti
Star Strider
Star Strider il 3 Gen 2022
Mohammad Shojaei Arani’s last Commnent to my now deleted Answer —
But, nobody answered my main problem "Is there a way to release the memory inside a code? something that 'pack' cannot not do". In fact, the reason I was thinking to round my big numbers was due to the fact that matlab was not able to release the memory while my code was running andto fix this extremely annoying problem I had to think how to shorten my text files, by roundin, etc., in order to be able to cope with lost memories. Later, I realized that my idea to round the numbers was not a good.
So, the main problem with matlab is what I said above. I must stress that this is an extremely big caveat for the success and let's say the survival of a programming language and if the developers of the language do not fix this problem researcher will move to other programming languages especially python which is also free!!! Remember the concept of evolution. Those species which cannot fit themselves to the environmental change and conditions will definitely go extinct!
Thanks for all of your helps and good ideas!
Babak
Star Strider
Star Strider il 3 Gen 2022
Note —
I deleted my Answer because this discussion is not going anywhere.
The actual problem has to do with stochastic differential equations, not precisely the symbolic toolbox code, and should be restated to address the problem for which a solution is being requested.
.

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 3 Gen 2022
Modificato: Walter Roberson il 3 Gen 2022
"Is there a way to release the memory inside a code?"
NO.
"If matlab would have a garbage collector I would not need to bother you."
As I already explained to you, MATLAB does generally have garbage collection. It is not under user control, but it does not generally need to be. I will post evidence in a few minutes that it has garbage collection.
"If you know a way to release the memory within a code (I mean within the for-loop ( the first for-loop) in the first code) then I am done."
MATLAB generally releases memory automatically, soon after it can prove that a given block of memory is no longer being referenced. It is an open question as to whether MATLAB does garbage collection every time memory is released, or not until a certain amount of memory is queued to be released, or if it depends upon the amount of memory that was allocated. MATLAB has a pool of fixed-sized blocks that can exactly substituted for each other -- an array that is not being shared can grow in-place until it reaches the block size, but after that a block for the other pool is allocated. Such blocks can, in theory, be tracked by a simple occupancy bit-map, but memory optimization for variable-length blocks is more complicated.
"Why the command 'pack' only works outside the code?
pack() literally works by saving all of the variables, clearing memory, and reloading the variables. That requires that all of the variables be serializable -- and functions that are in the middle of executing are not serializable.
  9 Commenti
Walter Roberson
Walter Roberson il 3 Gen 2022
I would expect problems with memory allocation if you calculate numerous symbolic expressions and return them to the MATLAB level.
You should assume that the Symbolic Engine can do garbage collection on any intermediate results it needs internally, but that once you have returned the expression to the MATLAB level, that you have effectively generated a pointer that will not be released unless you reset the engine with
reset(symengine)
or clear all
Note: there is also a
collectGarbage(symengine)
but it will not clear out the _symans_ table.
Mohammad Shojaei Arani
Mohammad Shojaei Arani il 4 Gen 2022
Thanks a lot Walter!
I will try the two commands you mentioned to see how they help me to release the memory.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by