A simple algebraic which I failed to do

1 view (last 30 days)
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 Comments
Star Strider
Star Strider on 3 Jan 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.
.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 3 Jan 2022
Edited: Walter Roberson on 3 Jan 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 Comments
Mohammad Shojaei Arani
Mohammad Shojaei Arani on 4 Jan 2022
Thanks a lot Walter!
I will try the two commands you mentioned to see how they help me to release the memory.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by