A simple algebraic calculation does not work properly!
1 view (last 30 days)
Show older comments
Hi friends!
I have been strugling to tell matab to do a simple calculation for me but I failed, unfortunately. I explain my problem using the following
simple problem (although my expressions are extremely large and more complicated). Consider the following function f:
syms dt y y0 muY(y)
f=-(4*muY(y)-7677*dt*diff(muY(y),y,y)+4434*y*muY(y)^2-4212*y0*muY(y)^2 +332*y^2*diff(muY(y),y,y))/(48*dt^(3/2));
Now, I would like to approximate f by rounding the coefficients with any decimal accuracy I wish. The command vpa(f,4) gives me
-(0.02083*(4.0*muY(y) - 7677.0*dt*diff(muY(y), y, y) + 4434.0*y*muY(y)^2 - 4212.0*y0*muY(y)^2 + 332.0*y^2*diff(muY(y), y, y)))/dt^(3/2)
which is not what I like. I wish to get a solution as bellow:
(-0.0833*muY(y)+159.9375*dt*diff(muY(y), y, y)-92.3750*y*muY(y)^2+87.7500*y0*muY(y)^2-6.9167*y^2*diff(muY(y),y,y))/dt^(3/2)
where each coefficient is calculated using the command round(-----,4). So, in this answer each coefficient is rounded by 4 decimal degit. I
do not want to use vpa since there is no way to tell vpa to consider 4 decimal degits after the decimal point. Unfortunately, vpa(----,n) only consideres n decimal degits in total. On the other hand, I cannot use the 'round' command here since I have a simbolic expression. If I use the command vpa(f,4) then I get the following undesirable answer, unfortunately:
0.0001*round(-(625*(4*muY(y) - 7677*dt*diff(muY(y), y, y) + 4434*y*muY(y)^2 - 4212*y0*muY(y)^2 + 332*y^2*diff(muY(y), y, y)))/(3*dt^(3/2)))
Any idea?
Thanks for your help in advance!
Babak
0 Comments
Answers (2)
Dyuman Joshi
on 28 Dec 2021
Edited: Dyuman Joshi
on 28 Dec 2021
syms dt y y0 muY(y)
f=-(4*muY(y)-7677*dt*diff(muY(y),y,y)+4434*y*muY(y)^2-4212*y0*muY(y)^2 +332*y^2*diff(muY(y),y,y))/(48*dt^(3/2));
vpa(simplify(f,3),7) %7 because the most amount of significant digits (159.9375)
0 Comments
Walter Roberson
on 28 Dec 2021
syms dt y y0 muY(y)
f = -(4*muY(y)-7677*dt*diff(muY(y),y,y)+4434*y*muY(y)^2-4212*y0*muY(y)^2 +332*y^2*diff(muY(y),y,y))/(48*dt^(3/2))
newF = mapSymType(f, 'constant', @(v) round(v,4))
If you need the powers to be preserved as rationals, then that is possible, but more of a nuisance.
0 Comments
See Also
Categories
Find more on Numbers and Precision in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!