Producing incorrect results with abs command

Hey, I am trying to do some pretty basic math here. The equation Im using is which as a i understand would be written f8=abs(x.^x)-2.*x where x is a -10:10 matrix. However, when I run the code, the answers are incorrect. they should start at -20 but the code outputs 0E9. any ideas what I am missing?

댓글 수: 2

dpb
dpb 2022년 9월 4일
Dunno. Attach actual case that you think fails, don't just try to describe it to us...
Cole
Cole 2022년 9월 4일
This is my code output for a a -10:10 matrix.
using a calculator to check those outputs shows it is wrong. for example |-10^-10|-(2*-10) would equal 20.00000000001 not 0 like the output shows. I have to believe it is either the abs function or the subtraction because if I input the values manually, i get the correct answers

댓글을 달려면 로그인하십시오.

 채택된 답변

You overlooked the "1.0e+09 *" part of the output. You are using format short, which displays only a limited number of decimal places for each item in order to create a compact display.
x = -10:10;
f8 = abs(x.^x) -2.*x;
f8
f8 = 1×21
1.0e+09 * 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 -0.0000 0 0.0000 0.0000 0.0000 0.0000 0.0008 0.0168 0.3874 10.0000
format long g
f8
f8 = 1×21
1.0e+00 * 20.0000000001 18.0000000025812 16.0000000596046 14.0000012142657 12.0000214334705 10.00032 8.00390625 6.03703703703704 4.25 3 1 -1 0 21 248 3115 46644 823529 16777200 387420471 9999999980

댓글 수: 4

Cole
Cole 2022년 9월 4일
is there a way to translate this to a plot so I dont just show a ton of 0's along the x-axis
dpb
dpb 2022년 9월 4일
With range of 10E9, you can try logarithmic axes and hope for the best...
f8 will be negative in a certain subset of the x-range ...
dpb
dpb 2022년 9월 4일
Well, there's always the FEX submission <symlog>

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2022년 9월 4일
You didn't provide the input x array so we can (easily) duplicate, but now this one is easy... :)
You're confusing the output display with default format short compared to the actual result stored in memory.
NB: the "1.0e+09 *" at the beginning -- that's a common e-format multiplier applied on all results as printed. The result of the first location thus is reported as "0.000E9" which clearly doesn't have sufficient precision to show a value of 20-something on a 10E9 range.
Try looking at
f8(1)
all by itself and the mystery will go away.

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2022년 9월 4일

댓글:

dpb
2022년 9월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by