elemental wise function of any kind not functioning

1 view (last 30 days)
Embla Dahlman
Embla Dahlman on 17 May 2022
Edited: Stephen23 on 18 May 2022
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 Comments
Stephen23
Stephen23 on 18 May 2022
"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.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 17 May 2022
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.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!