A simple algebraic calculation does not work properly!

4 ビュー (過去 30 日間)
Mohammad Shojaei Arani
Mohammad Shojaei Arani 2021 年 12 月 27 日
回答済み: Walter Roberson 2021 年 12 月 28 日
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

回答 (2 件)

Dyuman Joshi
Dyuman Joshi 2021 年 12 月 28 日
編集済み: Dyuman Joshi 2021 年 12 月 28 日
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)
ans = 

Walter Roberson
Walter Roberson 2021 年 12 月 28 日
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))
f = 
newF = mapSymType(f, 'constant', @(v) round(v,4))
newF = 
If you need the powers to be preserved as rationals, then that is possible, but more of a nuisance.

Community Treasure Hunt

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

Start Hunting!

Translated by