elemental wise function of any kind not functioning

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

"As you can see, Ka is not what it should be."
I doubt that.
Did you take into account the common multiplier at the top of the displayed output?:
"Which is simply not correct."
Lets check:
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
Ka = Ac.*Ac./(C-Ac)
Ka = 1×5
1.0e-04 * 0.1368 0.1443 0.1415 0.1426 0.1398
Ka(1)
ans = 1.3683e-05
Ac(1).^2./(C(1)-Ac(1))
ans = 1.3683e-05
So far everything looks correct.
Well, I definitely did read the common multiplier, which was not the issue. If you look at what I wrote further down, just typing in Ac.^2 the first number came back as 0.0132*10^-5. Anything that is 4^2 no matter the decimal should not have the numbers 132 as an answer, only 16. The answer to Ac.^2 should be:
[ 1.6 2.5 4.9 6.4 14.4 ] * 10^-7
But for some reason it isn't, and that's what I need an answer to. It's fully possible I've written something wrong, but I do need help identifying it. From my understanding, and past experiences with the program, adding a period before an operator should make the matrix do perform according to said operator with every single element. But that doesn't seem to be the case here
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.
"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)

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

Asked:

on 17 May 2022

Edited:

on 18 May 2022

Community Treasure Hunt

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

Start Hunting!