A simple algebraic which I failed to do
1 view (last 30 days)
Mohammad Shojaei Arani on 29 Dec 2021
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.
Thanks in advance!
Walter Roberson on 3 Jan 2022
Edited: Walter Roberson on 3 Jan 2022
"Is there a way to release the memory inside a code?"
"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.