ans representation in decimal

Good day.
Houp, I could get some hint on how to change the formatting of answers in a live script.
syms ht_inpt(mass, dlt_t, spc_ht_mtr)
ht_inpt(mass, dlt_t, spc_ht_mtr)=mass*dlt_t*spc_ht_mtr;
mass=6*u.kg;
mass_t1=(20+273)*u.K;
mass_t2=(100+273)*u.K;
spc_ht_mtr=4190*u.J/(u.kg*u.K);
dlt_t=mass_t2-mass_t1;
ht_inpt=ht_inpt(mass, dlt_t, spc_ht_mtr)
ht_inpt=2011200 J
ht_inpt = unitConvert(ht_inpt,u.kJ)
ht_inpt=10056/5 kJ
­How a document can be edited that all answers in it would be expressed with decimals?

 Accepted Answer

Cris LaPierre
Cris LaPierre on 1 Sep 2020
Edited: Cris LaPierre on 1 Sep 2020
FYI the code you shared generates the following error
Unable to resolve the name u.kg.
Add the following to fix that:
u = symunit;
I suspect the issue is that you are using symbolic. If you want to keep your answer symbolic, see this page (specifically, sympref('FloatingPointOutput',true);). If you want to convert it to numbers, use the double function.

6 Comments

Code with corrections for clarity.
u=symunit;
syms ht_inpt(mass, dlt_t, spc_ht_mtr)
ht_inpt(mass, dlt_t, spc_ht_mtr)=mass*dlt_t*spc_ht_mtr;
mass=6*u.kg;
mass_t1=(20+273)*u.K;
mass_t2=(100+273)*u.K;
spc_ht_mtr=4190*u.J/(u.kg*u.K);
dlt_t=mass_t2-mass_t1;
ht_inpt=ht_inpt(mass, dlt_t, spc_ht_mtr);
ht_inpt = unitConvert(ht_inpt,u.kJ)
Expected ans is 2011.2 kJ instead of 10056/5 kJ.
double didn't helpt. format long/short all so makes no changes...
sympref('FloatingPointOutput',true);
ht_inpt = unitConvert(ht_inpt,u.kJ)
ht_inpt =
2.0112e+03 kJ
[ht units] = separateUnits(ht_inpt);
double(ht)
ans = 2.0112e+03
Thank You. It suits the main goal.
Is it possible to avoid “e”?
For symbolic, see this post. For the double result, use format shortG.
[ht units] = separateUnits(ht_inpt);
format shortG
double(ht)
ans =
2011.2
If you want to include the units, you'd have to build a string.
ht_dec = sprintf('%.1f %s',double(ht),'kJ')
ht = '2011.2 kJ'
TY4I :)

Sign in to comment.

More Answers (0)

Asked:

on 1 Sep 2020

Commented:

on 1 Sep 2020

Community Treasure Hunt

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

Start Hunting!