How can i display symbolic expressions using decimals instead of rational fractions?

Hello,
When I express symbolic expressions using syms with decimal values, I often get very large fractions. Since the result is not constant, I cannot use double() to evaluate the result as a decimal. sym2poly() works in most cases, but one day I will probably need to evaluate a long expression that is not a polynomial. I have also tried sym(expression,'d') but this doesn't work for non-numeric inputs. Is there some way to display symbolic expressions using decimals?
Thanks!
Example:
syms SIG_X SIG_Y SIG_Z
S_bar = [[0.78,-0.35,-0.38];[-0.35,0.78,-0.38];[-0.38,-0.38,0.92]]; %[Pa]
alpha = [-0.018,24.3,24.3]*10^-6; %[/C]
stress = [SIG_X;SIG_Y;SIG_Z];
dT = 25;
strain = S_bar*stress+alpha'*dT';
disp(strain)
Result:
(39*SIG_X)/50 - (7*SIG_Y)/20 - (19*SIG_Z)/50 - 8500259669165361/18889465931478580854784
(39*SIG_Y)/50 - (7*SIG_X)/20 - (19*SIG_Z)/50 + 5603198512389277/9223372036854775808
(23*SIG_Z)/25 - (19*SIG_Y)/50 - (19*SIG_X)/50 + 5603198512389277/9223372036854775808
Desired Result:
0.78*SIG_X - 0.35*SIG_Y - 0.38*SIG_Z - 4.5e-07
0.78*SIG_Y - 0.35*SIG_X - 0.38*SIG_Z + 6.075e-04
0.92*SIG_Z - 0.38*SIG_Y - 0.38*SIG_X + 6.075e-04

 Accepted Answer

Dennis, you could use something like
vpa(strain,5)

13 Comments

Thank you! This is exactly what I was looking for :D
Thank you, Ryan Reynolds look-alike! This helped me too!
Thank you, Ryan Reynolds look-alike! This helped me too!
Thanks for your direct answer it helps me after long search.
in the code
vpa(strain,5)
what is the specific use of 5? thanks
The second argument of the vpa command (in this case "5") determines the number of significant digits that are displayed.
From the documentation :
a = vpa(1/3, 4)
a =
0.3333
thank you! I just used it in our exam. It really helped. I was changing the number and observed the display changed but i couldn't figure out exactly what's happening...
thanks again
Instead of using the second argument to "vpa", it's recommended to use "digits" instead so that all your calculations now use 5 digits. See: http://www.mathworks.com/help/symbolic/digits.html
Karan Gill recommended changing "digits". I do not agree, not for this context. If you change "digits" then you change how the calculation is done at each step, which can result in very different intermediate answers, leading you to a different solution completely, whereas what you want is to display the current solution to fewer digits. For example, 1/(1000001 - 1000000) is clearly 1/1 and not a problem, but with digits(5), 1000001 would be rounded to 1.00000e6 and 1000000 would be rounded to the same, and subtraction would give 0, leading to 1/0 .
@Walter Roberson first, 'vpa' is simplifying the fraction in decimal form, secondly, 'vpa(strain,a)' is specifying the order of accuracy 'd'.
So, 'vpa(strain)', would also work, if you don't specify any decimal accuracy.
When you vpa(strain) then that is the same as vpa(strain, digits()) -- it does the vpa at the number of default digits you have set up, rather than doing a different operation.

Sign in to comment.

More Answers (1)

Can we declare variables in matrix as 'real number' . if , y, then how to declare variable of matrix A as 'Real number'
A=[[sym( 'S_x', [N 3]) sym( 'S_y', [N 3]) ]

Categories

Find more on Phased Array System Toolbox 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!