如何让MATLAB用e而不是分数以小数显示答案 [英] How to have MATLAB display answer in decimals with e instead of fractions

查看:622
本文介绍了如何让MATLAB用e而不是分数以小数显示答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,即时通讯使用MATLAB来计算方程的系数,并且它始终将分数显示为常量,而不是像xxe-x这样的十进制形式 我的代码在下面

So im using MATLAB to calculate the coefficient of an equation and it keeps displaying fractions as the constants instead of decimal form like xxe-x My code is below

bt = 0.03175;
bpzt = 0.0078;
ht = 0.003;
w = 50; % frequency in Hz
pnic = 8908; % density of nickel
Enic = 200e9; % Young's modulus of nic
L = 0.3048; % Length of canitlever beam in m

syms hn

inertia = (1/12)*bt*ht^3 + (1/2)*bpzt*ht^2*hn - (1/2)*bpzt*ht*hn^2 + (2/3)*bpzt*hn^3

area = (bt*ht - 2*hn*bt + 2*bpzt*hn);

推荐答案

在R2014b中,此行:

In R2014b, this line:

inertia = (1/12)*bt*ht^3 + (1/2)*bpzt*ht^2*hn - (1/2)*bpzt*ht*hn^2 + (2/3)*bpzt*hn^3

返回

(13*hn^3)/2500 - (863307622649607*hn^2)/73786976294838206464 + (5304162033559185*hn)/151115727451828646838272 + 5527208847278085/77371252455336267181195264

这是一个符号表达式,其数值完全表示为有理分数(即使它们在代码中可能以十进制值开头).您可以使用 vpa

which is a symbolic expression with numeric values represented exactly as rational fractions (even though they may have started out as decimal values in your code). You can convert this using vpa

vpa(inertia)

返回:

0.0052*hn^3 - 0.000011699999999999999788190263583232*hn^2 + 0.000000035099999999999996664653271376613*hn + 0.000000000071437500000000005341045287673881

十进制值的长度/精度取决于 digits .除非您编写自己的函数来解析字符串并进行转换,否则任何符号数学表达式或值都不能以指数样式(xxe-x)格式显示.

The length/precision of the decimal values depends on digits. Displaying this in an exponential-style format (xxe-x) is not an option for any symbolic math expression or value unless you write your own function to parse the string and do the conversion.

要将其转换为向量化的双精度浮点函数,可以使用名称非常严格的

To convert this to a vectorized double-precision floating point function, you can use the terribly-named matlabFunctionmatlabFunction(inertia) returns:

@(hn)hn.*3.51e-8-hn.^2.*1.17e-5+hn.^3.*5.2e-3+7.143750000000001e-11

但是,如果您这样做的话,我想知道为什么您首先要使用符号数学,并且是否有可能以更快的双精度来完成所有事情.

But if you're doing this, I'd wonder why you were working with symbolic math in the first place and if it's possible to do everything in much faster double precision.

请记住,如果要转换为任何一种十进制形式,可变精度或浮点数,在许多情况下, 都会失去精度.但是,如果您只想查看结果或在双精度情况下执行第四次计算,那应该没问题.

Keep in mind that if you want to convert to any kind of decimal form, variable precision or floating-point, in many cases you will lose precision. However, if you just want to view the result or perform fourth calculations if double precision, then it should be fine.

这篇关于如何让MATLAB用e而不是分数以小数显示答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆