Hey all,
I have come across some weird behavior where I'm curious whether somebody might know the cause.
I'm trying to check if two (rather complicated) symbolic expressions are the same (spoiler: they are) using a matlab live script
They both contain pi as a part of a fraction. Strangely in one of them pi internally gets replaced by a numeric representation, as can be seen in the output. (More specifically 2/pi^2 gets replaced by 562949953421312 / 2778046668940015)
Thus when I substract the two expressions from each other and use simplify() on the result its not zero (as it should be).
This can be fixed, by defining pi as my own symbol, by adding the line "syms pi". This prevents Matlab from replacing pi and the result of the final substraction is indeed 0.
Does anybody know the cause of this odd behavior? Why does Matlab keep pi in one expression, but unnecessarily replaces it in the other?
This is the code where the behavior can be observed (at least for me, as mentioned I'm running it as a .mlx live scrpt)
(You can ignore most of the stuff in the front, the thing that matters is that rest is !=0 in the first run, but after adding "syms pi" rest=0)
clearsyms phi phid phidd ra ri b l mb ml J g di Dra Dri Dbsyms Mpressure p0 lr rho eta lambda Re deltapi deltaprsyms A r d V0syms phiddold phiddnew Mpressold Mpressnewsyms Reold Renew pralt prnew%syms pi
A = b*(ra-ri)d = (ra - ri)r = (ri + (ra-ri)/2)Dra = 0Dri = 0Db = 0V0 = pi*(ra^2-ri^2)*bphiddold = (-(A*r*(deltapr - p0) - (l^2*mb*phid^2*sin(phi))/4 - (l^2*ml*phid^2*sin(phi))/8 + g*l*mb*cos(phi/2) + g*l*ml*cos(phi/2))/(J/2 + (l^2*mb)/2 + (3*l^2*ml)/8 + (l^2*mb*cos(phi))/2 + (l^2*ml*cos(phi))/4 + (2*A*V0*lr*r*rho)/(di^2*pi^2)))phiddnew = -(g*l*mb*cos(phi/2) - (l^2*ml*phid^2*sin(phi))/8 - (l^2*mb*phid^2*sin(phi))/4 + g*l*ml*cos(phi/2) + (b*(ra - ri)*(ra/2 + ri/2)*(pi*deltapr*di^2 - pi*di^2*p0 + 2*lr*phid^2*rho*Db*ra^2 - 2*lr*phid^2*rho*Db*ri^2 + 4*lr*phid^2*rho*Dra*b*ra - 4*lr*phid^2*rho*Dri*b*ri))/(di^2*pi))/(J/2 + (l^2*mb)/2 + (3*l^2*ml)/8 + (l^2*mb*cos(phi))/2 + (l^2*ml*cos(phi))/4 + (b*(2*lr*rho*b*ra^2 - 2*lr*rho*b*ri^2)*(ra - ri)*(ra/2 + ri/2))/(di^2*pi))rest = simplify(phiddold-phiddnew)
Best Answer