Why is it when I am adding a number to array, the single number overrides array

5 ビュー (過去 30 日間)
Charles Bradford
Charles Bradford 2024 年 9 月 10 日
編集済み: Stephen23 2024 年 9 月 10 日
Currently trying to add a single number to an array, but despite the array initially outputting well, the moment I try to add to it, the entire array is replaced by the number I am adding. This can be best seen by running "lownutant" which outputs a 1x50 array, but adding 1 changes all values to 1.
% Modified Ramberg-Osgood Tension
E=10.3*10^6; %psi
Fty=68000; %psi
Ftu=76000; %psi
fnl=linspace(0,Ftu,250);
e0=0.09;
epu=e0-(Ftu/E);
nt=log(epu/.002)/log(Ftu/Fty);
enl=(fnl./E)+(0.002.*((fnl./Fty).^nt));
% Tangent Modulus
dEtan=linspace(.025,.03,50) %Domain for plotting Etan
dEtan = 1x50
0.0250 0.0251 0.0252 0.0253 0.0254 0.0255 0.0256 0.0257 0.0258 0.0259 0.0260 0.0261 0.0262 0.0263 0.0264 0.0265 0.0266 0.0267 0.0268 0.0269 0.0270 0.0271 0.0272 0.0273 0.0274 0.0276 0.0277 0.0278 0.0279 0.0280
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
lownutant=((.002.*E.*nt./Fty)).*((dEtan./Fty).^(nt-1))
lownutant = 1x50
1.0e-205 * 0.0015 0.0017 0.0019 0.0022 0.0025 0.0028 0.0032 0.0037 0.0042 0.0047 0.0054 0.0061 0.0070 0.0079 0.0089 0.0101 0.0115 0.0130 0.0147 0.0166 0.0188 0.0212 0.0240 0.0271 0.0306 0.0345 0.0389 0.0438 0.0494 0.0556
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
a=1
a = 1
b= a+lownutant
b = 1x50
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  2 件のコメント
Stephen23
Stephen23 2024 年 9 月 10 日
編集済み: Stephen23 2024 年 9 月 10 日
"...the entire array is replaced by the number I am adding"
Because the numbers you are adding are simply ginormously different in magnitude, certainly well beyond anything that any (commonly-used) numeric type can store in one number. Here are (approximately) two numbers that you are adding together:
fprintf('%.210f\n',1,0.0015e-205)
1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000150
Do you expect MATLAB to add those two numbers together numerically and then show you their sum which has digits over a range spanning more than 200 orders of magnitude?
Even if it was possible, how would that be useful as the default display behavior? I mean, as a human I easily lose track after five or six digits (without counting), are you telling me that you can read any number and you can tell at a glance that it has exactly 205 zeros before the non-zero digits? Or is it 206? Or maybe 207...? Perhaps I lost count.

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

採用された回答

Les Beckham
Les Beckham 2024 年 9 月 10 日
lownutant is so small (~10^207) that can't be represented in floating point after adding 1 to it.
Floating point resolution for numbers near 1 is only ~2*10^-16.
eps(1)
ans = 2.2204e-16
% Modified Ramberg-Osgood Tension
E=10.3*10^6; %psi
Fty=68000; %psi
Ftu=76000; %psi
fnl=linspace(0,Ftu,250);
e0=0.09;
epu=e0-(Ftu/E);
nt=log(epu/.002)/log(Ftu/Fty);
enl=(fnl./E)+(0.002.*((fnl./Fty).^nt));
% Tangent Modulus
dEtan=linspace(.025,.03,50) %Domain for plotting Etan
dEtan = 1x50
0.0250 0.0251 0.0252 0.0253 0.0254 0.0255 0.0256 0.0257 0.0258 0.0259 0.0260 0.0261 0.0262 0.0263 0.0264 0.0265 0.0266 0.0267 0.0268 0.0269 0.0270 0.0271 0.0272 0.0273 0.0274 0.0276 0.0277 0.0278 0.0279 0.0280
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
lownutant=((.002.*E.*nt./Fty)).*((dEtan./Fty).^(nt-1))
lownutant = 1x50
1.0e-205 * 0.0015 0.0017 0.0019 0.0022 0.0025 0.0028 0.0032 0.0037 0.0042 0.0047 0.0054 0.0061 0.0070 0.0079 0.0089 0.0101 0.0115 0.0130 0.0147 0.0166 0.0188 0.0212 0.0240 0.0271 0.0306 0.0345 0.0389 0.0438 0.0494 0.0556
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
a=1
a = 1
b= a+lownutant
b = 1x50
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

その他の回答 (2 件)

Milan Bansal
Milan Bansal 2024 年 9 月 10 日
Hi Charles Bradford
This behavior is due to the limitations of floating-point precision in MATLAB . The elements in "lownutant" array are of the order 10e-205 .When you have numbers that are extremely small, like those on the order of (10^{-205}), and you add a much larger number, such as 1, the result is dominated by the larger number due to the precision limits of floating-point arithmetic.
In MATLAB, the default numeric type is double precision, which follows the IEEE 754 standard for double-precision floating-point numbers. This standard provides about 15 to 17 significant decimal digits of precision. When you add 1 to a very small number like (10^{-205}), the small number is effectively lost in the arithmetic operation because it is much smaller than the precision limit.
Please refer to the following documentation link to learn more:
Hope this helps!

John D'Errico
John D'Errico 2024 年 9 月 10 日
編集済み: John D'Errico 2024 年 9 月 10 日
E=10.3*10^6; %psi
Fty=68000; %psi
Ftu=76000; %psi
fnl=linspace(0,Ftu,250);
e0=0.09;
epu=e0-(Ftu/E);
nt=log(epu/.002)/log(Ftu/Fty);
enl=(fnl./E)+(0.002.*((fnl./Fty).^nt));
% Tangent Modulus
dEtan=linspace(.025,.03,50); %Domain for plotting Etan
lownutant=((.002.*E.*nt./Fty)).*((dEtan./Fty).^(nt-1))
lownutant = 1x50
1.0e-205 * 0.0015 0.0017 0.0019 0.0022 0.0025 0.0028 0.0032 0.0037 0.0042 0.0047 0.0054 0.0061 0.0070 0.0079 0.0089 0.0101 0.0115 0.0130 0.0147 0.0166 0.0188 0.0212 0.0240 0.0271 0.0306 0.0345 0.0389 0.0438 0.0494 0.0556
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Funny, but I doubt that is true. In fact, I know your claim is false. But at the same time, it is true too. You need to understand floating point arithmetic.
Look at the vector lownutant. Do you see that factor of 10^-205 at the top? That means those numbers have roughly 205 zeros at the BEGINNING of the number, before you see a non-zero digit.
Now add 1 to that number. Does MATLAB store more than 200 decimal digits in a number? OF COURSE NOT! MATLAB uses double precision arithmetic, at least in what you are doing.
Essentially small numbers smaller than the number eps
eps
ans = 2.2204e-16
when added to 1, will give 1 back, when working in double precision. And the numbers in lownutant are many, many orders of magnitude smaller than eps.
So MATLAB did not replace the number with 1. Instead, you added a number too small to make a difference, and it got rounded to . There is a big difference. You could have used symbolic computation to do your work, but it will be very much slower.

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by