Why Do MATLAB and My Calculator Show Different Results for g(x) and h(x)?

  • Thread starter Thread starter ver_mathstats
  • Start date Start date
  • Tags Tags
    Matlab Output
Click For Summary
SUMMARY

The discussion centers on discrepancies between MATLAB and a calculator when evaluating the functions g(x) and h(x) at specific values. For x=1e-10, both functions yield the same numerator, yet g(x) results in a positive number while h(x) equals zero, attributed to roundoff error. Additionally, for x=2-33, g(x) produces a negative result on the calculator, while MATLAB returns zero, highlighting the impact of numerical precision and the order of operations on computational results.

PREREQUISITES
  • Understanding of numerical methods and roundoff error
  • Familiarity with MATLAB syntax and functions
  • Basic knowledge of mathematical functions and their evaluations
  • Concept of precision in floating-point arithmetic
NEXT STEPS
  • Research MATLAB's floating-point arithmetic and precision handling
  • Learn about roundoff error and its effects on numerical computations
  • Explore the order of operations in programming languages
  • Investigate differences in numerical precision between calculators and software like MATLAB
USEFUL FOR

Mathematicians, engineers, students in numerical analysis, and anyone using MATLAB for computational tasks who seeks to understand discrepancies in numerical results.

ver_mathstats
Messages
258
Reaction score
21
Homework Statement
We are required to explain the values of g(x) and h(x).

The script for Matlab is:
g = @(x) (exp(x)-1-x)./x.^2;
h = @(x) (exp(x)-x-1)./x.^2;
x = 1e-10;
fprintf(’x=%.16e\ng(x)=%.16e\nh(x)=%.16e\n’, x, g(x), h(x))
x = 2^(-33);
fprintf(’x=%.16e\ng(x)=%.16e\nh(x)=%.16e\n’, x, g(x), h(x))

The output is:
x=1.0000000000000000e-10
g(x)=8.2740370962658164e+02
h(x)=0.0000000000000000e+00
x=1.1641532182693481e-10
g(x)=0.0000000000000000e+00
h(x)=0.0000000000000000e+00
Relevant Equations
-
I am having a bit of trouble understanding what the values of g(x) and h(x) mean. I went through it by hand and here is information that I gathered about the values and questions I am trying to answer to gain a better understanding. Firstly, for the value of x=1e-10 both the numerator of g(x) and the numerator of h(x) equate to the same number on my calculator and of course they have the same denominator, but then I don't understand why g(x) equals a positive number whereas h(x) equals 0, does this all just come from the error? I get the answer I am expecting for g(x) but not h(x).

Secondly, when I calculate g(x) for x=2-33 I get a negative number and when I calculate h(x) when x=2-33 I get 0. But why would the output for g(x) be 0 on MATLAB but when I calculate it I obtain a negative number? Is this simply only due to the precision of using 16 decimals whereas on my calculator it's much less?

Any help would be appreciated, thanks.
 
Physics news on Phys.org
ver_mathstats said:
I went through it by hand ... equate to the same number on my calculator
Working it out on your calculator is not going through it by hand.

ver_mathstats said:
does this all just come from the error? ... Is this simply only due to the precision of using 16 decimals whereas on my calculator it's much less?
This question is all about the impact of the order of operations on roundoff error.
 
  • Like
Likes   Reactions: ver_mathstats and DrClaude

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
901
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K