elemental wise function of any kind not functioning

1 回表示 (過去 30 日間)
Embla Dahlman
Embla Dahlman 2022 年 5 月 17 日
編集済み: Stephen23 2022 年 5 月 18 日
When I attempted to use .*, .^, .^/, none of them worked as they're supposed to. Below is the code and the answer it gave:
---------------------------------------------------------------------- Code
C = [ 0.01 0.02 0.04 0.05 0.1 ];
kappa = [ 0.01418 0.0207 0.0291 0.0327 0.0459 ];
Ac = kappa./(39.05.*10^(-3))*10^(-3)
Ka = Ac.*Ac./(C-Ac);
pKa = -log(Ka)
----------------------------------------------------------------------- What it gave
Ac =
0.0004 0.0005 0.0007 0.0008 0.0012
Ka =
1.0e-04 *
0.1368 0.1443 0.1415 0.1426 0.1398
-------------------------------------------------------------------------- End
As you can see, Ka is not what it should be. Just typing in Ac.^2 gave us
ans =
1.0e-05 *
0.0132 0.0281 0.0555 0.0701 0.1382
Which is simply not correct. How do I fix this error so I can actually use matlab again?
  4 件のコメント
Walter Roberson
Walter Roberson 2022 年 5 月 17 日
format long g
sqrt(0.0132e-5)
ans =
0.000363318042491699
Notice that 0.00036 rounds to 0.0004 to four digits. Your actual data is not exactly 4/10000, it is only being rounded to that value for display purposes because you have "format short" in effect.
Stephen23
Stephen23 2022 年 5 月 18 日
編集済み: Stephen23 2022 年 5 月 18 日
"0.0132*10^-5. Anything that is 4^2 no matter the decimal should not have the numbers 132 as an answer, only 16"
You are confusing how data are displayed with what data are actually stored in memory. Lets have a look:
C = [ 0.01 0.02 0.04 0.05 0.1 ];
kappa = [ 0.01418 0.0207 0.0291 0.0327 0.0459 ];
Ac = kappa./(39.05.*10^(-3))*10^(-3)
Ac = 1×5
0.0004 0.0005 0.0007 0.0008 0.0012
Ac(1)
ans = 3.6312e-04
The value is 0.0004 when displayed with four fractional digits. The value is 3.6312e-4 when displayed using exponent notation and five significant digits. Neither is more "correct" or "better" than the other, they are both just different ways of displaying the same value. The actual value stored in memory is of course neither of those, but you can display something closer:
fprintf('%.50f\n',Ac(1))
0.00036312419974391800829224830060582007718039676547
Or show the exact value as hexadecimal:
num2hex(Ac(1))
ans = '3f37cc368ff1feb3'
If you want to see the value as decimal, download NUM2STREXACT here:
"But that doesn't seem to be the case here"
It is the case here.
Numeric computations use numeric data types which are displayed with a finite precision which is usually less than their stored precision. Do not confuse the displayed digits with the actual numeric (binary) value stored in memory.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 5 月 17 日
Give the command
format long g
and try again. You have format short in effect by default. format does not change the answer, just how the answer is displayed.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by